首页 > 解决方案 > 使用 OpenCV 合并重叠矩形

问题描述

我同时使用 OpenCV 的两个 Haar 级联算法(正面和侧面)来改进人脸检测。

不幸的是,检测无法正常工作,我不知道如何修复它。返回值为 2(在一张有 5 个面孔的图片上,通常检测到)并且所有矩形都消失了。

这是预期的结果(没有重叠的矩形):

图片

如果您想进行自己的测试,这是原始图片(也是 result.jpg)。

这是代码:

import cv2
import numpy as np

image=cv2.imread("/home/pi/Downloads/test.jpg")
face_cascade=cv2.CascadeClassifier("/home/pi/opencv-3.4.0/data/haarcascades/haarcascade_frontalface_alt.xml")
profil_cascade=cv2.CascadeClassifier("/home/pi/opencv-3.4.0/data/haarcascades/haarcascade_profileface_alt.xml")

gray=cv2.cvtColor(image,cv2.COLOR_BGR2GRAY)
face=face_cascade.detectMultiScale(gray, 1.06, 5)
profil=profil_cascade.detectMultiScale(gray, 1.1, 5)

combined_array=np.append(face, profil, axis=0)
combined_list=combined_array.tolist()
result=cv2.groupRectangles(combined_list,2)

print("I've found "+str(len(result))+ " face(s)")

for (x,y,w,h) in result[0]:
    cv2.rectangle(image,(x,y),(x+w,y+h),(0,0,255),2)

cv2.imwrite("/home/pi/Download/result.jpg", image)

标签: pythonopencvmachine-learningcomputer-vision

解决方案


非最大抑制算法用于解决检测结果重叠的问题。pyimagesearch有一篇非常好的文章和代码,可以让你朝着正确的方向前进。


推荐阅读