Python 如何使除人脸以外的所有内容透明

Python 如何使除人脸以外的所有内容透明,python,opencv,image-processing,Python,Opencv,Image Processing,我正在尝试使用opencv从图像中提取人脸。最初我将图像转换为灰度,就像这样 gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) 然后我使用dlib为我生成面部点,然后将其转换为numpy数组,并使用FILLCONVERXPOLY方法获得面部多边形内的面部 #detect facial landmarks shape = predictor(gray, rect) #convert facial landmarks to numpy

我正在尝试使用opencv从图像中提取人脸。最初我将图像转换为灰度,就像这样

gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
然后我使用dlib为我生成面部点,然后将其转换为numpy数组,并使用FILLCONVERXPOLY方法获得面部多边形内的面部

   #detect facial landmarks
   shape = predictor(gray, rect)
   #convert facial landmarks to numpy array
   shape = face_utils.shape_to_np(shape)



   #initialize new array layout as shape
   remapped_shape = np.zeros_like(shape)
   xmin, ymin = shape.min(axis=0)
   xmax, ymax = shape.max(axis=0)

   feature_mask=np.zeros((image.shape[0],image.shape[1],3),np.uint8)
   feature_mask[:]=(0,0,0)
   remapped_shape = face_remap(shape)

   cv2.fillConvexPoly(feature_mask, remapped_shape[0:27], [255, 255, 255])
   feature_mask = feature_mask.astype(np.bool)
   out_face[feature_mask] = image[feature_mask]
   cv2.imwrite("out_face.png", out_face)
我尝试使用初始化功能\u掩码

np.zeros((image.shape[0],image.shape[1],4),np.uint8)
但这给了我一个错误

ValueError: could not broadcast input array from shape (3) into shape (500,500,4)
如何调整代码以获得预期的输出

这是示例输入

这是我得到的输出


我希望背景是透明的,而不是黑色的

您需要一个4通道的
BGRA
RGBA
图像才具有透明度。我对你的代码做了一些调整,以适应这一点

feature_mask=np.zeros((image.shape[0],image.shape[1]),np.uint8)
remapped_shape = face_remap(shape)

cv2.fillConvexPoly(feature_mask, remapped_shape, [255])
out_face = cv2.bitwise_and(image, image, mask=feature_mask)

(x,y,w,h) = cv2.boundingRect(remapped_shape)
alpha = np.zeros((h,w), dtype=np.uint8)
feature_mask = feature_mask[y:y+h,x:x+w]
out_face = out_face[y:y+h,x:x+w]
alpha[feature_mask == 255] = 255

mv = []
mv.append(out_face)
mv.append(alpha)

out_face = cv2.merge(mv)

我已经找到了imagemagick去除黑色背景的方法,但我想知道如何调整我的代码以实现相同的BlendId和简明+1!!