How to train OpenCV cascade classifier

OpenCV ships with an application that can be used to train a cascade classifier. The steps to prepare your data and train the classifier can be quite elaborate. I have detailed the steps that I used below to train the classifier to identify an object (say car):

  • Two OpenCV programs: opencv_createsamples and opencv_traincascade will be used for this process. They should be installed along with your OpenCV. You can also compile them while compiling OpenCV from source. Make sure to enable the option BUILD_APPS in the CMake GUI. This is the option that builds these applications.

  • Prepare images that have one or more instances of the object of interest. These will be used to generate positive samples later. Also, prepare images that do not have the object of interest. These will be used to generate negative samples later.

  • It is mandatory to use opencv_createsamples to generate the positive samples for opencv_traincascade. The output of this program is a .vec file that is used as input to opencv_traincascade.

  • If you have image patches of the object and need to generate positive sample images by applying transformations (rotations) on this in 3 dimensions and then superimposing it on a background, you can do that using opencv_createsamples. See its documentation for details.

  • In my case, I already had one or more instances of the object captured with its actual background. So, what I instead had to do was to indicate the patch rectangles in each image where my object was located. You can do this by hand or by writing a simple OpenCV program to display image to you and you mark out the rectangles on the objects and stores these values in a text file. The format of this text file required by opencv_createsamples can be seen in its documentation.

  • To create the positive samples file, I invoked opencv_createsamples as:

$ opencv_createsamples -info obj-rects.txt -w 50 -h 50 -vec pos-samples.vec

Here, obj-rects.txt is a text file that has the information of the rectangles where the object is located in each image. See step above for details. The output of this program is stored in pos-samples.vec.

  • To view the positive samples that have been created by this program:
$ opencv_createsamples -vec pos-samples.vec -w 50 -h 50

Note that it switches to viewing mode when you only provide these three parameters and their values should match what you provided to create the positive samples.

  • Now we can train the cascade classifier. The details of its parameters can be seen in its documentation. I used this invocation:
$ opencv_traincascade -data obj-classifier -vec pos-samples.vec -bg neg-filepaths.txt -precalcValBufSize 2048 -precalcIdxBufSize 2048 -numPos 200 -numNeg 2000 -nstages 20 -minhitrate 0.999 -maxfalsealarm 0.5 -w 50 -h 50 -nonsym -baseFormatSave

obj-classifier is a directory where we are asking the classifier files to be stored. Note that this directory should already be created by you. pos-samples.vec is the file we generated in step above. neg-filepaths.txt is a file with list of paths to negative sample files. 2048 is the amount of MB of memory we are requesting the program to use. The more the memory, the faster the training. 200 is the number of positive samples in pos-samples.vec. This number is also reported by opencv_createsamples when it finishes its execution. 2000 is the number of negative sample image paths we have specified in neg-filepaths.txt. 20 is the number of stages in the classifier we wish. 50x50 is the size of object in these images. This should be same as what was specified with opencv_createsamples. 0.999 and 0.5 are self-explanatory. Details on all these parameters are found in the documentation.

Related: See my tips and other posts on using OpenCV Cascade Classifier.

Tried with: OpenCV 2.4.9 and Ubuntu 14.04

2 thoughts on “How to train OpenCV cascade classifier

  1. hi sir, i m working on a hand gesture recognization project on the android studio platform based Local Binary Pattern. What kind of strategy shoul ı follow? Are there a differences between this good tutorial and what i m try to do? thank you sir


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.