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

FAQ: OpenCV Haartraining

Posted on : 10-11-2009 | By : rhondasw | In : OpenCV


Hi All, before posting your question, please look at this FAQ carefully! Also you can read OpenCV haartraining article.  If you are sure, there is no answer to your question, feel free to post comment.  Also please, put comments about improvement of this post.  This post will be updated, if needed.

Positive images

Why positive images are named so?

Because a positive image contains the target object which you want machine to detect. Unlike them, a negative image doesn’t contain such target objects.

What’s vec file in OpenCV haartraining?

During haartraining positive samples should have the same width and height as you define in command “-w -h size”.  So original positive images are resized  and packed as thumbs to vec file. Vec file has header: number of positive samples, width, height and contain positive thumbs in body.

Is it possible to merge vec files?

Yes, use Google, there are free tools, written by OpenCV’s community.

I have  positive images, how create vec file of positive samples?

There is tool in C:\Program Files\OpenCV\apps\HaarTraining\src createsamples.cpp.  Usage:

createsamples -info positive_description.txt -vec samples.vec -w 20 -h 20

What’s positive description file?

The matter is that, on each positive image, there can be several objects. They have bounding rectangles: x,y, width, height.  So you can write such description info of image:

positive_image_name  num_of_objects x y width height x y width height …

Text file, which contains such info about positive images is called description file. So during vec file generation,  really objects are packed, but not whole image. Essentially vec file is needed to speed up machine learning.

Do I always need description file, even if I have only one object on a image?

Yes, with createsamples you need description file.  If you have only one object, it’s bounding rectangle may be bounding rectangle of whole image. If you want, write your own tool for vec file generation =)

Should lightning conditions and background be various on positive images?

Yes, it’s very important. On each positive image, beside object, there is background. Try to fill this background with random noise, avoid constant background.

How much background should be on positive image?

If you have much background pixels on your positive images in comparison with object’s pixels – it’s bad since the haartraining could remember the background as feature of positive image.

If you don’t have background pixels at all – it’s also bad. There should be small background frame on positive image

Should all original positive images have the same size?

No, original images can have any size.  But it’s important that width, height of this rectangle have the same aspect ratio as -w -h.

What’ s  -w and -h should I put in createsamples? Should it be always square?

You can put any value to -w and -h depend on aspect ratio of the target object which you want to detect.  But objects of smaller size will not be detected! For faces, commonly used values are 24×24, 20×20. But you may use 24×20, 20×24, etc.

Errors during vec file generation: Incorrect size of input array, 0 kb vec file,

-First check you description file: positive_image_name should be absolute path name without spaces like “C:\content\image.jpg” not “C:\con tent\image.jpg” or relative path name.

-Avoid empty lines in description file

-Resolution of original positive image file should be not less, then -w -h parameters you put.

-Check that positive images are available in your file systems and not corrupted.

-There can be unsupported formats. Jpeg, Bmp, PPM are supported!

Example of vec file generation!

Let’s working directory be C:\haartraining. In it there is createsamples.exe. There is folder

C:\haartraining\positives. So create description file positive_desc.txt.

positives\image1.jpg 1 10 10 20 20

positives\image2.jpg 2 30 30 50 50 60 60 70 70


C:\haartraining\positives\image1.jpg 1 10 10 20 20

C:\haartraining\positives\image2.jpg 2 30 30 50 50 60 60 70 70

You should avoid empty lines and empty space in image’s path

createsamples -info positive_desc.txt -vec samples.vec -w 20 -h 20

Negative images

What negative images should I take?

You can use any image of OpenCV supported formats, which does not contain target objects (which are present on positive images). But they should be various – it’s important! Good enough database is here

Should negative images have the same size?

No. But the size should not be less than -w -h, which were put during vec file generation.

What’s description file for negative image?

It’s just text file, often called negative.dat, which contains full path to negative images like:



Avoid empty lines in it.

How many negative/positive image should I take?

It depends on your task.  For real cascades there should be about 1000 positive images and 2000 negative images e.g.

Good enough proportion is  positive:negative = 1:2, but it’s not hard rule! I would recommend first to use small number of samples, generate cascade, test it, then enlarge number of samples.

Launch haartraining.exe (OpenCV\apps\HaarTraining\src)

Example of launching

Working directory is C:\haartraining with haartraining.exe tool and samples.vec file.

Let’s negative images are in C:\haartraining\negative, in this case negative.dat should be like this:



So in C:\haartraining launch this:  haartraining -data haarcascade -vec samples.vec -bg negatives.dat -nstages 20  -minhitrate 0.999 -maxfalsealarm 0.5 -npos 1000 -nneg 2000 -w 20 -h 20 -nonsym -mem 1024

  • w  h  is the same, you put during vec file generation
  • npos nneg  – number of positive samples and negative samples
  • mem – RAM memory, that program may use
  • maxfalsealarm – maximum false alarm, that stage may have. If big false alarm – it could be bad detection system
  • minhitrate – minimal hit rate, that should stage have at least
  • nstage – number of stages in cascade

What’ s falsealarm and hitrate of stage?

You should read theory of adaboost about strong classifier. Stage is strong classifier. In short:

  • For example you have 1000 positive samples. You want your system to detect 900 of them. So desired hitrate = 900/1000 = 0.9. Commonly, put minhitrate = 0.999
  • For example you have 1000 negative samples. Because it’s negative, you don’t want your system to detect them. But your system, because it has error, will detect some of them. Let error be about 490 samples, so false alarm = 490/1000 = 0.49. Commonly,put false alarm  = 0.5

Are falsealarm and hitrate depend on each other?

Yes, there is dependency. You could not put minhitrate = 1.0 and maxfalsealarm = 0.0. .

Firstly, the system builds classifier with desired hitrate, then it will calculate it’s falsealarm, if the false alarm is higher than maxfalsealarm, the system will reject such classifier and will build the next one. During haartraining you may see such:

| 0 |25%|-|-1423.312590| 1.000000| 1.000000| 0.876272|

HR – hitrate

FA – falsealarm

What’s falsealarm and hitrate of whole cascade?

