简体   繁体   中英

Parameters of detectMultiScale in OpenCV using Python

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?

I want to use this to detect pupil of the eye

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.

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.

1.05 is a good possible value for this, which means you use a small step for resizing, ie 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.

  • minNeighbors – Parameter specifying how many neighbors each candidate rectangle should have to retain it.

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.

  • minSize – Minimum possible object size. Objects smaller than that are ignored.

This parameter determines how small size you want to detect. You decide it! Usually, [30, 30] is a good start for face detection.

  • maxSize – Maximum possible object size. Objects bigger than this are ignored.

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.

A code example can be found here: 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

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

Make sure to obtain proper pretrained classifier sets for faces and eyes such as

  • haarcascade_frontalface_default.xml
  • haarcascade_eye.xml

The OpenCV Class List docs provides the descriptions for all C++ and Python method.

Here is the one for cv::CascadeClassifier 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

As noted, a sample usage is available from the OpenCV source code . You can pass in each documented parameter as a keyword.

rects = cascade.detectMultiScale(img, 
                                 scaleFactor=1.3, 
                                 minNeighbors=4, 
                                 minSize=(30, 30),
                                 flags=cv.CASCADE_SCALE_IMAGE)

detectMultiScale function is used to detect the faces. This function will return a rectangle with coordinates(x,y,w,h) around the detected face.

It takes 3 common arguments — the input image, scaleFactor, and minNeighbours.

scaleFactor specifies how much the image size is reduced with each scale. In a group photo, there may be some faces which are near the camera than others. Naturally, such faces would appear more prominent than the ones behind. This factor compensates for that.

minNeighbours specifies how many neighbours each candidate rectangle should have to retain it. You can read about it in detail here. You may have to tweak these values to get the best results. This parameter specifies the number of neighbours a rectangle should have to be called a face.

We obtain these values after trail and test over a specific range.

import cv2 video = cv2.VideoCapture(0) cascade = cv2.CascadeClassifier("haarcascade_frontalface_default.xml") while(True): check,frame = video.read() print(check) gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) # face = cascade.detectMultiScale(gray, scaleFactor = 1.1, minNeighbors = 5) face = cascade.detectMultiScale(gray, scaleFactor=1.3, minNeighbors=4, minSize=(30, 30)) print(type(face)) for x,y,w,h in face: frame = cv2.rectangle(frame, (x,y), (x+w,y+h), (0,255,255), 3) cv2.imshow('Truniced TM',frame) key = cv2.waitKey(1) if ( key== ord('q')): break vid.release() cv2.destroyAllWindows() What's wrong with it?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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