OPENCV is an open source image processing library developed by Intel and currently maintained by WillowGarage. It is very powerful and can be coded in C/C++ or Python. I chose C, as it is more comfortable to me.

Bottle Detection
Bottle Detection

Object detection can be done using SURF or by using  Haartraining using Viola Jones algorithm to form a cascade file. Even though detection using Haartraining is more difficult to train  compared to SURF, it is less error prone and of course faster to run in real time.I used Haartraining to detect  a bottle. I just experimented. The first step was to  take some positive and negative images, I took about 180 photos containing the bottle to be detected (positive images) and about 260 background images which did not have the bottle (negative images). It was really a small  number of images compared to about 5000 images used to train the classifier for face detection.STEP

The next step was to mark the object in the positive set of images using Objectmarker tool to yield a description file, a description file for negative samples was also created. Then using createsamples utility provided with opencv, a training sample , ie, a vec file was created. Now I started Haartraining using the haartraining tool. 25 stages was defined. It was going really fast upto the 15th stage, then gradually it slowed down, in fact training the 25th stage took me about 2 days. The whole process  took about 4 days, after which the xml file was created. the xml file can also be created in between using the convert_cascade program provided with opencv. The screenshots at various stages are given below.

Marking the object to be detected using Objectmarker
Output of the createsamples tool

An intermediate stage  during Haartraining

Intermediate output showing the haartraining progress

Here comes the outputs, the bottle in my hand is identified and encircled

some more............

The final outputs………

