我想将眼镜PNG图像添加到另一幅图像上,但是我得到的白色背景的PNG图像隐藏了脸部。
我需要知道我做错了什么。
原始人脸图像
原始眼镜图片
预期结果
import cv2
import numpy
img = cv2.imread("barack-obama.jpg")
lunette = cv2.imread("lunette.png", cv2.IMREAD_UNCHANGED)
eye_cascade = cv2.CascadeClassifier('haarcascade_eye.xml')
face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
eyes = eye_cascade.detectMultiScale(img, scaleFactor = 1.1, minNeighbors = 5)
r = 500.0 / img.shape[1]
dim = (500, int(img.shape[0] * r))
resized = cv2.resize(img, dim, interpolation = cv2.INTER_AREA)
grey = cv2.cvtColor(resized, cv2.COLOR_BGR2GRAY)
faces = face_cascade.detectMultiScale(grey, 1.3, 5)
for (x,y,w,h) in faces:
roi_grey = grey[y:y+h, x:x+w]
roi_color = resized[y:y + h, x:x + w]
eyes = eye_cascade.detectMultiScale(roi_grey)
for (ex,ey,ew,eh) in eyes:
cv2.rectangle(roi_color,(ex,ey),(ex+ew,ey+eh),(0,255,0),2)
lunette = cv2.resize(lunette , (w,h))
w, h, c = lunette.shape
for i in range(0, w):
for j in range(0, h):
if lunette[i, j][0] != 0:
resized[y + i, x + j] = lunette[i, j][1]
#help please
cv2.imshow('img',resized)
cv2.waitKey(0)