AUTHORS:Abhishek Kumar Annamraju,Akashdeep Singh,Adhesh Shrivastava

Hello Friends

In our previous post we learnt how to generate a cascade.xml file using opencv_traincascade.We usually tend to use it with facedetect.cpp present in OpenCV folder.

Now what if we want to use multiple xml files over the same image and display a combined result????

Well,after a little struggle we came up with an optimum code that does the above mentioned task in a very fast way.The code has only been implemented to images,will try to develop it further for videos too.

Download the code from here:https://github.com/abhi-kumar/OPENCV_MISC/blob/master/objectDetectionMultipleCascadeInput.cpp

The code is well commented,so you will understand everything once you carefully go through it.

Some of the results of the code:

I used it to detect cars,database was chosen as UIUC car database.

The cascade files used can be downloaded from here:https://github.com/abhi-kumar/CAR-DETECTION.git

These are sum of the results:
Detection time:38.2002 ms




Detection time:38.4898 ms




You are always welcome to download the code and modify it for better usages.Any contribution to the code is highly appreciated.

Thank you 🙂

See also :- http://blindperception.wordpress.com/



  1. Hello, I want to ask a question for your car detection code, why do you use two cascades which are respectively checkcascade and cascade to detect the car, is two cascade different when in training or else?
    thanks in advance!

    1. Hi there,
      The two cascades are trained in different way.The main ones detect the car as a whole,while the check cascade detects small features like car doors,rear-view mirrors in a car.I wanted to go for this because as you might be aware of the fact that how training using train_cascade can be bad a lot of times.So whenever you get poorly trained cascades of the same object,you can use my code to detect it.Also the check cascade can even contain a normal trained xml file for the whole car,but when you do this make sure you create the samples with width and height less than 30 pixels(just an experimentally determined result)

      Hope this helps you 🙂

  2. Hi man, I train my traincascade for detect traffic sign, but stay slow, any idea for make better ? I already resize the frame for process, stay better but no suficient.

    1. Hi Antonio,
      The traincascade procedure is actually slow,it is a matter of how well you put the parameters(numPos,numNeg,numStages,maxfalsealarm,minhitrate etc) and how efficiently these parameters match your system specifications.According to me it is a trial and error issue.I too got the trained cascades after a very long training time and those were damn poor too.Well these are some things that could make your training a little faster:

      a)-numNeg must be the number of photos you have in neg folder.
      b)numPos = (npos + (numStages-1) * (1 – minHitRate) * numPose) + S, where S is a count of samples from vec-file.S is a count of samples from vec-file that can be recognized as background right away.

      Well because of these badly trained cascades I came up with the code of using multiple cascades in one run.You could also look for SVM training,its a lot faster.Also I am working on building an efficient method of object learning stuff,will publish it as soon as it gets ready.

      Hope this helps you 🙂

  3. Which OpenCV version you are using ?? I used 2.3 It dosen’t seem to be working .. It crashes while loading xml files

      1. Hi again, i have a big doubt, What was the difference in the training samples of each xml? I see different stages in each xml, and with the cas2.xml you use HOG, but indeed that, how was the samples?

      2. Hi there,

        Apologies for the delay in my reply. Yeah, all the XMLs present there are trained using different parameters sets. I was actually experimenting on the effects of those parameters on the training quality( paper published on the same). So the presented XMLs there are one of the many training sets, may be the weakest ones, only to prove the fact that these when combined in some order/form may work efficiently due to overfitting. I think by reading the first few lines of the XMLs you may be able to find out the differences between them, the training data was always the same as mentioned.


  4. hi abhishek…code seems to be just perfect…how do we implement this on matlab….and is this code for detecting moving objects with still camera or camera is too in motion…

  5. I have a problem, I have 50 positives images, and 990 negatives when I run .bat traincascade. this stops process in stage four. you help me understand the problem

    “C:\OpenCV2.2\bin\opencv_traincascade.exe -data cascade -vec G01_222_282.vec -bg negatives.txt -numPos 50 -numNeg 990 -numStages 20 -nsplits 2 -featureType LBP -minHitRate 0.998 -w 24 -h 24 -mode ALL -precalcValBufSize 20248 -precalcIdxBufSize 20248”


    ===== TRAINING 4-stage =====
    POS count : consumed 100 : 100
    NEG count : acceptanceRatio 300 : 4.21601e-005
    Required leaf false alarm rate achieved. Branch training terminated.

    1. Hi,

      The traincascade works on the principle of finding a probability of accepting a negative image, so when this value gets very low the negatives are not taken. I may not be correct here, keep positive to negative images ratio to approximately 1:2.


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