我试图创建一个与opencv重叠在我脸上的玻璃杯的应用程序。然而,当视频出现,眼镜有一个黑色的阿尔法层。这是我的代码:
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)如果有人能帮忙,我会非常感激的。
发布于 2017-12-13 02:35:05
您以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蠕虫

调合:

https://stackoverflow.com/questions/47783371
复制相似问题