OpenCV中detectMultiScale的参数使用Python
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/36218385/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Parameters of detectMultiScale in OpenCV using Python
提问by vss
I am not able to understand the parameters passed to detectMultiScale. I know that the general syntax is detectMultiScale(image, rejectLevels, levelWeights) However, what do the parameters rejectLevels and levelWeights mean? And what are the optimal values used for detecting objects?
我无法理解传递给detectMultiScale的参数。我知道一般语法是 detectMultiScale(image, rejectLevels, levelWeights) 但是,参数 rejectLevels 和 levelWeights 是什么意思?用于检测物体的最佳值是多少?
I want to use this to detect pupil of the eye
我想用它来检测眼睛的瞳孔
回答by tfv
A code example can be found here: http://docs.opencv.org/3.1.0/d7/d8b/tutorial_py_face_detection.html#gsc.tab=0
可以在此处找到代码示例:http: //docs.opencv.org/3.1.0/d7/d8b/tutorial_py_face_detection.html#gsc.tab=0
Regarding the parameter descriptions, you may have quoted old parameter definitions, in fact you may be faced with the following parameters:
关于参数描述,您可能引用了旧的参数定义,实际上您可能会遇到以下参数:
- scaleFactor: Parameter specifying how much the image size is reduced at each image scale.
- minNeighbors: Parameter specifying how many neighbors each candidate rectangle should have to retain it
- scaleFactor:指定在每个图像比例下图像尺寸减小多少的参数。
- minNeighbors:参数指定每个候选矩形应该有多少个邻居来保留它
Here you can find a nice explanation on these parameters: http://www.bogotobogo.com/python/OpenCV_Python/python_opencv3_Image_Object_Detection_Face_Detection_Haar_Cascade_Classifiers.php
在这里你可以找到关于这些参数的很好的解释:http: //www.bogotobogo.com/python/OpenCV_Python/python_opencv3_Image_Object_Detection_Face_Detection_Haar_Cascade_Classifiers.php
Make sure to obtain proper pretrained classifier sets for faces and eyes such as
确保为面部和眼睛获得适当的预训练分类器集,例如
- haarcascade_frontalface_default.xml
- haarcascade_eye.xml
- haarcascade_frontalface_default.xml
- haarcascade_eye.xml
回答by Rajshah
Amongst these parameters, you need to pay more attention to four of them:
在这些参数中,你需要更加关注其中的四个:
scaleFactor
– Parameter specifying how much the image size is reduced at each image scale.
scaleFactor
– 指定图像大小在每个图像比例下缩小多少的参数。
Basically, the scale factor is used to create your scale pyramid. More explanation, your model has a fixed size defined during training, which is visible in the XML. This means that this size of the face is detected in the image if present. However, by rescaling the input image, you can resize a larger face to a smaller one, making it detectable by the algorithm.
基本上,比例因子用于创建您的比例金字塔。更多解释,您的模型在训练期间定义了固定大小,这在 XML 中可见。这意味着在图像中检测到此大小的人脸(如果存在)。但是,通过重新缩放输入图像,您可以将较大的人脸调整为较小的人脸,使其可被算法检测到。
1.05 is a good possible value for this, which means you use a small step for resizing, i.e. reduce the size by 5%, you increase the chance of a matching size with the model for detection is found. This also means that the algorithm works slower since it is more thorough. You may increase it to as much as 1.4 for faster detection, with the risk of missing some faces altogether.
1.05 是一个很好的可能值,这意味着您使用一小步来调整大小,即将大小减小 5%,您增加了找到与模型匹配的大小的机会以进行检测。这也意味着该算法的工作速度较慢,因为它更彻底。您可以将其增加到 1.4 以加快检测速度,但可能会完全遗漏某些人脸。
minNeighbors
– Parameter specifying how many neighbors each candidate rectangle should have to retain it.
minNeighbors
– 指定每个候选矩形应保留多少邻居的参数。
This parameter will affect the quality of the detected faces. Higher value results in fewer detections but with higher quality. 3~6 is a good value for it.
该参数会影响检测到的人脸的质量。值越高,检测次数越少,但质量越高。3~6 是一个很好的价值。
minSize
– Minimum possible object size. Objects smaller than that are ignored.
minSize
– 最小可能的对象大小。小于该值的对象将被忽略。
This parameter determines how small size you want to detect. You decide it! Usually, [30, 30] is a good start for face detection.
这个参数决定了你想要检测的尺寸有多小。你来决定!通常,[30, 30] 是人脸检测的良好开端。
maxSize
– Maximum possible object size. Objects bigger than this are ignored.
maxSize
– 最大可能的对象大小。大于此值的对象将被忽略。
This parameter determines how big size you want to detect. Again, you decide it! Usually, you don't need to set it manually, the default value assumes you want to detect without an upper limit on the size of the face.
这个参数决定了你想检测多大的尺寸。再次,你决定!通常,您不需要手动设置它,默认值假设您要检测的人脸大小没有上限。
回答by Gino Mempin
The OpenCV Class List docsprovides the descriptions for all C++ and Python method.
的OpenCV的类列表的文档提供了所有的C ++和Python方法的描述。
Here is the one for cv::CascadeClassifier detectMultiScale:
这是cv::CascadeClassifier detectMultiScale 的一个:
detectMultiScale
Python:
objects = cv.CascadeClassifier.detectMultiScale(image[, scaleFactor[, minNeighbors[, flags[, minSize[, maxSize]]]]]
Parameters:
image Matrix of the type CV_8U containing an image where objects are detected. objects Vector of rectangles where each rectangle contains the detected object, the rectangles may be partially outside the original image. scaleFactor Parameter specifying how much the image size is reduced at each image scale. minNeighbors Parameter specifying how many neighbors each candidate rectangle should have to retain it. flags Parameter with the same meaning for an old cascade as in the function cvHaarDetectObjects. It is not used for a new cascade. minSize Minimum possible object size. Objects smaller than that are ignored. maxSize Maximum possible object size. Objects larger than that are ignored. If maxSize == minSize model is evaluated on single scale.
Note
- (Python) A face detection example using cascade classifiers can be found at opencv_source_code/samples/python/facedetect.py
检测多尺度
蟒蛇:
objects = cv.CascadeClassifier.detectMultiScale(image[, scaleFactor[, minNeighbors[, flags[, minSize[, maxSize]]]]]
参数:
image Matrix of the type CV_8U containing an image where objects are detected. objects Vector of rectangles where each rectangle contains the detected object, the rectangles may be partially outside the original image. scaleFactor Parameter specifying how much the image size is reduced at each image scale. minNeighbors Parameter specifying how many neighbors each candidate rectangle should have to retain it. flags Parameter with the same meaning for an old cascade as in the function cvHaarDetectObjects. It is not used for a new cascade. minSize Minimum possible object size. Objects smaller than that are ignored. maxSize Maximum possible object size. Objects larger than that are ignored. If maxSize == minSize model is evaluated on single scale.
笔记
- (Python) 可以在 opencv_source_code/samples/python/facedetect.py 找到使用级联分类器的人脸检测示例
As noted, a sample usage is available from the OpenCV source code. You can pass in each documented parameter as a keyword.
如前所述,OpenCV 源代码中提供了一个示例用法。您可以将每个记录的参数作为关键字传入。
rects = cascade.detectMultiScale(img,
scaleFactor=1.3,
minNeighbors=4,
minSize=(30, 30),
flags=cv.CASCADE_SCALE_IMAGE)