I am now working on an image processing robot that will track down objects, go near to them and pick them  and  handle them to the operator, upon his command to take something. It is for our college technical fest. Now I have to train some more cascades to detect various household objects


  1. hii… i am finding error in haartraining… i have used 200 positive samples and 289 negative samples…i gave the following command ….
    C:\project\temp\haartraining.exe -data cascade -vec vector.vec -bg NegativeNew.txt -npos 200 -nneg 289 -nstages 10 -mem 1000 -mode ALL -w 30 -h 30 -nonsym
    but the problem is only two stages get completed when i specified 10 stages .. why does this happen? one more thing i noticed that Required leaf false alarm rate comes around 6.10e-005 or something similar if i give higher stages like 25… is that anythin to do wd this? or is it the count of positive and negative samples?or any other reason.. no matter wat number of stages i specify it jus completes 2 stages…pls help.. thanks

  2. Could you share the haar cascade? Is it form invariance? For example, a coke bottle is not detected by your cascade?
    I want to create a cascade to recognize a kind of bottle, but I don’t want that this cascade recognize another bottles. I create a cascade with a few images and it recognize bottle, but if I put the label in another bottle the cascade recognize this bottle as the real bottle. What do you think about that? Do I need train the cascade with the bottle without the label? So the cascade will lear the shape of the bottle and discard the label. Then I could looking for another approach to recognize the label.

  3. achhu that was very useful but how to stop a aining midway??i gave 19 stages and its been going on 3 days now…pliz help..

  4. Hi, I am a university students who is studying Image processing and pattern recognition. I’m interested to get your bottle detection coding from you. Could you send me your coding in my email, huilian28@1utar.my ? Thank you very much !

  5. Dear Achu, I am in high school, and working on project to detect objects using kinect and opencv. I am wondering if you are willing to send me the xml files for objects you may have created, like the one for bottle. There are many available for faces, hands, etc but not for objects that I can find. I would really appreciate your help. You can email me at arjun.dhawan@outlook.com.

  6. Hi. I have to detect car form an image. Please help if anyone knows something about it as i don;t know nothing about OpenCv.

  7. Hi, im trying to detect a 7up bottle as well. Ive been trying to run the haartraining.. but in my computer instead of taking a few hours or days to run, it just finishes up in a few mins. I’m using around a 100 positives an 300 negatives. Obviously theres some error and the xml file does not work.

    Is it possible for anyone to share with me your xml database? I need this urgently for a school project.

    My email is ken_h2000@hotmail.com

    Thank you.

  8. Hi Achu,

    Your blog helped me a lot with my dissertation. I scored pretty good marks too. Thanks a lot. Keep writing.


  9. Hi man I am new to Opencv and python. My project is to detect and segragate objects( basic coloured spheres). The problem is i am to do it on a raspberry pi. This is where we are facing problems, the pi is not able to capture images at times so i guess optimization is gonna be a huge issue. how do you suggest we should go about? Should be do haartraining..my doubts if pi will be able to sustain that.

  10. Hi….I am new to the OpenCV. I want to detect all the objects in video e.g.in video whether it is a trench or not.So please help me from where should I start?
    Thanks in advance.

  11. Hi,
    Suppose i have several cubes in an arena which are of the same color(red).How do i find the centroid of the top face of each cube(assuming my camera can c only the top face of all cubes)??
    If i use cv moments on the thresholded image i will get the centroid of the entire red parts in the image.how do i get the centroid of the individual cubes??

  12. hello. we’re doing a project on ASL hand sign recognition on a mobile device. it’s supposed to identify what letter the hand sign is equivalent to and display it on screen.
    we’re planning on preprocessing the image first. like conversion to hsv and some dilation. identification of contours will follow. the biggest contour will be referred to as the contour of the hand. we are making this conclusion because our project is limited to just the hand and black static background present in the images/video stream obtained from the mobile device camera.
    i was just wondering. can we use haartraining for the identification of the hand signs after the necessary preprocesses? supposedly, it should be able to identify the 24 alphabets, excluding the moving hand signs for now. if so, can you give me a more detailed tutorial on how to start the haartraining process? i am really new to opencv and image processing. i can’t understand most of the technical stuff you were describing above. also, i am using c++ for the code.

  13. Hello, im a beginner in opencv….i have to detect type of vehicle in my project first i want to detect vehicle….so should i use haar training for it…thanks in advance

    1. accu ……please help me can u please help me m in troubble please sort ma problem please m doing vehicle detection as ma year project but m stuck while creatng vec file vec file is created but having 0 kb size please helppp meeeeeeeee please……….

  14. Achu,

    Your blog was of great help. Thank you. I have one question though.
    How do we decide the number of stages required for haar training?

    1. The choice of the number of stages depends on the accuracy you need. About 20 -25 stages will do good. You can inspect the performance of the classifier while training is going on and stop the training when it is found to be accurate.

      1. Thank you Achu for your reply!!

        I am trying with the new traincascade exe now to compare it with haartraining. The speed is very good with the new one. I wil let you know how it turns out. Thanks.

      2. acu can u please help me m in troubble please sort ma problem please m doing vehicle detection as ma year project but m stuck while creatng vec file vec file is created but having 0 kb size please helppp meeeeeeeee please……….

  15. Hi !

    Can you please tell me if it’s better to have one item in each positive sample image, or if it’s ok to have many items in the positive sample image ? For example is it ok to have two or 5 or even 100 bottles in a positive picture used for training of bottle detection ?


  16. hey,
    we have a b-i-g problem in our project. we try to run the haartraining on 3 pictures(3 negitive and 3 positive) just to try if it’s working’ and it’s stuck in cluster 11, we read that it took to u 2 days too, but we used only 3 pictueres, so we think maybe it’s stuck for other reason.. do u have an idea?


  17. hey,
    we have a b-i-g problem in our project. we try to run the haartraining on 3 pictures(3 negitive and 3 positive) just to try if it’s working’ and it’s stuck in cluster 11, we read that it took to u 2 days too, but we used only 3 pictueres, so we think maybe it’s stuck for other reason.. do u have an idea?


  18. Hey man. My project is quite similar to yours. So I would be really really glad if you can send me your bottle.xml file. I know I am asking a lot, but my laptop was working for 3 days now and still the classifier is not accurate enough. Also I have to submit the project in a few days. So thanks in advance mate🙂

  19. hi dude..
    i have no idea how to do it.i have detected the object using your above suggested method.
    i dont know how to proceed.
    it would be helpful if u can give a detailed specification for my problem.
    thank you

  20. hello
    i am akshat and i am doing a project on computer interaction [ emulate mouse etc] using hand gesture recognition in openCV.
    do you have any projects on such topic
    if you have plz share
    since me and my partner are not very familiar with opencv and linux environment
    and our project mentor is useless.
    it will be really helpful ..

  21. hi i am new to opencv. can u plz elaborate the process. my project is to move robot based on gesture. i am getting how to detect different gesture. It is similar like ur project. THANKS

  22. hi. I’m new to opencv and i am doing a similar project for my undergrad thesis. What do you mean when you’re talking about “stages for classifying”?

    Your help will be very much appreciated. Thank you.

    1. Hi, I would be happy to help you with the coding, but now I am a little busy with my exams. Detecting tennis balls wont be a big challenge. You dont need to do Haartraining and such stuff for this. They can be detected by simple color and shape based detection. You have to
      1)grab a frame from the camera
      2)Convert the RGB image to HSV so that intensity variations does not affect much
      3)Threshold the HSV image for the color ranges of the tennis ball. Now you will get those areas of the frame having the color of the tennis ball
      4) Smooth the image
      5)Apply Hough transform to get areas of circular shape. you can specify the maximum and minimum size limitations too.
      6) Get the pixel coordinates of the circle
      Better try it in python if you are a beginner.

  23. Hey,

    When using haartraining how do you find the correct value to put into the -npos parameter? I have a list of images in a text file with the additional crop info, eg;

    ./Images/Postives/frontal_sitting_0006.jpg 2 112 60 36 28 142 76 25 20
    ./Images/Postives/frontal_sitting_0007.jpg 2 107 66 32 25 134 82 26 20
    ./Images/Postives/frontal_sitting_0008.jpg 2 95 74 31 24 123 89 27 21
    ./Images/Postives/frontal_sitting_0009.jpg 2 86 78 34 27 113 92 28 22
    ./Images/Postives/frontal_sitting_0010.jpg 2 107 97 23 18 77 83 32 25
    ./Images/Postives/frontal_sitting_0011.jpg 1 247 2 53 42
    ./Images/Postives/frontal_sitting_0012.jpg 3 75 109 26 20 48 96 31 24 202 8 61 48
    ./Images/Postives/frontal_sitting_0013.jpg 3 38 97 32 25 66 108 23 18 187 6 54 43
    ./Images/Postives/frontal_sitting_0014.jpg 3 172 11 56 44 33 100 29 23 58 108 25 20
    ./Images/Postives/frontal_sitting_0015.jpg 3 24 101 29 23 47 111 26 20 152 17 56 44

    I assumes the vec file contains the number of positive samples? Do I just leave the npos parameter out?


  24. Hi,

    I am trying to use haartraining to detect my hand. I have used 500 positive samples and 1000 negative samples. Haarperformance report was not good. None of the testing images detected. All were missed. Error was high. It created some mark in testing photo. But each photo had different place of marking and it was multiple detection in wrong place. I have also tried by taking very few images as both positive and negative. But all are same problem. haarperformance result is poor. Can you give some tips for getting better result by using haartraining?

  25. Hi There;

    Have you experimented with Positive Builder. If so can you guide me with the following question. I am using Positive Builder and instead of using an avi video file I want to use a folder that contains many images and I want it to load my images one by one just like taking frames from video. I know its not a big deal but if it is already available in Positive Builder this work would be redundant.


  26. HI could you tell me what are the features of the pc that you used to do the haartraning? right now i`m using a pentium core i7 processor 2.8 GHZ, 12 gigabytes RAM, OS windows 7 and my sample consist of 1492 positive and 5000 negative images(aprox).

    curiosly as you, it`s gettin slower once i get through the 15th stage, right know i`m on 17(it took 6 hours aprox to calculate this levels) so i wonder if it will take the same time as you.


    1. Hi, I did haar training on my laptop (ACER ASPIRE 5738) powered by intel core2duobprocessor 2.2Ghz, 4GB DDR2 RAM, Ubuntu 10.04 Lucid Lynx LTS OS.. and I allocated 2 GB of RAM for haartraining

      I also faced a situation similar to that described by you. The training of initial stages went on smoothly and fastly, after about 12 th stage, it became slower. It took me about 2/3 days to complete the 20th stage. I opted for 30 stages but I found that the classifier was satisfactory, so the training was aborted. The whole training took about 1 week, and killed my battery since the lap was continuously plugged in for 1 week.

      1. I think it would be enough. You can use the convert cascade tool to create the xml file and use it to detect your object. You need not stop the training for using the convert cascade tool. If found successful you can stop the training…

  27. hi, i have just started using opencv2.1 on vs2010 for my college hand gesture recognition project search a lot online but the picture is not clear how to and what method to follow plz help

    1. Hi, for hand gesture recognition, all you have to do is to detect and get the x,y coordinates of the hand, and then map the changes in those x,y values when a gesture is performed. You would better use haar classifier to detect the hand. You may first try the classifier given in this link with the code given in my blog. Then you try to create your own classifier. Its all simple and best wishes.

  28. Hello Mr. achuwison,

    Your article is very interesting, I am researching object detection by using openCV, I want to to study more about your test. If you still keep this example, could you please send me your positive samples in this article, if there are also negative samples, your help is highly appreciated.

    Please contact with me via email bienna@necsv.com

    Many thanks from me.

    Best regards.

    Nguyen An Bien

  29. Hi, I’m brazilian, I’m based in your work to develop an program for my monography. I would like an opinion. I need to make an program to recognize banknotes. Do you recommend that I do this with HaarTraining? Do you think I will have success? Otherwise, I would like an opinion how could I have a good result! Thank you.

    1. Hi, I feel that using HaarTraining would be an overkill for recognizing banknotes. The training of the classifier is little difficult. It would be better if you try out template matching.

  30. Hello. I have been trying to implement object recognition using OpenCV as a part of my final degree thesis. I have mostly followed the steps presented on a blog http://www.quotientrobotics.com/2010/04/opencv-haartraining-object-detection.html using a number of 900 positive samples and 900 negative images for the process. The number of stages in the haartraining step was set to 50 and I used Ubuntu 10.10. I was disappointed to notice the classifier was not behaving accurate at the end. Could you give me a more detailed description of your success with OpenCV and object recognition and if you can the source code for the final test detection?

    Thank you.

  31. Hi I’m Marco.

    I see that you used an small amount of images, I thought that haartrining would require more, but it looks pretty well (enough for me).

    I’m thinking of doing something similar, with a similar amount of images also, so I would like to ask you something: How do you recomend to set the parameters in haartrining.exe?


    1. Hii,I did haartraining on my laptop powered by Intel Core 2 Duo (2.2 GHz) and with 4 Gb DDR2 RAM and running ubuntu 10.04 64 bit. The vec file and a text file containing the name of negative images (background images) was created. The number of stages was set to 30, although I stopped haartraining at 26th stage because it was taking too much time and the results at 26th stage was satisfactory . 2000 Mb of memory was allocated to haartraining.

      In short the arguments given to haartraining are “opencv-haartraining -data h -vec positive/bmp/h.vec -bg negative/bmp/negative.txt -nstages 30 -mem 2000 -mode all -w 30 -h 32”

      1. Hi,

        I successfully created the vec file but when i try haartraining, I get the below error
        Invalid background description file.
        Wonder if you encounter this before?
        I would appreciate some pointers, thank you.

      2. Hi, I haven’t encountered an error like this, but I think that I can help.. Can you please describe something about your background description file?. My background description file constituted of just the negative sample image names, one filename per line. And that background descriptor file was placed in the same folder of the negative images. You can easily create the negative descriptor using the following command:
        ls > negative.txt
        in the linux terminal. It lists all the files and writes their names into a text file named negative. Then while running haartraining, the path of the negative file descriptor is given. Thats all, and it worked for me.

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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 )

Google+ photo

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

Connecting to %s