Cascade is linked list (or three) of stages. That’s why:

  • False alarm of cascade = false alarm of  stage 1* false alarm of  stage 2* …
  • Hit rate = hitrate of  stage 1 * hitrate of stage 2* …

How many stages should be used?

  • If you set big number of stages, then you will achieve better false alarm, but it will take more time for generating cascade.
  • If you set big number of stages, then the detection time could be slower
  • If you set big number of stages, then the worse hitrate will be (0.99*0.99*… etc). Commonly 14-25 stages are enough
  • It’s useless to set many stage, if you have small number of positive, negative samples

What’s weighttrimming, eqw, bt, nonsym options?

Really all these parameters are related to Adaboost, read theory. In short:

  • nonsym – If you positive samples are not X or Y symmetric, put -nonsym, -sym is default!
  • eqw – if you have different number of pos and neg images, it’s better to put no eqw
  • weighttrimming – for calculation optimization. It can reduce calculation time a little, but quality may be worse
  • bt – what Adaboost algorithm to use: Real AB, Gentle AB, etc.

What’s  minpos, nsplits, maxtreesplits options?

These parameters are related to clustering. In Adaboost different week classifier may be used: stump-based or tree-based.  If you choose nsplits > 0, tree-based will be used and you should set up minpos and maxtreesplits.

  • nsplits – minimun number of nodes in tree
  • maxtreesplits – maximum number of nodes in tree. If maxtreesplits < nsplits,  tree will not be built
  • minpos – number of positive images, that can be used by one node during training.  All positive images are splitted between nodes. Generally minpos  should be not less than  npos/nsplits.

Errors and stranges during haartraining!

  • Error (valid only for Discrete and Real AdaBoost): misclass – it’s warning, but no error.  Some options are specific to D and R Adaboost.  So your haartraining is ok.
  • Screen is filled with such | 1000 |25%|-|-1423.312590| 1.000000| 1.000000| 0.876272| – your training is cycled, restart it. First column should have value < 100
  • cvAlloc fails. Our of memory – you give too much negative images or sample.vec is too big. All these pictures are loaded to RAM.
  • Pay attention you put the same -w and -h, as during vec file generation
  • Pay attention, that number of positive samples and negative samples, you put in -npos -nneg are really available
  • Avoid empty line in negative.dat file
  • Required leaf false alarm rate achieved. Branch training terminated – it’s impossible to build classifier with good false alarm on this negative images. Check your negative images are really negative =),  maxfalsealarm should be in [0.4-0.5]

OpenCV XML haarcascade

During haartraining, there are txt file in haarcascade folder, how can we get XML  from them?

There is OpenCV/samples/c/convert_cascade.c. Use like:

convert_cascade –size=”20×20″ haarcascade haarcascade.xml

How can I test generated XML cascade?

 There is OpenCv/apps/HaarTraining/src /perfomance.cpp. You need have positive images(not used during training) and positive description file.  Use like:

performance -data haarcascade -w 20 -h 20 -info positive_description.txt -ni

performance -data haarcascade.xml -info positive_description.txt -ni

Time and Speed of haar cascade generation

Average time to generate cascade on PC?

It depends on task and your machine.  I generated cascade for face detection, for this used such parameters: -nstages 20 -minhitrate 0.999 -maxfalsealarm 0.5 -npos 4000 -nneg 5000 -w 20 -h 20 -nonsym -mem 1024.  It took 6 days on Pentium 2.7GHZ 2GB RAM.

What is OpenMP?

The OpenMP (Open Multi-Processing) is an application programming interface (API) that supports multi-platform shared memory multiprocessing programming in C, C++ and Fortran on many architectures, including Unix and Microsoft Windows platforms“. If you have MT processor, you can use it.  In code you should add OpenMP defines and put compile options.  For example in VisualStudio2005: Properties->C/C++->Language->OpenMP support

Is it possible to improve speed of haartraining?

Yes, one of possible ways is to use parallel programming. We have realized OpenCV haartraining using MPI for linux cluster. You can read it here

Object detection with OpenCV XML cascades

Is it possible to detect rotated faces?

Yes. It is impossible to generate cascade, which can detect face in all orientations. But you can generate cascade for each orientation separately. For this you need positive content of rotated faces. You can try to generate cascade with OpenCV , add -mode ALL, with it tilted haar feature will be used. But it’s badly implemented, at least in OpenCV 1.1. If you want you can add your own feature to opencv haartraining – it’s not too hard.

Another approach is to write head pose estimator. Then rotate your pictures, so that you have frontal face and detect it with OpenCV default face cascade

Is it possible to recognize gender, attention, race with  Haar features?

We tried, but could not do it with OpenCV haartraining. That’s why for such classification, we used our own gender and attention classificators. Of course you can use Adaboost for this task, which is implemented in haartraining, but we did not get good results.

Is it possible to detect faces in real time?

Yes.  On PC default OpenCV facedetector takes about 200 ms for 640×480 picture, about 5fps – it’s not real time. We have changed facedetector and get about 15 fps – which is real time. You can see results  here and here

Comments (129)

Hi, first of all, thanks for this information, it’s really useful and explains a lot, my question it’s about the performance tool. you see, i know how to use it but my doubt is in the later table for the ROC plot, i know that the column 3 it’s the hit rate and its rank goes from 0 to 1 always (where 0 is bad and 1 its great!), and the 4 column it’s the false alarm rate but what is his rank??

Thanks for your help

how to train on Linux?

If you have compiled linux version of opencv_haartraining, use it with the same parameters as windows version.


can I use two xml files at the same time?
one xml is use to detect face, one xml is use to detect hand
can my program detect these two things concurrently?


Hi, unfortunately, there is no code in OpenCV for two concurrently executed cascades. You could easy implement two threads for executing cascades concurrently, though.

yes,of course. but u have to change the sample codes a little bit 🙂

Hi..I’ve a problem with createsamples. I’ve made directory “haartraining” as you show in your example. In this directory there are:

* createsamples.exe

* positives —-> it is a directory in wich there are png files of my object I want to detect. All images are 120 x 150 pixels. Each image is named as “hand (1).png”

