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_traincascadewill 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_APPSin 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_createsamplesto generate the positive samples for
opencv_traincascade. The output of this program is a
.vecfile that is used as input to
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_createsamplescan be seen in its documentation.
To create the positive samples file, I invoked
$ opencv_createsamples -info obj-rects.txt -w 50 -h 50 -vec pos-samples.vec
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
- 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
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
0.5 are self-explanatory. Details on all these parameters are found in the documentation.
Tried with: OpenCV 2.4.9 and Ubuntu 14.04