A demo of Traincascade in OpenCV

Source: Internet
Author: User

1 Training of the classifier

Training Cascade classifier Traincascade requires two EXE files in OpenCV, these two files are Opencv_createsamples.exe and Opencv_traincascade.exe files respectively.

The training process can be divided into the following steps:

(1) Prepare positive and negative training samples.

Positive Sample:

Positive sample size is consistent, we recommend that you write a small program to tailor the image to achieve dimensional unity.

Negative Sample:

The size of negative samples need not be unified, the more negative samples, the smaller the error detection rate of test results.

The demo detects the target is the cloud in the image, the other is the background.

After you have prepared your training samples, create two folders, assume Pos and neg, store positive and negative samples, and place the two folders in the same directory as the Opencv_createsamples.exe and Opencv_traincascade.exe files, as shown in the following figure.

To prevent clutter, these files are under the Unified folder "Yunc". Figure 1 has two folder Pos_new, as well as neg_new, which is a subsequent folder built for the new sample library. Because the general need to deal with more samples, easy to confuse, if the sample is rarely used to cascade.

(2) In the DOS environment generated positive and negative sample descriptor, the specific command screenshot as follows:

First enter the directory where the positive and negative samples are located. In the directory where the positive sample is located, enter the command: Dir/b>pos.txt generates a positive sample description file.

In the case where the negative sample is in the directory, enter the command: Dir/b>neg.txt, then generate a negative sample description file. The generated positive and negative sample description file is a. txt file, which can be seen as a sample name after it is opened, and the positive and negative samples are treated as follows:

For negative samples, the following illustration shows:

In front of the sample name plus "neg/", that is, the negative sample folder, all the sample name to be modified, after the modification of the last line of Neg.txt removed, and the last line of the empty line removed, remember, otherwise the training process will be an error. For a positive sample, the following illustration shows:

In addition to the name of the folder in front of the sample name, also after the suffix name plus a sample description, that is, the size of the sample, 1 for the sample number 1, 0 0 for the sample in the upper left corner of the coordinates is (0,0), 4 4 for the lower right corner of the coordinates. The Pos.txt and empty lines of the last line are removed after the same modification. At this point, the positive and negative sample description file has been fully processed.

(3) generate. vec files

A Opencv_createsamples.exe file is required to generate the. vec file, and the. vec file is prepared for the following training classifier. In a DOS environment, type the following command in the relative directory:

Opencv_createsamples.exe-vec pos.vec-info pos.txt-num 1467-w 4–h 4

You can see the generated. vec file in the directory where the Opencv_createsamples.exe file resides.

As you can tell from the command line, the size of a positive sample for-W 4–h 4 is also an indirect indication that the size of the positive sample must be the same. -num is the number of positive samples that need to be generated. For negative samples, you do not need to generate. vec files. The next step is to use Opencv_traincascade.exe to start training classifiers.

The following figure indicates the success of the build. Vec file.

(4) The training of Cascade classifier

Type the following command at the command line:

Opencv_traincascade.exe-data Xml-vec pos.vec-bg neg.txt-numpos 900 Numneg 788-numstage 20-precalvalbufsize 300-prec Alidxbufsize 100-featuretype haar-w 4-h 4-mode All

We started training the classifier. Each of these meanings is now Yi Yilai described.

-data XML

XML is the name of the folder, which needs to be created to store a trained classifier. -data represents the folder where the classifier resides.

-BG Numpos 900

This is the number of training positive samples, why this place is not 1467, and the creation of a sample created 1467. This I am not very clear, in short, if you can see the source code to be able to figure out.


This is obviously a negative sample file.

Numneg 788

Number of negative samples.

-numstage 20

Number of training orders. (This place is actually set to 20 does not make sense, because the Cascade classifier's highest training order is level 19)

-precalvalbufsize 300

The cache size, which is used to store the feature values in megabytes (MB).

-precalidxbufsize 100

The cache size, which is used to store a feature feature index (MB), in megabytes (indices). The larger the memory, the shorter the training time.

-featuretype HAAR

Features used for detection are Haar features

-mode All

Select the type of Haar feature used during the training process. BASIC uses only upper-right features, all using all upper-right features and 45-degree rotation features.

The correct training process is shown in the following illustration:

The results of the training are as follows:

After the training is completed, the results of the final training can be found in the XML file under the Yunc file. Open the XML folder as shown in the following illustration:

The XML folder in the graph above is the intermediate process of the Cascade classifier training, and the Cascade.xml file is the classifier of training completion. 2 detection of clouds

The detection process is mainly through the Detectmultiscale () to detect, for the function is how to detect, many literature and technical blog did not have an answer. Therefore, only through the experimental exploration of its detection process.

The declaration that only its original function can be found in OpenCV is as follows:

void Detectmultiscale (const mat& image,

Cv_out vector<rect>& objects,

Double scalefactor=1.1,

int minneighbors=3,

int flags=0,

Size minsize=size (),

Size maxsize=size ());

Where mat& image is the input of mat class images, cv_out vector<rect>& objects containers for the detected targets, double scalefactor=1.1, and int Minneighbors=3 These two parameters are more complex, first introduce the last three parameters, the int flags=0 indicates that the Haar feature is detected, size minsize=size () is the minimum size for detection, size maxsize=size ()) is the maximum size for detection.

scalefactor=1.1 is a scaling factor for multi-scale detection, and when 4*4 detects the target, it continues to expand the detection range.

int minneighbors=3 to continue detection in the detected target area.

Vomiting blood took two days to do the experiment summed up, to now do not understand the relationship between these three (scalefactor,minneighbors, detection of the minimum scale)

On the Multiscale feature detection function detectmultiscale () Detection scale table:



Detect minimum scale
































The minimum detection scale is related to the running rate of the algorithm, and whether the parameters are set reasonably or not affects the final result of the algorithm. The smaller the detection range factor, the longer the detection time, the larger the minimum neighborhood, and the longer the detection time. That is, the detection time is inversely proportional to the minimum range factor, and to the minimum neighborhood.

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: info-contact@alibabacloud.com 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.