* positive_desc.txt —> positives\hand (1).png 1 0 0 120 150
positives\hand (2).png 1 0 0 120 150
becouse each image contains only the object I want to detect.

So, when I run createsamples -info positive_desc.txt -vec samples.vec -w 20 -h 20

command prompt show this result:
info file name:
vec file name : samples.vec
BG file name:
num: 1000
BG color: 80
invert: FALSE
max intensity deviation: 40
max x angle: 1.1
max y angle: 1.1
max z angle: 0.5
show samples: False
scale: 4
width: 20

Windows Vista throws a message like this: ” createsamples.exe has terminated…Windows: There is a problem that prevents the proper functioning of the program”

Can you help me?


What version of OpenCV do you use? With versions 1.1 and 2.0 it’s impossible to get “scale: 4” line in response to “createsamples -info positive_desc.txt -vec samples.vec -w 20 -h 20” because of source code:

if( showsamples )
printf( "Scale: %g\n", scale );

So, you will see “Scale: 4” (not “scale: 4”) only if you use “-show” parameter with createsamples.

Hai, i’m riki, Mr Andrey, is it possible to make a personal_frontal_face.xml that generates from my face only, so when it is applied, it will detect my face only, not any one else, so it would be a smart face verification apllication. Is it possible..?, if yes, how to make it…?

Hi, Riki, it’s possible, but you have to provide haartraining with number of negative samples containing faces of other people. Original algorithm of opencv’s haartraining automatically crops and resizes negative images (for example landscapes without faces) to get negative samples (images with size about 20×20 like positive samples). It gets hundreds of millions of negative samples for several thousands of positives. In your case you need 1) to prepare negative faces-samples manually
2) to modify haartraining code to use negatives samples instead of automatic processing of negative images. Of course, you will not be able to prepare 100000000 faces of “not-you”, so you will not be able to get 20-stages cascade. But, I think, you can build several stages and check the result.

After that you will need to execute your “personal” cascade only on windows which passed general face detection cascade (or merge these two cascades).

Hi..I wanted to add some more features for haar training. But i am not sure where to add.. is adding another cvHaarFeature() in icvCreateIntHaarFeatures() the right way?

