我正在尝试使用opencv创建一个与我脸上的玻璃重叠的应用程序。但是,当视频出现时,眼镜在alpha层上有黑色。这是我的代码:
video_capture = cv2.VideoCapture(0)
anterior = 0
glasses = cv2.imread('Glasses_1.png')
def put_glasses(glasses,fc,x,y,w,h):
face_width = w
face_height = h
glasses_width = int(face_width)
glasses_height = int(face_height*0.32857)
glasses = cv2.resize(glasses,(glasses_width,glasses_height))
for i in range(glasses_height):
for j in range(glasses_width):
for k in range(3):
if glasses[i][j][k]<235:
fc[y+i-int(-0.25*face_height)-1][x+j][k] = glasses[i][j][k]
return fc
while True:
if not video_capture.isOpened():
print('Unable to load camera.')
sleep(5)
pass
ret, frame = video_capture.read()
if ret is True:
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
else:
continue
faces = faceCascade.detectMultiScale(
gray,
scaleFactor=1.1,
minNeighbors=5,
minSize=(40,40)
)
for (x, y, w, h) in faces:
cv2.rectangle(frame, (x, y), (x+w, y+h), (0, 255, 0), 2)
cv2.putText(frame,"Person Detected",(x,y),cv2.FONT_HERSHEY_SIMPLEX,1,(0,0,255),2)
frame = put_glasses(glasses, frame, x, y, w, h)
如果有人能提供帮助,我将非常感激。
答案 0 :(得分:1)
您以bgr
格式读取了png,而不是bgra
或未更改的格式。然后,我不认为程序中的玻璃图像是(h,w,4)
的形状。您应该阅读国旗cv2.IMREAD_UNCHANGED
。
glasses = cv2.imread("xxx.png", cv2.IMREAD_UNCHANGED)
也许这个链接会有所帮助。 How do I clear a white background in OpenCV with c++?
bgra worm:
混合: