簡體   English   中英

使用Python的OpenCV-將帽子的圖像放置在網絡攝像頭源的頭部

[英]OpenCV with Python - Placing an image of a hat over the head of a webcam feed

我試圖將帽子的png圖像放置在網絡攝像頭供稿的頭部上方。 我正在嘗試檢測一張臉並將圖像放置在它上面。 到目前為止,這是我的代碼-

import cv2
import numpy as np

face_cascade = cv2.CascadeClassifier('haarcascades/haarcascade_frontalface_default.xml')

imghat = cv2.imread('hat.png', -1)

print imghat is None

imghatGray = cv2.cvtColor(imghat, cv2.COLOR_BGR2GRAY)

ret, orig_mask = cv2.threshold(imghatGray, 0, 255, cv2.THRESH_BINARY)
orig_mask_inv = cv2.bitwise_not(orig_mask)

# Convert hat image to BGR
# and save the original image size (used later when re-sizing the image)
imghat = imghat[:,:,0:3]
origHatHeight, origHatWidth = imghat.shape[:2]

video_capture = cv2.VideoCapture(0)

while True:

    ret, frame = video_capture.read()

    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

    faces = face_cascade.detectMultiScale(gray, 1.3, 5, flags=cv2.cv.CV_HAAR_SCALE_IMAGE)

    for (x, y, w, h) in faces:
        print "x : %d , y : %d, w: %d, h: %d " %(x,y,w,h)
        cv2.rectangle(frame, (x,y), (x+w, y+h), (255,0,0), 2)
        cv2.rectangle(frame, (x-15,y-h), (x+w+15, y), (255,255,0), 2)

        print w
        print h
        hatWidth = w
        hatHeight = hatWidth * origHatHeight / origHatWidth

        roi_gray = gray[y-hatHeight:y, x-15:x+w+15]
        roi_color = frame[y-hatHeight:y, x-15:x+w+15]

        # Center the hat
        x1 = x - 15
        y1 = y - h
        x2 = x + w +15
        y2 = y

        cv2.rectangle(frame, (x1,y1), (x2, y2), (0,255,0), 2)

        # Check for clipping
        if x1 < 0:
            x1 = 0
        if y1 < 0:
            y1 = 0
        if x2 > w:
            x2 = w
        if y2 > h:
            y2 = h

        # Re-calculate the width and height of the hat image
        hatWidth = x2 - x1
        hatHeight = y2 - y1

        # Re-size the original image and the masks to the hat sizes
        # calcualted above
        hat = cv2.resize(imghat, (hatWidth,hatHeight), interpolation = cv2.INTER_AREA)
        mask = cv2.resize(orig_mask, (hatWidth,hatHeight), interpolation = cv2.INTER_AREA)
        mask_inv = cv2.resize(orig_mask_inv, (hatWidth,hatHeight), interpolation = cv2.INTER_AREA)

        # take ROI for hat from background equal to size of hat image
        roi = roi_color[y1:y2, x1:x2]

        # roi_bg contains the original image only where the hat is not
        # in the region that is the size of the hat.
        roi_bg = cv2.bitwise_and(roi,roi,mask = mask_inv)

        # roi_fg contains the image of the hat only where the hat is
        roi_fg = cv2.bitwise_and(hat,hat,mask = mask)

        # join the roi_bg and roi_fg
        dst = cv2.add(roi_bg,roi_fg)

        # place the joined image, saved to dst back over the original image
        roi_color[y1:y2, x1:x2] = dst

        break


    # Display the resulting frame
        cv2.imshow('Video', frame)

        if cv2.waitKey(1) & 0xFF == ord('q'):
            break

video_capture.release()
cv2.destroyAllWindows()

我收到此錯誤-OpenCV錯誤:每次運行setSize時斷言失敗(s> = 0) 網絡攝像頭啟動並突然關閉。 錯誤在-

            hat = cv2.resize(imghat, (hatWidth,hatHeight), interpolation = cv2.INTER_AREA)
            mask = cv2.resize(orig_mask, (hatWidth,hatHeight), interpolation = cv2.INTER_AREA)
            mask_inv = cv2.resize(orig_mask_inv, (hatWidth,hatHeight), interpolation = cv2.INTER_AREA)

hatWidth和hatHeight的值為負。 但是我找不到坐標分配的錯誤。 是否因為該計划的投資回報率?

在代碼Center the hat您擁有Center the hat

    x1 = x - 15
    x2 = x + w +15

其中x1和x2似乎是帽子的水平邊界。

然后,在幾行之后, 無需更改x1和x2的值

    # Check for clipping
    if x1 < 0:
        x1 = 0
    if x2 > w:
        x2 = w

這段代碼將始終修改x2,因為根據定義x2大於w,實際上是x + w + 15 這可能不是您想要的。

再將hatWidth設置為

hatWidth = x2 - x1

此時,由於上述“剪切”代碼,x2始終為w

因此,如果x2小於x1,這將使hatWidth為負數,這會導致您在cv2.resize( ... )看到問題。

x2何時會小於x1? 好吧,x2是w,x1是x - 15所以每當w <(x-15)等於w + 15 <x時,即每當檢測到的臉部位置比臉部寬度加15時就正確了; 這似乎是一件很可能經常發生的事情,實際上不應該成為問題。

我懷疑您的剪輯代碼應檢查x2是否大於圖像寬度,而不是面部寬度。

您對裁剪y2有類似的問題

我遇到了同樣的錯誤。 通過繪制坐標值可以找到解決方案。 其中之一就是圖像尺寸之外。 所以越來越出錯。 稍后我對其進行了修復,並設置了邊界條件,以使ROI尺寸不超過圖像尺寸。 不管怎么說,還是要謝謝你。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM