gpt4 book ai didi

python - OpenCV 错误 : Assertion failed (mask. size == src1.size) in binary_op

转载 作者:太空宇宙 更新时间:2023-11-03 21:35:42 25 4
gpt4 key购买 nike

我正在尝试将帽子的 png 图像图像放在网络摄像头的头上喂养。

import cv2
import numpy as np

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

# Load the overlay image: hat.png
imghat = cv2.imread('hat2.png', -1)

print imghat is None

# 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:
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)

hatWidth = w
hatHeight = hatWidth * origHatHeight / origHatWidth

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

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

#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 > 640:
x2 = w
if y2 > 360:
y2 = h


print "x1: %d , y1 : %d, x2: %d, y2: %d " %(x1,y1,x2,y2)
# Re-calculate the width and height of the hat image
hatWidth = x2 - x1
hatHeight = y2 - y1

cv2.rectangle(frame, (x1,y1), (x2, y2), (255,255,0), 2)
print "hatWidth: %d, hatHeight: %d" %(hatWidth, hatHeight)
# 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

# When everything is done, release the capture
video_capture.release()
cv2.destroyAllWindows()

当我执行程序时,相机突然打开和关闭。显示的错误在下面一行 -

roi_bg = cv2.bitwise_and(roi, roi, mask = mask_inv)

最佳答案

错误是

Assertion failed (mask.size == src1.size)

即蒙版图像的大小与您正在执行按位运算的源图像的大小不匹配_and on

源图像是您的投资返回率

roi = roi_color[ y1:y2, x1:x2 ]

看起来像 y2-y1 x x2-x1

掩码大小为

mask = cv2.resize(orig_mask, (hatWidth,hatHeight), interpolation = cv2.INTER_AREA)

看起来像 hatWidth x hatHeight(x2-x1) x (y2-y1)除非括号做了一些事情来颠倒宽度和高度的顺序——我不是 Python 大师,但这似乎不太可能。

关于python - OpenCV 错误 : Assertion failed (mask. size == src1.size) in binary_op,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30117740/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com