【问题标题】:OpenCV with Python - Placing an image of a hat over the head of a webcam feedOpenCV with Python - 将帽子的图像放在网络摄像头的头部
【发布时间】:2015-05-07 19:28:46
【问题描述】:

我正在尝试将帽子的 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 的值为负数。但我在坐标分配中找不到错误。是因为程序的投资回报率吗?

【问题讨论】:

  • 嘿,这篇文章好像已经有一段时间了。想要跟进 - 你有没有得到这个工作? 5 年后,这似乎仍然是一个合理的方法吗?

标签: python opencv computer-vision webcam


【解决方案1】:

在代码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

我怀疑您的剪辑代码应该检查 x2 是否大于 图像 宽度,而不是面部宽度。

你在裁剪 y2 时遇到了类似的问题

【讨论】:

  • 我必须注释掉剪裁部分下方的方程式才能打印。值是 - x1: 228, y1: 0, x2: 274, y2: 166 x: 255, y: 177, w: 255, h: 255
  • 你说The values of hatWidth and hatHeight are negative你在哪里打印这些以确定它们是负面的?
  • 我尝试在重新计算的行之后打印出值(hatWidth = x2- x1 和 hatHeight = y2 -y1),在几次迭代后,hatWidth 变为负数。帽子宽度:-29,帽子高度:195
  • 我将裁剪限制设置为 640 和 360,矩形在头部上方。谢谢你。调整大小后,我仍然遇到 ROI 区域的问题。我会发布另一个问题。再次感谢。
【解决方案2】:

我遇到了同样的错误。通过绘制坐标值来找到解决方案。其中一个点位于图像尺寸之外。所以出错了。我稍后修复它并设置边界条件,以使 ROI 尺寸不超过图像尺寸。不管怎么说,还是要谢谢你。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-06-16
    • 2018-11-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-28
    • 2012-06-12
    相关资源
    最近更新 更多