OPENCV Haar training-Feature Trainer

Source: Internet
Author: User

1. Positive sample File collection:

Tools to use:objectmarker

The label used for the object to be recognized, and generates a callout file. Note that after generating the callout file (usually the Info.txt file), open the file with the editor, delete all the path information, only the file name and its corresponding callout information, the modification is completed, save the file as Sample_pos.dat (note, the file name does not change, Otherwise it will be very pit, this specific has not yet to study the source code, it should be the problem of the program);

PS: If you already have the callout information for the image, you can skip this step.

2. Normal sample Vec file generation (sample file)

Tool: Opencv_createsamples.exe

Create a folder in the directory where the tool is located: POS, the collected positive samples, and the label file into the POS folder;

Use CMD to enter the directory where the tool is located and execute the command:

Opencv_createsamples.exe-info./pos/sample_pos.dat-vec./pos/sample_pos.vec-num 17-w 20-h 20-show YES

Ps:-num: Number of positive samples

3. Negative Sample Collection

It is recommended that the ratio of positive and negative samples be 1:3 (specifically not known, refer to the data provided online). Negative samples in the acquisition of particular needs to be noted, as far as possible to maintain similar to the positive sample environment, for example, the positive sample image contains the object to be detected, negative sample image is best to remove the object to be detected, only the background. Qualified students can write the program directly, the negative sample image to cut, the standard is 40*40 (because our positive sample is 20*20).

After the negative sample acquisition is complete, create the folder in the statistics directory of the POS directory: Neg, copy the negative sample to the Neg directory, and then use the command line to enter the Neg directory and execute the command:

dir/b > Sample_neg.dat

Open dat file with text editor, delete last line: Sample_neg.dat, save file;

PS: Do not change the file name, otherwise it will be wrong

4. Training samples

So far, all the papers we have to prepare are ready for training.

Tools to use: Opencv_haartraining.exe

Go to the directory where the tool is located, first create the directory: Cascade, and then execute the command:

Opencv_haartraining.exe-data./cascade-vec./pos/sample_pos.vec-bg./neg/sample_neg.dat-nstage 20-npos 100-nneg 300 -mem 256-mode all-w 20-h 20
-nstage: How many layers to train
-npos: Number of positive samples per layer
-nneg: Number of negative samples per layer

PS: In this step often throw exception, usually negative sample file is wrong, and DAT file name must be named according to the above, otherwise it will be wrong;

5. Merging XML files

After the sample training is complete, multiple subdirectories are generated under the Cascade directory, and these directories need to be merged to generate an XML file for use:

Tool: C-example-convert_cascade.exe (PS: Depending on the version of OpenCV, the name of the tool may vary)

Execute command:

C-example-convert_cascade.exe--size=20x20./cascade haar_adaboost.xml

Here, the Haar_adaboost.xml file will be generated in the directory!



1. All tools and their dependent files are recommended to be uniformly placed in a directory (e.g. build);

2. In addition to the negative sample description file Generation command needs to be executed in the Neg directory, the other commands are all in the build (assuming you also have this directory, in the respective actual directory) directory to execute;

3.good luck!




(Opencv Error:assertion failed (elements_read==1))

OPENCV Haar training-Feature Trainer

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.