69

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() )
herohuyongtao
  • 49,413
  • 29
  • 133
  • 174
torayeff
  • 9,296
  • 19
  • 69
  • 103

2 Answers2

167

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.

Abhijat Biswas
  • 174
  • 1
  • 14
herohuyongtao
  • 49,413
  • 29
  • 133
  • 174
  • 1
    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? – user961627 May 18 '14 at 13:22
  • 4
    and what does "minNeighbors" refer to? is it about pruning excessive detections around the same face? – user961627 May 18 '14 at 13:23
  • 3
    @user961627 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. – herohuyongtao May 18 '14 at 15:50
  • Thanks, the explanation really helps. Do you know of any python implementation of the pruning of excessive detections? – user961627 May 18 '14 at 20:54
  • 1
    @user961627 There is also the Python API of OpenCV, see [here](http://docs.opencv.org/modules/objdetect/doc/cascade_classification.html#cv2.CascadeClassifier.detectMultiScale). – herohuyongtao May 19 '14 at 03:28
  • Is this *grouprectangles* pruning function? http://docs.opencv.org/modules/objdetect/doc/cascade_classification.html#grouprectangles – user961627 May 21 '14 at 11:26
  • 1
    Yes, you can use it as the pruning function or use `minNeighbors` parameter of `detectMultiScale()`. – herohuyongtao May 21 '14 at 12:07
  • 1
    `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 xml – Micka Jan 13 '15 at 13:51
  • @Micka Incorporate it to the answer. Thanks to make it clear. – herohuyongtao Jan 13 '15 at 14:29
  • 4
    @Micka 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. – Iwillnotexist Idonotexist Jan 13 '15 at 14:37
  • 2
    @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 :) – Micka Jan 13 '15 at 16:09
  • 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: http://docs.opencv.org/master/d7/d8b/tutorial_py_face_detection.html#gsc.tab=0 – MrE Jan 07 '16 at 23:48
  • @MrE Could you share your test image? – herohuyongtao Jan 08 '16 at 00:15
  • 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? – MrE Jan 08 '16 at 00:28
  • Tried with this http://www.download-free-wallpaper.com/img89/zjvvrfxqujoblxuzomsi.jpg to try with a static image, and no luck I'm using openCV 2.4.12 – MrE Jan 08 '16 at 00:35
  • @MrE 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`? – herohuyongtao Jan 08 '16 at 01:20
  • 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 needed – MrE Jan 08 '16 at 01:30
  • if i use minSize=(30,30) it runs but detects nothing – MrE Jan 08 '16 at 01:32
  • @MrE 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. – herohuyongtao Jan 08 '16 at 01:34
  • 1
    Thanks.. I found the problem: I had find and to give it the absolute path to the model file. – MrE Jan 08 '16 at 05:23
  • 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. – Chungzuwalla Jul 18 '18 at 04:32
-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

The Beast
  • 1,629
  • 2
  • 29
  • 42