Very good material! Thank You!
Now my question: I’m understood this phrase –
“No, original images can have any size. But it’s important that width, height of this rectangle have the same aspect ratio as -w -h”
– that “this rectangle” is rectangle about recognizing object. Is this right?
I’ve found answers for all my questions but this…(

PS: I’m sorry, my English is not good :-[

For training we supply number of positive images, cropped to remove extra background down to boundaries of object. Cropped positive pictures are marked by surrounding rectangle (position and size of this rectangle is saved in positive images list). When you make this markup, you have to retain aspect ratio. Haartraining application will scale your positive images to size of (w,h) and this process must not disturb proportions of object.


My problem is that I get while executing Haartraining, I get a ,xm. file but its not able to detect anything.

While training I start getting message “Premature end of JPG file” and training ends around stage 7 with the message “Branch training terminated”

Any help pleaseee



It is an excellent blog for opencv.

I have a question about the training data file.

Following link is another opencv realized with PHP.


My question is that if we can convert the opencv data file to detection.dat.


I’ve looked at the article and believe we could develop a tool for converting detection.dat to opencv xml and vice versa.
If you are interested in such program/script please feel free to contact our sales/marketing team (consult About page for more info).

I’m just wondering if somebody else also encounters endless loops in haar training (OpenCV 1.0 and 2.0 on 64bit Linux). I tried to increased my training data (>2000 pos and >2000 neg samples) but after some hours nothing happens anymore.
The output looks like this:

Parent node: X

*** 1 cluster ***
POS: 2754 2768 0.994942

The “NEG” line never shows up after ‘n’ stages (n~8 of 15) … I have an instance running for ~1 week now other look like that too. Any hints what happened? Will it terminate if a certain falsealarm rate cannot be achieved or will it try again and again and thus ending up in an endless loop?
Thx and greetings,

Andreas, yes, endless loop happens if haartraining cannot extract enough negative samples (small images) from your negative images. Try to replace negative images with others of bigger size or more multifarious. You can stop training, change images and restart training. It will continue from the last successful stage.

Thanks for your answer. All of my positive and negative samples have the same size 18×18. Can I add negative images with different dimensions to finally finish training?

Hi, I am having the exact same problem and even after increasing the number of negative images, the training gets hung.

Parent node: X

*** 1 cluster ***
POS: 2754 2768 0.994942

The NEG line never shows up. Please help.

Are there any details or procedures which should be pay attention to for stopping and restarting the training process?

I have a problem when training a cascade of classifier to detect an object which is the letter E. I started with a 70 samples positive set and 300 negative samples. the result was not satisfying since my detector sometimes detect black or white regions. I have the same question of Andreas. In my negative set, may a include images of arbitrary size, or i should resize all images to -w -h of the detection?

Hi, Lara45. The algorithm requires big negative images, the bigger the better (for example 1280×1024 for positive samples 20×20), don’t use images of size –w –h – it will not work. Haartraining will automatically extract millions of small negative samples from your negative images, so it need images with millions of subregions.

I would like to know if it’s possible to have haarcascade XML generator running on Mac OSX.


Rui Sobral

Hi, i want to thank you for not answer my question, i dont know why you dont want to answer it

Hi Andrey,
Thx for reply. Do u know any source where i can download my negative images instead of wasting time downloading each one alone. I tried http://tutorial-haartraining.googlecode.com/svn/trunk/data/negatives/ but still having problems.
Just one more question, for my positive samples, i constructed them by hand and created the vector file of -w 10 -h 15 (the code indicates that those are distorted when processed). First i tried to not include a lot of background edges with the letter, it didn’t work (bad detection), then i included edges for bg, the performance still not satisfying me :(. Any suggestion???? since unlike faces, this letter (E) presents concavity then features will be aplied on a portion of the background while processing.

One more question, I have to know what does -nneg and -npos refer to in the call for opencv_haartraining function,


hi, pls explain this

— Should all original positive images have the same size?
No, original images can have any size. But it’s important that width, height of this rectangle have the same aspect ratio as -w -h. —

does it mean that original image must be a square if -w and -h in createsamples is 20×20 ?

I think it mean all original pos sample have the same aspect ratio
ex: 20*20 = 30*30 = 40*40 =>the ratio is 1:1
20*40 = 40*80 = 80*160 =>the ratio is 1:2

one more q. — How much background should be on positive image? —

i have original image with face and some background.

then i make positive_desc.txt file were i crop face with bounding rectangles ( for exapmle img\image1.jpg 1 10 10 200 200 )

so, i want to know — must be some background only in original image?

or i must crop face and some background with bounding rectangles ( for exapmle img\image1.jpg 1 0 0 210 210 )?


Hi. I wanna create a database for smile detection. For this purpose, should I include non-smile mouth images in negative images set?

Hi Reisanibal,

I don’t think that haar training is the best approach for this task. Smile and none-smile are too similar classes. You could have a lot of false positives with Viola-Jones approach.


Hi, thanks for the reply. Could you suggest a better approach for smile detection than haar training?
Thanks again.

I had a question, why should time is double each stage when executed “haartraining.exe”.

hello ! I had created my classifier,but when I wanted to run it I get strange Error massage :”OpenCV Error: Unspecified error (width must be positive integer and (x + width)
must not exceed window width. (stage 0, tree 0, node 0, rect 0)) in cvReadHaarCl
assifier, file ..\..\..\src\cv\cvhaar.cpp, line 1988″
please help me …

xml of cascade has sx sy and <_>a b c d e parameters. In this example (a+c) must be < = sx and (b+d) must be <= sy. The error, you mention, appears if (a+c) > sx. Of course, any valid cascade meets these conditions. Are you sure you have valid cascade? You can check xml manually.

Hi Andrey

I have the same error as Fafa but i don’t understand what you said please give some more details and what you said about a,b,c,d,e parameters

I got this error when I created and used an intermediate xml file with the wrong width and height settings from what was being training e.g.

convert_cascade –size=”41×110″ haarcascade_Qhearts haarcascade_Qhearts-inter.xml

…though training with -w 16 -h 44

for train a Haarcascade I need the positive.vec file (created from the positive.txt) and the negative.txt description file.
Am I right?

Now I would like to create a vec file from more than 1000 positive images. The createsamples utility just creates a vec file for a maximum of 1000 samples. Is it possible the create a vec file with more than 1000 samples?

Try ‘-num 2000’ parameter. 1000 is just default value.

Of course … I didn’t notice this parameter.
Thank you Andrey

Second question:
Whats the difference between haartraining and traincascade?
Is it true that traincascade needs the output from haartraining as input?
First I thought they have the same usage …
So long

haartraining is legacy from OpenCV 1 and traincascade is new object-oriented implementation with c++ functions of OpenCV 2. There are some differences in format of intermediate files (and bugs), but they are almost the same.

Are there any differences between the implementation of the cascade (output) from OpenCV 1 and OpenCV 2?
In my first program I implemented the haarcascade.xml from OpenCV 1 … it runs very good. Yesterday I wanted to implement the cascade.xml from OpenCV 2 … I just got an OpenCV GUI Error message … “Unspecified error (The node does not represent a user object (unknown type?) in function cvRead, .\cxpersistence.cpp(5061)”
Is it generally possible to generate a cascade with OpenCV 2 but use it for OpenCV 1?

I found an example program on http://opencv.willowgarage.com/wiki/FaceDetection
but I didn’t get it started. It compiles and builds with no errors and no warnings. I just got the message “Usage: facedetect –cascade=\”\” [filename|camera_index]”.
Didn’t he find my webcam?

OpenCV 1 and 2 use same format of cascades. “The node does not represent a user object” appears if you don’t prepare FileNode when call cvRead. Do you call cvRead in your program? Could you share part of your code where you declare variables for this function?

“Usage: facedetect cascade=” appears if you don’t add “–cascade=”your_cascade.xml”” parameter when you start this sample. What command do you use to start the sample?

EDIT (I was not exactly right with my answer): OpenCV 2 added cv::HaarClassifierCascade class in addition to cvLoadHaarClassifierCascade and cvHaarDetectObjects functions. This class works with the same xml cascades as legacy functions. These cascades can be trained with haartraining application.

But besides, OpenCV 2 added class CascadeClassifier which works with xml of different format and has slightly different functionality (they added other features except haar and removed some flags of detection). These cascades can be trained with traincascade application.

New-style CascadeClassifier can load old cascade but in this case it’s only wrapper for old-style functions. If new-style cascade is loaded, CascadeClassifier will treat it by its own new code.

Hey Andrey, I tried to solve the problem with creating an image and release it afterwards, but it doesn’t work:
IplImage* temp = cvCreateImage(cvSize(8,8), IPL_DEPTH_8U, 1);

It seems to be a bug (see these links):

The error appears by loading the cascade:
CvHaarClassifierCascade *cascade;
char *cascadeName = “haarcascade.xml”;

// load the classifier
cascade = ( CvHaarClassifierCascade* )cvLoad( cascadeName, 0, 0, 0 );

// initialize memory buffer for the face detector
storage = cvCreateMemStorage( 0 );
cvLoads loads object from file. It provides a simple interface to cvRead. After object is loaded, the file storage is closed and all the temporary buffers are deleted. Thus, to load a dynamic structure, such as sequence, contour or graph, one should pass a valid destination memory storage to the function.

p.s. What means FileNode? :[

I don’t use any command, because the console close after compiling the program.

facedetect.exe –cascade=”haarcascade.xml”

I got it and it works ^^

It seems to be a bug … see these links

I tried to create a image and release it afterwords, but it did work =(

I am using haar training for various hand gestures detection………… I receive the following error………… can anybody help plz …………. I have positive images = 2000 and negative images = 500

Parent node: 15

*** 1 cluster ***
POS: 1990 2000 0.995000
NEG: 402 1.56032e-007
Required leaf false alarm rate achieved. Branch training terminated.
Total number of splits: 0

Tree Classifier
| 0| 1| 2| 3| 4| 5| 6| 7| 8| 9| 10| 11| 12| 13| 14| 15|


OpenCV Error: Unspecified error (Could not open the file storage. Check the path
and permissions) in unknown function, file ..\..\..\..\ocv\opencv\src\cxcore\cx
persistence.cpp, line 4765

This application has requested the Runtime to terminate it in an unusual way.
Please contact the application’s support team for more information.

Normaly the program ends and creates the output haarcascade.xml file, if it oversteps the false alarm rate… It seems that the program can’t create this haarcascade.xml file.

Your directory structure should loks like this:


Hi yasir,

i am also working on hand gesture recognition using haar training. I need some help regarding that. I will appreciate if you could help me.


Please feel free to contact me at maniksinghal.nsit@gmail.com


I am working on haar training of different hand gestures………… how multiple cascades are loaded into the system simultaneously, and each cascade is responsible for a specific hand posture. Is there anyway to do it……….. plz help me out in this


I got an OpenCV GUI Error message … “Unspecified error (The node does not represent a user object (unknown type?) in function cvRead, .\cxpersistence.cpp(5061)”
how can i get rid of this problem plz ??+ Ididn’t call cvread before

Could you clarify, do you use sample program from OpenCV (haartraining.exe for example) or write your own program. Please, provide command line in 1st case and part of your code with call of cvRead or cvLoad or .load and declaration of its arguments in 2nd case.


I suspect you encounted the issue with non-initialization of static haar_type variable inside harr.cpp. During initialization of haar_type variable, some important information is registered, that is used later to help parse XML file during cvLoad call. Depending on OpenCV version, operation system and static vs dynamic library linkage, it might happen that this static variable is not initialized before cvLoad is invoked, and then you may get this error.
To resolve the issue, you need to invoke some function in object detection library *before* invoking cvLoad for haar cascade file. This forces initialization of all static variables including haar_type.
cvReleaseHaarClassifierCascade with pointer to empty cascade worked fine for me.

CvHaarClassifierCascade * cascade = 0;
cascade = (CvHaarClassifierCascade) cvLoad(path);

Best regards,

Hi all!!!

I implemented a face recognition system based on OpenCV HaarCascadeClassifier and eigenfaces. Now I want to review the code and use new functionality of OpenCV.

I start to read and I found the next text:

flags – This parameter is not used for new cascade and have the same meaning for old cascade as in function cvHaarDetectObjects.

It’s mean that cascades trained with the new traincascade app doesn`t supprot this flags? Some of them are very usefuls sometimes.

Thanks for your answers!

Unfortunately, you are right, cv::CascadeClassifier with cascade trained by traincascade doesn’t support these flags. As sample cascades provided by opencv are in “old” format, I believe it’s better to use haartraining with cvHaarDetectObjects or cv::HaarClassifierCascade::detectMultiScale (its OOP form) if you needn’t new features of “new” cascades.

Dropping a note… I’ve been studying this haar-training for some time and it’s probable that some of the things written here may not be that accurate. I don’t intend to spoonfeed anyone but there are some things to consider, as is known already the final false-alarmrate is also dependent on the number of stages. There are more parameters that may or may not have effect on some final variables.

If you don’t get any fairly working cascades try using default parameters for the training, see what it does and what it doesn’t and perhaps draw some conclusions. Let it run for a couple of hours at least.

Make use of the feature that it allows to continue a training-sequence, if you get too many false positives make it run (continue) for one or more stages.

You can also compare the cascades included and the ones you create yourself, using IE e.g.



I’m trying to make LOGO detection. But it doesn’t work at all.

As long as it’s just simple company logo, i took only positive 14 images of logo.
Ratio between width and height were all same.
And logo’s pictures are all taken with same direction. (without any angle)
logo is really simple. its “AXE” on black background.
And negative images are 101 images which are all 640×480 and black and white.
my opencv version is 2.1.0

I tried this.
1. 14 positive images with background. (logo was just part of image)
-> It didnt work so I changed 14 positive images having only logo without any background->but doesn’t work as well.
2. opencv_createsamples -info positivelist.txt -vec sample1.vec -w 8 -h 25 -num 14
3. opencv_haartraining -data haarcascade -vec samples1.vec -bg negativelist.txt nstages 9 -nsplits 2 -minhitrate 0.997 -maxfalsealarm 0.5 -npos 14 -nneg 101 -w 8 -h 25 -nonsym -mem 1024 -mode ALL
(I tried to change nstages from 9 to 20 but same. doesnt work)
(Also I tried to change minhitrate from 0.995 to 0.999 but all doesn’t work)
(Also i tried to change w and h with many values with same ratio but all doesn’t work)
4. and with xml file i tried to detect from flash program(swf) with webcam, but it never detect… (web cam resolution was set to 220 x 180)

in 2 ~ 3 stage, it always log.
“Required leaf false alarm rate achieved. Branch training terminated.”

this is very simple logo “AXE” so I dont think problem is coming from not enough number of positive sample.

Is there someone who tried to detect logos?

I really tried all possible way except increasing the number of positive sample.
Please give me some comment.

Thank you for reading…and thanks in advanced for your answer.

Hi MJ.Kim, why do you use “-w 8 -h 25” parameters, do you have vertically oriented “AXE”? Actually the algorithm is not very good for logo recognition. It likes smoothed forms, imagine picture of a face in small resolution. So the word is not simple for this algorithm. Nevertheless it should be quite simple to get cascade with big number false positives (when it says “logo” but there is no logo actually). If it founds nothing, first of all, try your cascade on training set. Did you try opencv_performance with training set as test set?


I am new to OpenCV and trying to detect facial features like smile / anger. I have begun by Harr-training of faces. I am stuck up as I get “cannot open info.txt file” , which is my positive image info file, while using the createsamples command. You have mentioned that positives dont need to be of the same size. I am working with a very small data set(total 10 positive images). I have verified my info.txt file but I dont understand why this command does not work.

Can you please help me ? Also I read in one of your posts that Haar-training is not a good idea for facial feature detection..Is this true ?

Could you provide few rows of your info.txt and command line you use to start createsamples? You should use something like this:

info.txt file:
pos/10003_frame.png 1 0 0 500 300
pos/10010_frame.png 1 0 0 500 300
pos/10021_frame.png 1 0 0 500 300

and the command line:
createsamples -info info.txt -vec pos.vec -w 25 -h 15

Hello everybody. I have a problem with file creatsamples.cpp. I compiled
createsamples.cpp and no errors. when I run creatsamples.exe, dos screen appears then quickly disappears. I get an message

“The thread ‘Win32 Thread’ (0x9e8) has exited with code 0 (0x0).
> The thread ‘Win32 Thread’ (0x124) has exited with code 0 (0x0).
> The program ‘[2592] creatsamples.exe: Native’ has exited with code 0 (0x0).”

contents of the screen only is:
” [-info ]
[-img ]
[-vec ]
[-bg ]
[-num ]
[-bgcolor ]
[-inv] [-randinv] [-bgthresh ]
[-maxidev ]
[-maxxangle ]
[-maxyangle ]
[-maxzangle ]
[-show []]
[-w ]
[-h ]

Please help me. Thank you very much

See http://opencv-users.1802565.n2.nabble.com/Problem-with-createsamples-cpp-td5611640.html. I believe, it’s a good answer there by smuscroft:

Looks like you’re not providing any arguments to the program…commonly the program is run from the command line such as
createsamples -info samples.dat -vec samples.vec -w 20 -h 20
or you could include the arguments in the project settings…


I am having trouble understanding how to determine the values for -w and -h :

createsamples -info positive_description.txt -vec samples.vec -w 20 -h 20

How are the values “-w 20″ and -h 20” determined?

Here is my situation:

example positive Image details
name: pos0001.bmp
width: 320 pixels
height: 240 pixels

In my description file for my positive images, the line for the above image look like this:
C:\BMPpositve\pos0001.bmp 1 17 81 232 64

This means that the bounding box that surronds the object of interest is of the following size:
width: 232
height: 64

232 divided by 64 = 3.625

Above it is stated that -w and -h are just aspect ratios? What will be the appropriate values in my case?

Here are other lines from my description file:
c:\BMPpositve\pos0002.bmp 1 54 82 228 70
c:\BMPpositve\pos0003.bmp 1 56 86 162 58
c:\BMPpositve\pos0004.bmp 1 46 94 141 50
c:\BMPpositve\pos0005.bmp 1 44 98 137 45
c:\BMPpositve\pos0006.bmp 1 38 99 127 44
c:\BMPpositve\pos0007.bmp 1 11 64 279 80
c:\BMPpositve\pos0008.bmp 1 11 65 242 79
c:\BMPpositve\pos0009.bmp 1 22 68 227 76
c:\BMPpositve\pos0010.bmp 1 15 70 222 74
c:\BMPpositve\pos0011.bmp 1 14 76 204 63
c:\BMPpositve\pos0012.bmp 1 15 79 191 63
c:\BMPpositve\pos0013.bmp 1 18 82 177 57
c:\BMPpositve\pos0014.bmp 1 9 74 153 66
c:\BMPpositve\pos0015.bmp 1 6 73 186 63
c:\BMPpositve\pos0016.bmp 1 11 76 213 64
c:\BMPpositve\pos0017.bmp 1 31 116 157 41
c:\BMPpositve\pos0018.bmp 1 41 113 147 41
c:\BMPpositve\pos0019.bmp 1 16 118 143 38
c:\BMPpositve\pos0020.bmp 1 19 131 86 26

First of all choose -w and -h parameters. See next paragraph how to choose it. In the second place make all bounding boxes in your description file of the same ratio as ratio of -w and -h. It may mean that you need to crop some of your current boxes and expand the others. Don’t use bounding boxes of different aspect ratios as they will be scaled and proportions will be distorted hence algorithm will never see in nature object it was learned.

As for value of -w and -h, all your images from description file will be cropped by bounding box and rescaled to -w -h. If you choose small size, it can be impossible to recognize object by small picture. But if you choose big size, it can be impossible for the algorithm to distinguish meaningful features of object and random noise. So quality of recognition depends on these size significantly. You should choose it by trial and error method. For face recognition sizes of 20×20 or 24×24 are commonly used. Start from something like this. Make sure that you can (by your eyes) recognize the object downscaled to chosen size. Don’t use size bigger than necessary.


I am using haartraining to test a classifier for face detection. My training set consists of ~4k POS and 4k NEG (POS is of size 32×32) (INRIA and MIT datasets). I trained 17 stages using h = w = 35. When I then ran the opencv_performance.exe on it (both cascade and xml result), it only showed 100% missed and no hits or false detections. I then created a smaller set of 100POS and 100NEG and tried w=h=35, 20 and I also got the same result. For this case, at the end of the haartraining this was displayed:
Cascade performance:
POS: 100 100 1.00000
NEG: 100 5.59081e-007

I have tried to do it using two different builds of opencv and got the same results. I have tried to use the performance tool on both training and testing data and both got the same results. I have also downloaded and compiled the performance.c file and got the same results. Does anyone know what could be the problem?

Hi, Ted. It should work, at least for testing on training set. Could you provide exact command lines you use to create vec file with positive images, train cascade and check results? Could you copy several lines of file describing your positive images? May be, there is some error in parameters.

Hi. I also meet this problem, all are miss and no hit and no fault in my performance.exe. The commad I use is “performance.exe -data trainout.xml -info samples.txt -w 20 -h 20 -ni”. sample.txt is like this:
vehicle\100.bmp 1 2 2 20 20
vehicle\101.bmp 1 2 2 20 20
vehicle\102.bmp 1 2 2 20 20
vehicle\103.bmp 1 2 2 20 20
vehicle\104.bmp 1 2 2 20 20
vehicle\105.bmp 1 2 2 20 20
Is there something wrong here? THX!

I am working on traninig rotated faces. I have frontal face images and I rotated them using opencv warpAffine funtion by 45 degree. I have 2400 original samples and I create 2 samples per original sample using opencv_createsample utility. Finally I have a total of 7200 positive samples and 4000 negative samples. Now my questions is as follows:

When we rotate there is a clear rectangle border present for each positive sample, will this effect the training? I filled the background with my negative sample images but my training is terminating after 16 stages saying required false alarm rate achieved. Any suggestions how to train the faces rotated by 45 degree.


Hey…after executing performance.exe it does not show anythin as not even an error. I have my .xml document available. Can u please help me as to why i am not able to run this.

Hi, Any. What does you mean saying “does not show anything”? At least, performance.exe prints out small help with set of available parameters. You need to execute the program, for example, from cmd console to see output of the program. Do you do it this way? If no, just click Start, Run and type ‘cmd’. Then type performance.exe without parameters and see list of needed parameters.

Hello, I’d like to get your advice and maybe you can help me to understand what mistake I make. I stuck with Haartraining function. I have generated delnas.vec file. My decription file looks like:

C:\Nuotraukos\Delnas\Neigiamos\nuotr265.jpg 1 170 100 200 200
C:\Nuotraukos\Delnas\Neigiamos\nuotr266.jpg 1 150 80 250 280

The main my work directory is C:\Nuotraukos. I moved there all neccessary files: opencv_haartraining.exe, delnas.vec, duomenys.txt…
After I run the command:
C:\Nuotraukos>C:\OpenCV2.2\bin\opencv_haartraining.exe -data C:\Nuotraukos -vec
delnas.vec -bg duomenys.txt -npos 519 nneg 1081 -nstages 20 -mem 1500 -mode ALL
-w 20 -h 20

(for haartraining.exe file I had to specify the whole dirtectory to Opencv2.2: C:\OpenCV2.2\bin\opencv_haartraining.exe, otherwise I get an error that msvcp100.dll is missing despite the fact that I moved opencv_haartraining.exe to C:\Nuotraukos). So after I get an error:

Height: 20
Applied boosting algorithm: GAB
Error (valid only for Discrete and Real AdaBoost): misclass
Max number of splits in tree cascade: 0
Min number of positive samples per cluster: 500
Required leaf false alarm rate: 9.53674e-007

Tree Classifier
| 0|

Number of features used : 67074

Parent node: NULL

*** 1 cluster ***
POS: 519 519 1.000000
Invalid background description file.

Do you know what it depends on? Please reply back to me asap directly to my e-mail: anastasija.kac@gmail.com

I am working on traninig rotated faces. I have frontal face images and I rotated them using opencv warpAffine funtion by 45 degree. I have 2400 original samples and I create 2 samples per original sample using opencv_createsample utility. Finally I have a total of 7200 positive samples and 4000 negative samples. Now my questions is as follows:

When we rotate there is a clear rectangle border present for each positive sample, will this effect the training? I filled the background with my negative sample images but my training is terminating after 16 stages saying required false alarm rate achieved. Any suggestions how to train the faces rotated by 45 degree.



I got a VS2008 on an xp SP3 and installed opencv2.1 on it. I have been trying to run the facedetect.c from the C:\OpenCV2.1\samples\c, the default location. I ran the executable and it worked fine. But when i started a new VS sln with the code just pasted in it with no modifications, there seems to be a set of run time errors i run into. I understand that this must be due to the wrong referencing of the xml files. Sometimes i get the “Unhandled exception at” and it redirects to the malloc.c with some prob in systemheap or heap alloc. I am not sure of what the memory allocation prob is. It always starts at the


function and hence i understand that the xml file is not loaded and name is incorrectly read. As i step through the code, i see that the “cascade” info is not populated at all and hence the error. if i comment out this line and run it the program just captures and as expected, it doesnt do any face detection. I wnt through
webpage but the dummy code was of no use as it dint work.( i realised it was not for opencv2.1 version)

Also above in of of the comments by Bob the solution was to create and release, which is already don in the 2.1 version i think as if i go into the funtion in the class, it starts off wit oldcascade.release()
When i run the prog as directed from the command prompt i get some cxpersistence run time error on line on line 2568 which turns out to be a Null file name as guessed. So I dont know how to get the prog to interact with the xml.

PS: i tried
I have the xml file in all the associated folders and where the executable is so i have it in the default dir and sevral other directories.

Any info, help and advice is greatly appreciated.



Right now, to create the positive description file a rectangular bounding box is used. Do you know if it is possible to use a different type of bounding shape for example a “U” shape or a “star” shape?

I want to detect an object that has a “U” shape and after detecting it I was to check the properties of the pixels that are located in the “valley region” of the “U”. How do I go about doing this?

Please let me know if my question needs more clarification.

Thank you.

Great article! how hard would it be to detect a face at 90 degrees?

Hi Jack, as mentioned in article above in section “Is it possible to detect rotated faces?”, you can detect face at 90 degree (as a separate cascade). However you can’t detect face in all orientations with single cascade using this method.

We’re getting an error in the training phase of the haar training.
This is the result of the haartraining command:

“Data dir name: data/cascade
Vec file name: data/positives.vec
BG file name: negatives/train.txt, is a vecfile: no
Num pos: 7
Num neg: 7
Num stages: 5
Num splits: 1 (stump as weak classifier)
Mem: 1300 MB
Symmetric: TRUE
Min hit rate: 0.995000
Max false alarm rate: 0.500000
Weight trimming: 0.950000
Equal weights: FALSE
Mode: ALL
Width: 20
Height: 20
Applied boosting algorithm: GAB
Error (valid only for Discrete and Real AdaBoost): misclass
Max number of splits in tree cascade: 0
Min number of positive samples per cluster: 500
Required leaf false alarm rate: 0.03125

Tree Classifier
| 0|

Number of features used : 67074
OpenCV Error: Unspecified error (Unable to read negative images) in cvCreateTree
CascadeClassifier, file C:\User\VP\ocv\opencv\apps\haartraining\cvhaartraining.c
pp, line 2420

This application has requested the Runtime to terminate it in an unusual way.
Please contact the application’s support team for more information.”

If anyone knows a solution to this, please revert back asap.

What is the format of you negative images?

Hey, I’m getting the same error OpenCV Error: Unspecified error (Unable to read negative images) in function cvCreateTreeCascadeClassifier, C:\User\VP\ocv\opencv\apps\haartraining\cvhaartraining.cpp(2282).
Do you know how can I solve this OpenCV GUI Error Handler??????


I recently said this video of the work of Zdenek Kalal (http://info.ee.surrey.ac.uk/Personal/Z.Kalal/tld.html ). He created TLD which is able to learn and track an object at the same time. For object tracking in OpenCV, is there a way of putting a boundary rectangle around the object of interest and automatically track and generate the description file?

I don’t know how normalized image and create the positive description file?
Please, help me to solve this problem? I am really very grateful


I am making a Hardware implementation of Viola jones and it’s very important to know the exact way to use the threshold value.

I read that the variance normalization is suggested by Viola and Jones as a mean of reducing that effect of different lighting conditions.
So, for to realize an implementation of Viola and Jones, the threshold value given in the .xml is multiplied for the standard desviation, according to the code performance.cpp.
So i perceive is — Calculate variance – calculate Standard deviation from it – and multiply it with the Threshold and then compare it with the normal feature value:

if(feature value < threshold*Standard desviation) then
left value given in the .xml
right value given in the .xml

I'm not sure….Someone can explain it better? and Why the threshold is multiplied for the standard desviation??


I have already send the same message on an other page of this sit but maybe some one can help me here too.
I’m working with opencv on Windows for the start of a project but I wuold like to pass on ARM platform or FPGA for embedded aplications.
I want to make shape recognition on vidéos with high raisolution so I need platform with good performances in this field, and of course compatible with opencv.
So I thought Tegra2 (dual core) from nividia or TI OMAP4430 (dual core) from Texas Instruments but I don’t know if I can instaler opencv on it.
I would like to know if the two platforms are compatible with opencv, if there were other possibilities and what should I check before making my choise.

Thank you for everything.

I have a problem running opencv_haatraining…I have 873 positive images and about 200 negative images….All negative images are big usually 1280*1024…(-nneg is about 1500)
When I run opencv_haartraining after a while it just stops…It works normal for few hours and then it stops at this line

Parent node: X

*** 1 cluster ***
POS: 864 875 0.994942

There is to percentage shown as it usually is, and line NEG: is not showing…
I saw at some previous posts that you suggested to try different or bigger negatives…I tried that all but it still doesn’t work…
Can anyone please answer this question it is really important…
Thank you…

OK now I set -nneg 1 and it works, but when i put -nneg 10 i still doesn’t work…Now I have about 500 negative images in negs.txt…
What is the problem….Why can’t I set -nneg at some normal value

I want to detect character of license plate using adaboost + haarlike features. createsamples -info pos.txt -vec pos.vec -w 20 -h 20. I want to detect object with small size. I write file pos.txt: pos\1.jpg 1 0 0 20 20. Should I crop all image of object into square (410×410,900×900)? After that, I resize all them into 20×20 and convert into gray image.
Please give me some advice!

Does anybody know why I get an error while running haartraining.exe?

Error: Invalid background description file

Good information. With the help of your site and two others, I was able to create an xml classifier in Ubuntu.

I also made a tutorial, you can have a look at it, here


Hello, i’m triying to implement haar training for objects using a smartphone as target…

iḿ working with Opencv 2.1 in ubuntu

i have the negative images in a folder named “negativas” with a background file named “negativas.txt”

it looks like this:




also i have a folder for postive images named “positivas” and a positive images named “positivas.txt”

it looks like this:

pictures1.bmp 1 222 94 241 345

pictures10.bmp 1 217 79 207 340

pictures100.bmp 1 207 7 217 429

both folders are in a train folder

the route is “odemariscasa@ubuntu:~$ cd Escritorio/train”

to generate the vec file i did this:

opencv-createsamples -info positivas/positivas.txt -vec muestra.vec -num 1492 -w 20 -h 20

no problem until now:

Info file name: positivas/positivas.txt
Img file name: (NULL)
Vec file name: muestra.vec
BG file name: (NULL)
Num: 1492
BG color: 0
BG threshold: 80
Invert: FALSE
Max intensity deviation: 40
Max x angle: 1.1
Max y angle: 1.1
Max z angle: 0.5
Show samples: FALSE
Width: 20
Height: 20
Create training samples from images collection…
Done. Created 1492 samples

now iḿ about to train the data:

I used this command

opencv-haartraining -data data/cascade -vec muestra.vec -bg negativas /negativas.txt -nstages 30 -nsplit 2 -minhitrate 0.999 -maxfalsealarm 0.5 -npos 1492 -nneg 5147 -w 20 -h 20 -mem 4000 -mode ALL

and i get this error:

Data dir name: data/cascade
Vec file name: muestra.vec
BG file name: negativas/negativas.txt
Num pos: 1492
Num neg: 5147
Num stages: 30
Num splits: 1 (stump as weak classifier)
Mem: 4000 MB
Symmetric: TRUE
Min hit rate: 0.999000
Max false alarm rate: 0.500000
Weight trimming: 0.950000
Equal weights: FALSE
Mode: ALL
Width: 20
Height: 20
Max num of precalculated features: 105294
Applied boosting algorithm: GAB
Error (valid only for Discrete and Real AdaBoost): misclass
Max number of splits in tree cascade: 0
Min number of positive samples per cluster: 500
Required leaf false alarm rate: 9.31323e-10

Tree Classifier
| 0|

Number of features used : 67074

Parent node: NULL

*** 1 cluster ***
POS: 1492 1492 1.000000
Invalid background description file.
segmentation fault

but i really dont know what i must fix in the background file to make this work.
could you tell me what iḿ doing wrong? please send me the answer to fractal_behavior@hotmail.com or ccventes@hotmail.com

I am working with Haar Classifiers in openCV and could not understand the meaning of some of the switches that are provided in the opencv_haartraining utility like -nneg and -npos. Is the number of images specified here (say 2000) leads to selection of 2000 new images for every stage of the cascade or does the cascade simply pick 2000 negative image samples and use them for training throughout.

Also, I’ve positive images which are 24 × 24 in size and around 100k negative images of the same size.

Sorry, I should have double checked on my search results before posting.
http://note.sonots.com/?cmd=htmlinsert&page=haartraining clearly says that this number is number of images used per training stage.

Hi anybody, I do with haar training but when it reach to 8 satges, the cmd window force close. I don’t know what the problem is. Hope someone can help me. Thank you so much.

Write a comment