Recommended values for OpenCV detectMultiScale() parameters
Asked Answered
K

2

73

What are the recommended parameters for CascadeClassifier::detectMultiScale() and depending on which factors I should change default parameters?

void CascadeClassifier::detectMultiScale(
    const Mat& image, 
    vector<Rect>& objects, 
    double scaleFactor=1.1,
    int minNeighbors=3, 
    int flags=0, 
    Size minSize=Size(),
    Size maxSize=Size() )
Kirven answered 27/12, 2013 at 12:48 Comment(0)
S
171

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 can be found here. In short, as described here, your model has a fixed size defined during training, which is visible in the xml. This means that this size of 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, i.e. reduce 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 less 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 determine 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 determine 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.

Sarajane answered 27/12, 2013 at 17:36 Comment(21)
are image pyramids used in combination with sliding window techniques? Isn't it true that if we're using a sliding window that scans the image at different scales and different sizes, then we don't need to use image pyramids?Abash
and what does "minNeighbors" refer to? is it about pruning excessive detections around the same face?Abash
@Abash Yes, image pyramids and sliding window techniques are combined being used. If you use a sliding window that scans the image at different scales and different sizes, you don't need to use image pyramids anymore as image pyramids just up/down-samples images when processing. For minNeighbors, yes, it is about pruning excessive detections around the same face. Please refer to the answer for the description.Sarajane
Thanks, the explanation really helps. Do you know of any python implementation of the pruning of excessive detections?Abash
@Abash There is also the Python API of OpenCV, see here.Sarajane
Is this grouprectangles pruning function? docs.opencv.org/modules/objdetect/doc/…Abash
Yes, you can use it as the pruning function or use minNeighbors parameter of detectMultiScale().Sarajane
scaleFactor depends on the size of your trained detector. e.g. if your detector windows has a training size of 20x20 and you want to detect for 21x21, 22x22 etc (each pixel size without skipping something) too, you have to choose scaleFactor = 1.05. If your trained detector is 10x10 and you want to detect for 11x11, 12x12 and so on (each pixel size), you can choose scaleFactor = 1.10 for haarcascade, your detector size is visible in the xmlAccommodative
@Accommodative Incorporate it to the answer. Thanks to make it clear.Sarajane
@Accommodative Don't 100% agree on scaleFactor. In fact, you want it as high as possible while still getting "good" results, and this must be determined somewhat empirically. It's heavily dependent on the target to be detected, the type of cascade and the training; Even and a value as high as 1.1 for a 24x24 FD cascade has worked for me in the past. A too low value for either scaleFactor or minSize will result in huge computational costs because many more pyramid layers need to be generated. A factor of 1.05 requires roughly double the # of layers (and >2x the time) than 1.1 does.Amianthus
@IwillnotexistIdonotexist that's obviously true, sorry I didn't mention it. I just wanted to say that it is possible to let the detector choose window sizes to someones exact needs and that scaleFactor can be computed to fulfill those needs. But it is hard to recommend a value without knowing the size of the trained window and the min/max size in combination with the constraints to computational time and detection sensitivity/specifity :)Accommodative
Playing around with this and I see 0 detections... How would I go about troubleshooting? What size image is recommended (using 1080p video here, does it matter) ? I tried scale factor from 1.005, and still no result. any hint? I followed this: docs.opencv.org/master/d7/d8b/…Sear
@Sear Could you share your test image?Sarajane
just using my webcam on laptop, so it's just me on white background. is there a requirement for size of subject with regard to the size of the picture? what ratio is typical?Sear
Tried with this download-free-wallpaper.com/img89/zjvvrfxqujoblxuzomsi.jpg to try with a static image, and no luck I'm using openCV 2.4.12Sear
@Sear I can detect 3 faces for the image you provided using scaleFactor=1.05, minNeighbors=3, minSize=30. Are you sure you have successfully loaded the haarcascades model files, e.g. haarcascade_frontalface_alt.xml or haarcascade_profileface.xml?Sarajane
I have no error in creating the cascade detector with face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml') but I just see there are 2 method signatures in the Python binding: Python: cv2.CascadeClassifier.detectMultiScale(image[, scaleFactor[, minNeighbors[, flags[, minSize[, maxSize]]]]]) → objects Python: cv2.CascadeClassifier.detectMultiScale(image, rejectLevels, levelWeights[, scaleFactor[, minNeighbors[, flags[, minSize[, maxSize[, outputRejectLevels]]]]]]) → objects and when using minSize=30 as you show, it complains that rejectLevels is neededSear
if i use minSize=(30,30) it runs but detects nothingSear
@Sear I am afraid that I cannot help you in this case as I have no experience using the Python API (C++ APIs instead). I suggest you open a new question for your specific case. Good luck.Sarajane
Thanks.. I found the problem: I had find and to give it the absolute path to the model file.Sear
Note that cascades based on Haar-like features (like the ones mentioned above) should not be using image pyramids -- if they do the implementation is broken. The beauty of Haar-like feature cascades is that they can be evaluated at any size on a single fixed-resolution image in constant time, giving high performance. In this case the scaleFactor is used to scale the detection window up, not scale the input image down.Pompei
L
-6

If you have a good CPU and RAM performance or more you can set scaleFactor=1 minNeighbors=3

if you're working in an embedded system like in raspberry i recommand to choose smth like scaleFactor= 2, (Higher values means less accuracy) minNeighbors = 1, (Higher values means less accuracy but more reliability) the algorithm will run much faster otherwise it will freeze if the CPU performance and RAM are not enough .

hope it helps

Lunar answered 26/7, 2015 at 11:25 Comment(1)
ScaleFactor should always be greater than 1, something like 1.05 for good CPU and RAM.Contrived

© 2022 - 2024 — McMap. All rights reserved.