【发布时间】:2018-01-02 21:59:36
【问题描述】:
我正在使用 OpenCV 和 Python。我正在检测人脸图像中的人脸,我将注意力集中在图像中人脸的区域,并创建了一个蒙版(零),我想在该区域用白色(或一般的任何颜色)填充的脸。我的源代码如下:
import cv2
import numpy as np
import matplotlib.pyplot as plt
img = cv2.imread('ManWithGlasses.jpg')
RGB_img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
# Detect the face in the image
haar_face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
faces = haar_face_cascade.detectMultiScale(gray_img, scaleFactor=1.1, minNeighbors=8);
mask_face = np.zeros(RGB_img.shape[:2], np.uint8)
# Loop in all detected faces - in our case it is only one
for (x,y,w,h) in faces:
cv2.rectangle(RGB_img,(x,y),(x+w,y+h),(255,0,0), 2)
plt.imshow(RGB_img)
plt.show()
roi_rgb = img[y:y + h, x:x + w]
mask_face[y:y + h, x:x + w] = [255, 255, 255]
但是我收到以下错误:
mask_face[y:y + h, x:x + w] = [255, 255, 255]
ValueError: could not broadcast input array from shape (3) into shape (462,462)
如何将图像的这个区域设置为我想要的任何颜色?
【问题讨论】: