对OpenCV人脸检测方法detectMultiScale参数“最直白”的理解——Python学习笔记(8)

最近学习图像处理,用到OpenCV的detectMultiScale方法检测人脸,但是对它的参数的含义不太理解,于是各种查资料和测试效果,下面把学习的过程和个人理解总结一下。

1、准备环境

我用的是python3.6 + PyCharm,并且安装了OpenCV及相关的库。

然后下载Haar级联的XML,放在代码工程目录下。

下载地址:https://download.csdn.net/download/leaf_zizi/12667450

再借用《浪姐》的一张人脸图片,像素300*200。

2、编写代码

import cv2

# 读取照片,转换成灰度图
img1 = cv2.imread("langjie.jpg")
gray_img = cv2.cvtColor(img1, cv2.COLOR_RGB2GRAY)

# 将级联算法加载到一个变量中
haar_face_cascade = cv2.CascadeClassifier(
    'haar\haarcascade_frontalface_alt.xml')

# 识别图像中的人脸,返回所有人脸的矩形框向量组
# scaleFactor 为了检测到不同大小的目标,通过scalefactor参数把图像长宽同时按照一定比例(默认1.1)逐步缩小,
# 然后检测,这个参数设置的越大,计算速度越快,但可能会错过了某个大小的人脸。
# minNeighbors 构成检测目标的相邻矩形的最小个数,默认值是3
faces = haar_face_cascade.detectMultiScale(gray_img, scaleFactor=1.1,
                                           minNeighbors=3)

# 在图像中画上矩形框
for (x, y, w, h) in faces:
    cv2.rectangle(img1, (x, y), (x + w, y + h), (0, 255, 0), 1)

# 显示结果
cv2.imshow("Final_detected_image", img1)
cv2.waitKey(0)

看一下识别的效果,发现有两个人脸没有识别到,作为一个“较真儿”的人,我需要研究一下detectMultiScale参数的含义。

3、detectMultiScale参数的含义

detectMultiScale(image[, scaleFactor[, minNeighbors[, flags[, minSize[, maxSize]]]]]) -> objects
@brief Detects objects of different sizes in the input image. The detected objects are returned as a list of rectangles.
此方法的任务是检测不同大小的对象,并返回矩形的列表。

@param image Matrix of the type CV_8U containing an image where objects are detected.
被检测的图片,需要转换为灰度图

@param scaleFactor Parameter specifying how much the image size is reduced at each image scale.
scaleFactor 是重点,直接翻译就是“指定每次图像缩小的比例”,后面再说我的理解。

@param minNeighbors Parameter specifying how many neighbors each candidate rectangle should have to retain it.
minNeighbors 也是重点,翻译为:指定每个候选矩形有多少个“邻居”,后面详说。

@param flags Parameter with the same meaning for an old cascade as in the function cvHaarDetectObjects. It is not used for a new cascade.
flag参数与旧的级联方法cvHaarDetectObjects中一样,新的级联中不用。(没用到这个参数)

@param minSize Minimum possible object size. Objects smaller than that are ignored.
@param maxSize Maximum possible object size. Objects larger than that are ignored. If `maxSize == minSize` model is evaluated on single scale.
minSize和maxSize 设置检测对象的最大最小值,低于minSize和高于maxSize的话就不会检测出来。

这里面最关键的两个参数就是scaleFactor和minNeighbors,我是通过一段英文解释来理解的,先来看一下:

Haar cascade classifier works with a sliding window approach. If you look at the cascade files you can see a size parameter which usually a pretty small value like 20 20. This is the smallest window that cascade can detect. So by applying a sliding window approach, you slide a window through out the picture than you resize it and search again until you can not resize it further. So with every iteration haar's cascaded classifier true outputs are stored. So when this window is slided in picture resized and slided again; it actually detects many many false positives. You can check what it detects by giving minNeighbors 0. 

So there are a lot of face detection because of resizing the sliding window and a lot of false positives too. So to eliminate false positives and get the proper face rectangle out of detections, neighborhood approach is applied. It is like if it is in neighborhood of other rectangles than it is ok, you can pass it further. So this number determines the how much neighborhood is required to pass it as a face rectangle. 

