我同时使用两个 haarcascade 算法(frontal 和 profil)来改进人脸检测。
我发现我需要使用 cv2.GroupRectangles(rectList, groupThreshold)
但它不起作用。程序返回给我:
The true value of a array with more than one element is ambigious. Use a.any() or a.all()
如何将它集成到我的代码中?
import cv2
image=cv2.imread("/home/pi/Downloads/test.jpg")
face_cascade=cv2.CascadeClassifier("Path of first algorithm")
profil_cascade=cv2.CascadeClassifier("Path of second algorithm")
gray=cv2.cvtColor(image,cv2.COLOR_BGR2GRAY)
face=face_cascade.detectMultiScale(gray, 1.06, 5)
profil=profil_cascade.detectMultiScale(gray, 1.1, 5)
cv2.groupRectangles(face and profil, 2)
print("I've found "+str(len(face)+len(profil))+ " face(s)")
for (x,y,w,h) in face:
cv2.rectangle(image,(x,y),(x+w,y+h),(0,0,255),2)
for (x,y,w,h) in profil:
cv2.rectangle(image,(x,y),(x+w,y+h),(0,255,0),2)
cv2.imwrite("/home/pi/Download/result.jpg", image)
在此先感谢您,祝您有愉快的一天:)
最佳答案
cv2.groupRectangles()
需要一个 list
作为参数传递。
在你的例子中,face
和 profil
是 array
类型。所以当你做 和
操作时,它肯定会抛出提到的错误。这是修复:
按列追加两个数组:
combined_array = np.append(face, profil, axis = 0)
将此数组转换为列表:
combined_list = combined_array.tolist()
现在将此列表传递给 cv2.GroupRectangles()
:
result = cv2.groupRectangles(combined_list, groupThreshold)
检查 result
后,它实际上是一个包含两个值的 tuple
:
- 矩形坐标数组
- 矩形的权重(表示它是分组矩形的可能性)
关于opencv - 如何在 OpenCV 中合并重叠矩形? (Python),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51312199/