首页 > 解决方案 > 如何标记 OpenCV 中两个数组中都存在的特征?

问题描述

我正在尝试编写一个 Python 3.7 脚本来使用 OpenCV Haar 分类器文件检测人脸和特征,该文件将图像存储为 n 维 Numpy 数组。现在我只处理两个特征: - 整个脸 - 眼睛 这两个都是使用两个不同的分类器获得的。代码检测图像中这两个特征的存在,然后使用 cv2.rectangle() 函数在每个特征的 for 循环内用一个矩形标记它们,即一个用于检测到的面部,一个用于检测到的眼睛。

我希望脚本仅在面部阵列和眼睛阵列中找到这些点时才标记眼睛矩形。numpy.intersect1d() 仅在一维数组中查找交集。

我什至试过

for x,y,w,h in eyes and x,y,w,h in faces:

它所做的只是返回此错误消息:

ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

任何帮助将不胜感激。

这是在 Windows 10 64 位上尝试的,代码是用 Pycharm 2019 编写的,OpenCV 被导入为 CV2。

# Creating the cascade classifier objects.
face_cascade = 
cv2.CascadeClassifier("haarcascade_frontalface_default.xml")
eye_cascade = cv2.CascadeClassifier("haarcascade_eye.xml")

# Entering and displaying the original image.
img = cv2.imread("filename.jpg", 1)
cv2.imshow("Original Images",img)
cv2.waitKey(0)
cv2.destroyAllWindows()

# Convert the image to Black and White and store in a variable.
gry_img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

faces = face_cascade.detectMultiScale(gry_img, scaleFactor = 1.05, minNeighbors = 8)
eyes = eye_cascade.detectMultiScale(gry_img, scaleFactor = 1.30, minNeighbors = 12)

# Now, we mark the detected features with a rectangle on the original image.
for x,y,w,h in faces:
    img = cv2.rectangle(img, (x,y), (x+w, y+h), (255, 21, 21), 3) # Blue.


for x,y,w,h in eyes:
    img = cv2.rectangle(img, (x,y), (x+w, y+h), (255, 255, 24), 3) # Cyan.

cv2.imshow("Detected Faces and Eyes",img)
cv2.waitKey(0)
cv2.destroyAllWindows()

只有在面部​​特征数组中也找到眼睛特征时,我才需要标记眼睛特征。

标签: pythonpython-3.xnumpyopencvhaar-classifier

解决方案


OpenCV 文档中已经提出了解决此问题的更好方法。它建议不要通过整个图像来检测眼睛,而应该首先检测面部,然后裁剪图像,并使用这个裁剪后的图像进行眼睛检测。要裁剪面部图像,您可以使用 numpy 切片:

for x,y,w,h in faces:
    face_img = gry_img[y:y+h, x:x+w]
    # Now detect the eyes in this `face_img` only.
    eyes = eye_cascade.detectMultiScale(face_img, scaleFactor = 1.30, minNeighbors = 12)

推荐阅读