So by increasing this number you can eliminate false positives but be careful, by increasing it you can also lose true positives too.

我就不直接翻译了,说一下我的理解:

大概意思是Haar cascade的工作原理是一种“滑动窗口”的方法,通过在图像中不断的“滑动检测窗口”来匹配人脸。

因为图像的像素有大有小,图像中的人脸因为远近不同也会有大有小,所以需要通过scaleFactor参数设置一个缩小的比例,对图像进行逐步缩小来检测,这个参数设置的越大,计算速度越快,但可能会错过了某个大小的人脸。

其实可以根据图像的像素值来设置此参数,像素大缩小的速度就可以快一点,通常在1~1.5之间。

那么,经过多次的迭代,实际会检测出很多很多个人脸,这一点可以通过把minNeighbors 设为0来验证。

所以呢,minNeighbors参数的作用就来了,只有其“邻居”大于等于这个值的结果才认为是正确结果。

4、参数值测试

首先scaleFactor=1.1(默认值),分别设置minNeighbors=0,1,3(默认为3),看一下输出结果。

minNeighbors=0,每个人脸都识别出来了,特别的是孟佳(左3)被识别出2次,李斯(右二上)被识别出1次:

minNeighbors=1,李斯没识别出来,因为她的识别结果中没有“邻居”:

minNeighbors=3,孟佳也没有识别出来,因为她的识别结果中只有1个“邻居”:

 

此时,对minNeighbors是不是有了形象的认识了。

因为我的图像只有300*200像素,而每次缩小1.1倍,所以导致识别出的结果较少。下面我让scaleFactor=1.03,minNeighbors=0,1,3,你会发现,当scaleFactor=1.03时,每个人脸被识别的次数都比上一组测试要多,因为每次缩小的比例小,迭代的次数就多了。看一下输出结果:

scaleFactor=1.03,minNeighbors=0:

scaleFactor=1.03,minNeighbors=1:

scaleFactor=1.03,minNeighbors=3:


参考:

《Python机器学习和图像处理实战》

https://stackoverflow.com/questions/22249579/opencv-detectmultiscale-minneighbors-parameter

 

 

  • 64
    点赞
  • 168
    收藏
    觉得还不错? 一键收藏
  • 6
    评论
Python中使用OpenCV进行人脸检测方法可以按照以下步骤进行操作。首先,导入所需的库和模型。可以使用以下代码导入OpenCV库和人脸检测模型: import cv2 cascade = cv2.CascadeClassifier("path/to/haarcascade_frontalface_default.xml") 在代码中,将"path/to/haarcascade_frontalface_default.xml"替换为你自己安装的OpenCV人脸检测模型的路径。 接下来,读取视频流或图像并将其转换为灰度图像以方便处理。可以使用以下代码完成此步骤: video_capture = cv2.VideoCapture(0) # 如果要从网络摄像头读取视频流,将0更改为视频流的URL while True: ret, frame = video_capture.read() gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) 然后,使用模型进行人脸检测并标记出检测到的人脸。可以使用以下代码完成此步骤: faces = cascade.detectMultiScale(gray, scaleFactor=1.1, minNeighbors=5, minSize=(30, 30)) for (x, y, w, h) in faces: cv2.rectangle(frame, (x, y), (x+w, y+h), (0, 255, 0), 2) 最后,显示标记后的图像并等待用户按下"q"键退出程序。可以使用以下代码完成此步骤: cv2.imshow('Video', frame) if cv2.waitKey(1) & 0xFF == ord('q'): break 完成以上步骤后,释放视频捕获对象并销毁所有窗口。可以使用以下代码完成此步骤: video_capture.release() cv2.destroyAllWindows() 以上是使用PythonOpenCV进行人脸检测的基本步骤。可以根据需要进行调整和扩展,例如添加眼睛和嘴巴的检测功能,或使用Dlib库进行更高级的人脸相关任务。 <span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [python opencv人脸检测提取及保存方法](https://download.csdn.net/download/weixin_38552305/13769902)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* *3* [使用OpenCV实现人脸检测Python)](https://blog.csdn.net/weixin_42990464/article/details/120171635)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值