Code Yarns ‍👨‍💻
Tech BlogPersonal Blog

Tips for using OpenCV Cascade Classifier

📅 2014-Sep-30 ⬩ ✍️ Ashwin Nanjappa ⬩ 📚 Archive

My earlier post describes the basic steps to train the OpenCV Cascade Classifier. Unfortunately, the training process of opencv_traincascade is not for the newbie. It may take days to finish and the classifier might not work as you expected. Here is a list of tips that I found useful to train the classifier.

Rotation invariance

The cascade classifier is not rotation invariant. If your images can have the object in different rotations, then you have two options:

  1. You can generate a training set where the objects in the positive samples are rotated at different angles. You have to generate these yourself, opencv_createsamples will not do this for .vec file output.

  2. You strictly use positive samples where the object is in an upright or canonical orientation. For example, positive samples of faces where the face is always upright. You may have to write a program with some user interaction to generate these from your training samples. Later during the testing stage, the cascade classifier will only detect the object, if it appears in canonical position (in which it was trained). So, to get it to detect the object, you need to repeatedly rotate the image and try the classifier.

Rotation angle

While the cascade classifier is rotation invariant (see above), this does not mean that the object needs to be perfectly in upright or canonical orientation. The classifier should be able to detect object up to about 10 degrees of rotation. What this means is that:

  1. If you are using rotated positive samples, you do not need to try very small rotations (say 5 degrees). Instead, a larger rotation should be fine. For example, rotations in increments of 20 degrees.

  2. If you are rotating the image during the testing stage, then again no need to try small rotations. Same advice as earlier point.


If training is extremely slow, then build OpenCV with Intel TBB support. This can be done by installing the libtbb-dev package and then building OpenCV with the WITH_TBB option enabled. With this feature enabled (along with LBP), I found that opencv_traincascade which used to use 1 core, started to use all the available cores. Again, this can provide another 8x jump in training speed. With a speedup like this, I highly recommend to use this feature for everyone who is using an Intel CPU.

LBP features

If training is extremely slow, possibly taking days, then consider using the -featureType LBP option. By default, HAAR features are computed, which can be very slow. Local Binary Patterns can be almost an order of magnitude faster to compute. Its testing performance can match HAAR if you provide a good training set with lots of positive samples.

Related: Enable multithreading with TBB during cascade