简体   繁体   English

将重叠矩形与 OpenCV 合并

[英]Merging overlapping rectangle with OpenCV

I'm using two Haar cascade algorithms (frontal and profile) with OpenCV at the same time to improve the face detection.我同时使用两种 Haar 级联算法(正面和侧面)和 OpenCV 来改进人脸检测。

Unfortunately, the detection is not working correctly and I don't know how to fix it.不幸的是,检测无法正常工作,我不知道如何修复它。 The return value is 2 (on a picture with 5 faces, normally detected) and all the rectangles have disappeared.返回值是 2(在一张有 5 张人脸的图片上,正常检测到)并且所有的矩形都消失了。

This is the expected result (without the overlapping rectangles):这是预期的结果(没有重叠的矩形):

图片

This is the original picture (and also the result.jpg) if you want to make your own test.如果您想自己进行测试,这是原始图片(以及result.jpg)。

This is the code :这是代码:

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)

The Non Maximum Suppression algorithm is used to solve the problem of overlapping detection results.非最大抑制算法用于解决检测结果重叠的问题。 pyimagesearch has a very good article on it and code to get you going in the right direction. pyimagesearch有一篇关于它的非常好的文章和代码,可以让你朝着正确的方向前进。

After a lot of research, i've partially solved the problem .经过大量研究,我已经部分解决了这个问题

I've changed the Threshold and EPS of result=cv2.groupRectangles and I've also make a subtraction in print function between the number of total faces detected (in combined_list ) and the number of overlapping detection (return by result )我已经改变了阈值和的EPS result=cv2.groupRectangles和我也使在减法print功能之间测得的总的面部的数量(在combined_list)和重叠的检测次数(由结果返回)

Here's the new code :这是新代码:

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,1,0.85)

print("I've found "+str(len(combined_list)-str(len(result[1]))+ " 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)

The overlapping rectangles have disappear... but also the other rectangles !重叠的矩形消失了......还有其他矩形!

Finally, the program give me the correct number of face (5) and redraw overlapping rectangle (that's good news), but the non-overlapping rectangles have disappear...最后,程序给了我正确的面数 (5) 并重绘重叠矩形(这是个好消息),但非重叠矩形已经消失......

I've tried to solve this by using a np.subtract between the coordinate from combined_list and result , then draw the missing rectangle with for (x,y,w,h) in np.subtract[0] but it didn't work.我试着通过使用来解决这个np.subtract之间从combined_list结果的坐标,然后绘制矩形失踪与for (x,y,w,h) in np.subtract[0]但它没有工作. The reason is that the coordinate point of overlapping rectangle are directly recalculate, so i can't make the subtraction with the original coordinate points原因是重叠矩形的坐标点是直接重新计算的,所以不能和原来的坐标点做减法

If someone has a idea to solve this issue, don't hesitate :)如果有人有解决此问题的想法,请不要犹豫:)

The openCV function groupRectangles requires 3 inputs. openCV 函数 groupRectangles 需要 3 个输入。

1) rectList : Vector of rectangles 1) rectList : 矩形向量

2) groupthreshold : Minimum possible number of rectangles minus 1 2) groupthreshold :最小可能的矩形数减 1

3) eps : Relative difference between sides of the rectangles to merge them into a group 3) eps : 矩形边之间的相对差以将它们合并为一组

Your code:您的代码:

   result=cv2.groupRectangles(combined_list,1,0.85)

From your code you have set the groupthreshold parameter as 1 which rejects all clusters that has one rectangle.从您的代码中,您已将 groupthreshold 参数设置为 1,这将拒绝具有一个矩形的所有集群。 Set this parameter as 0 and you should get the result you wanted.将此参数设置为 0,您应该会得到您想要的结果。

Solution:解决方案:

   result=cv2.groupRectangles(combined_list,0,0.85)

Detailed explanation given below: ( https://docs.opencv.org/3.4/d5/d54/group__objdetect.html#ga3dba897ade8aa8227edda66508e16ab9 )详细解释如下:( https://docs.opencv.org/3.4/d5/d54/group__objdetect.html#ga3dba897ade8aa8227edda66508e16ab9

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM