Rhonda Software

Highest quality full cycle software development.

Expert in areas of Computer Vision, Multimedia, Messaging, Networking and others. Focused on embedded software development. Competent in building cross-platform solutions and distributed SW systems.

Offer standalone custom solutions as well as integration of existing products. Opened for outsourcing services.

Visit us at: http://www.rhondasoftware.com

OpenCV Haartraining: Detect objects using Haar-like features

Posted on : 02-06-2009 | By : rhondasw | In : OpenCV


OpenCV’s standart cascades allow to detect faces and eyes.  I wanted to create cascade in similar way to detect another objects:  pringles or plate for example.  I found some material in Net how to use OpenCV training tools, also I investigated training tool’s source  code myself  to found out, what training parameters can be tuned.

Prepare images.

For training, I needed thousands of images, containing my object with different lightning conditions and perspectives. After trying to find required number of pictures with Google ,  I understood, that it’s really difficult task =).  So I decided to take video with my object,  then I wrote simple program to crop object from video, frame by frame.  In such way,  I generated about 3000 positive samples (cropped images  with my object).  Resolution varied from 50×50 to 100×100.  The advantage of this method – you  get many samples with different reflections, illuminations and backgrounds.  It’s very important, that all these images “features” are various!

Negative samples I got  from Internet public databases…, there were about 5000 images of different resolution, which did not contain my objects.   I worked with BMP format, due to compression artefacts in some compression levels of JPEG.

Sample/Test data creation.

So I split all my images into 4 sets:

  • Positive images, containing my object – about 3000 images, for training.
  • Negative images – about 5000 images, for training.
  • Positive images – about 100 for testing.
  • Negative images – about 100 for testing.

Of course, I could use the same images for testing and training, but it’s not good.  Positive images should be be packed to OpenCV vec-file.  it can be done with createsamples program from OpenCV.  When packed, all images are resized to 20×20 – it’s good size for training speed.  Also during object detection, object with smaller size will not be found.   For negative samples,  I only created negative.dat(list of negative file names) with win dir \b command.


OpenCV implements Adaboost algorithm in haartraining. exe.  This tool  generates xml cascade file, which is used in haardetection tool for object detection.  There are some main characteristics of cascade:  number of stages, type of classifiers on each stage. These parameters can be configured when you launch haartraining. I configured it as per my training data (positives and negatives).

Training is finished, when required false alarm of cascade is achieved.  So cascade can contain less stages than nstage – it’s ok. It’s rather worse, when we achieve required number of stage, but required false alarm is not. It’s signal, that something is wrong either with content or with haartraining parameters.  Also maxfalsealarm is false alarm of stage, false alarm of cascade is multiplication of stage’s alarms.

I used P4 2.7GHZ with 2 GB and it took about !6 days! of permanent working to finally get cascade.  If you have Hyper Threading, you can recompile haartraining.exe with OpenMP support, it will speed up process a little bit =).


Finally I got haarcascade.xml file. OpenCV perfomance.exe tool can be used for testing cascade.  I tested it on my testing content  – sorry for pun =).

Comments (293)

Write a comment