Haar Cascade OpenCV Object Detection

Today, you can see your face getting recognized by android phone or anywhere. That’s very cool! But recognizing of faces in images or in video is also talking every days. So let we talk now about detecting and recognizing somethings of our choice using haar cascade opencv.


In the first word, I think you need let time to watch the tutorial after reading this post for easier to understand. Because this task takes a lot of time for me to learn. And that is why I am hurry to write this post.
And this page is an open source computer vision library.

Starting with a green housing:


Preparation of Hardware and Software
Train our own OpenCV Haar Cascade classifier

Firstly, we talk about some theories.


To train our own classifier, we need many samples. A sample is the training data that contains positive sample and negative sample. In particular, a positive simple contains a lot of images that show the object we want to detect. And a negative contains images without object. In addition, the numbers of samples depend on a variety of factors, including the quality of the images, the object you want to recognize, the method to generate the samples, etc. For example, we have success with 2000 samples created using 100 positive images and 135 negative images with createsamples tool of OpenCV.


Positive image contain the object to recognize which is made by crop object’s part of the large image. So we need get positive images different angle, side, etc. But in our project, we only recognizes the object from the front view.
Some positive samples:



The negative image don’t contain the object to recognize. The negative image maybe created by crop the part which don’t contain the object. However, for easier and faster, we can take an image without any object.
Some negative samples:

3 13

Preparing data by OpenCV’s tool

You can manually create the training data. Or using other program like “object locator”. However, in this post, we will talk about using OpenCV’s tool.
Needed preparing:

  • Positive images folder: positive
  • Negative images folder: negative
  • Clone the reference code: https://github.com/Booppey/haarcascade.git

After that, you could create your “samples.txt” file:

cd to workspace
find positive -iname "*.png" > positives.txt
find negative -iname "*.png" > negatives.txt
perl code_ref/createsamples.pl positives.txt negatives.txt samples 2000 "opencv_createsamples -bgcolor 0 -bgthresh 0 -maxxangle 1.1 -maxyangle 1.1 maxzangle 0.5 -maxidev 40 -w 60 -h 60"

find samples -name '*.vec' > samples.txt

Then create your “samples.vec” used to train the detector.

cp opencv-haar-classifier-training/src/mergevec.cpp ~/opencv-2.4.11/apps/haartraining
cd ~/opencv-2.4.11/apps/haartraining
g++ `pkg-config --libs --cflags opencv` -I. -o mergevec mergevec.cpp cvboost.cpp cvcommon.cpp cvsamples.cpp cvhaarclassifier.cpp cvhaartraining.cpp cvsamplesoutput.cpp -lopencv_core -lopencv_imgproc -lopencv_highgui -lopencv_imgproc -lopencv_features2d -lopencv_flann -lopencv_nonfree -lopencv_calib3d -lopencv_objdetect
cd your_workspace_name
cp ~/opencv-2.4.11/apps/haartraining/mergevec .
./mergevec samples.txt samples.vec
Training the classifier

Now train our detector using samples.vec by typing on terminal:

opencv_traincascade -data classifier -vec samples.vec -bg negatives.txt -numStages 20 -minHitRate 0.999 -maxFalseAlarmRate 0.5 -numPos 1500 -numNeg 135 -w 60 -h 60 -mode ALL -precalcValBufSize 2048 -precalcIdxBufSize 2048
Using our own classifier for object detection

After the training, the detector can understand what is the object will be detected by the cascade.xml file. So now you need to use this file to detection. Finally, check our github for see the source code for this task.

Check our github
git clone https://github.com/Booppey/haarcascade.git

Haar Cascade is a method simple and effective for detect 1 type of object at time. But this approach can detect only one object. If you want to detect multi type of object, you can also use other method like Bag of Visual Word (BoVW) using SIFT/SURF, KMEAN, SVM. Those kinds of method not only detect 1 object, but its also detect multiple objects.

Share This:

11 Replies to “Haar Cascade OpenCV Object Detection”

  1. Daniel Cole

    I do not even understand how I ended up right here, however I assumed this put up was once great. I don’t understand who you are however definitely you’re going to a famous blogger in case you are not already 😉 Cheers!

    • admin

      Hi @Daniel Cole,
      I am sure that I am not a really blogger 😉
      I was researched and working in this domain, so I just write here for share with others something I interest.

  2. Paula Green

    Thanks for another informative website. Where else may I get that kind of information written in such an ideal means? I have a venture that I’m simply now operating on, and I have been on the glance out for such info.

  3. Richard Fuller

    Very nice post. I just stumbled upon your blog and wished to say that I’ve really enjoyed browsing your blog posts. After all I will be subscribing to your feed and I hope you write again very soon!

  4. DaciaQBaisey

    I like the valuable info you provide within your articles.
    I will bookmark your weblog and look again here regularly.
    I am just quite sure I’ll learn plenty of new stuff on this site!
    Best of luck for the following!

Leave a Reply to Gerardo Quintero arrest Cancel reply

Your email address will not be published. Required fields are marked *