python - 使用 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)
解决方案
非最大抑制算法用于解决检测结果重叠的问题。pyimagesearch有一篇非常好的文章和代码,可以让你朝着正确的方向前进。
推荐阅读
- python - virtualenv 比 venv 快是什么意思?就在创作的时候?或者在运行应用程序和脚本时?
- python - 访问未分配给任何东西的类对象
- python - 正则表达式在最后一次出现字符后查找文本直到另一个字符
- java - 嵌套回收站视图,子级和父级删除问题
- javascript - Onclick 将 setAttribute 动态添加到 td 元素将变量作为字符串传递
- javascript - 如何使用大 json 文件(在 vue 项目中)
- android - 本机崩溃报告包含偏移地址
- typescript - 使用带有打字稿的 Vuex 4,类型“ComponentPublicInstance”上不存在属性“$store”
- postgresql - 获取函数在vacuumdb中不存在错误
- oracle - 将超链接添加到行并在另一个页面中显示特定行