如何使面部以外的一切变得透明

时间:2018-06-17 09:32:19

标签: python opencv image-processing

我正在尝试使用opencv从图像中提取面部。最初我将图像转换为灰度,如此

gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

然后我使用dlib为我生成面部点,然后将其转换为numpy数组并使用fillconvexpoly方法将面部转换为面部多边形

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

我尝试使用

初始化feature_mask
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)

如何调整代码以获得预期的输出

以下是示例输入enter image description here

这是我得到的输出

enter image description here

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

1 个答案:

答案 0 :(得分:3)

您需要一个4频道BGRARGBA图片才能拥有透明度。我稍微调整了你的代码以适应这个。

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)

image