【问题标题】:OpenCV Error: Assertion failed (s >= 0) in setSize - while trying to place an image over a webcam feedOpenCV 错误:setSize 中的断言失败 (s >= 0) - 在尝试将图像放置在网络摄像头源上时
【发布时间】:2015-05-07 10:40:18
【问题描述】:

我正在尝试在网络摄像头提要中将帽子的图像覆盖在头上,但在以下行中出现错误 -

hat = cv2.resize(imghat, (hatWidth,hatHeight), interpolation = cv2.INTER_AREA)

程序运行,但很快退出并给出上述错误。代码段如下 -

# Create the mask for the hat
imghatGray = cv2.cvtColor(imghat, cv2.COLOR_BGR2GRAY)
#cv2.imwrite("imghatGray.png", imghatGray)

ret, orig_mask = cv2.threshold(imghatGray, 0, 255, cv2.THRESH_BINARY)
#cv2.imwrite("orig_mask.png", orig_mask)

# Create the inverted mask for the hat
orig_mask_inv = cv2.bitwise_not(orig_mask)
#cv2.imwrite("orig_mask_inv.png", orig_mask_inv)

# 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:
        cv2.rectangle(frame, (x,y), (x+w, y+h), (255,0,0), 2)

        roi_gray = gray[y:y+h, x:x+w]
        roi_color = frame[y:y+h, x:x+w]

        hatWidth = w
        hatHeight = hatWidth * orighatHeight / orighatWidth

        # Center the hat
        x1 = x -15
        x2 = x + hatWidth + 15
        y1 = y - hatHeight
        y2 = y + 15

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

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

        # 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

这是显示的错误消息 -

v2.error: /home/vinravun/opencv-2.4.10/modules/core/src/matrix.cpp:116: error: (-215) s >= 0 in function setSize

【问题讨论】:

  • 是你添加的那一行是错误产生的地方
  • 是的,这是显示的完整错误消息 - cv2.error: /home/vinravun/opencv-2.4.10/modules/core/src/matrix.cpp:116: error: (-215 ) s >= 0 在函数 setSize
  • 打印 hatWidth , hatHeight
  • 它们都是否定的。我已经更新了代码段,我不明白为什么它们是负面的。
  • x1,x2,y1,y2 是您自己计算的数字。只需调试他们的计算。

标签: python opencv computer-vision webcam webcam-capture


【解决方案1】:

没有足够的细节可以说明,但您尝试调整大小的图像之一似乎是空的。

你知道这个错误发生在哪一行调整大小上吗?

您应该在抓取网络摄像头图像后检查它是否有任何内容。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-10-23
    • 1970-01-01
    • 2017-10-24
    • 1970-01-01
    相关资源
    最近更新 更多