Opencv training Classifier

Source: Internet
Author: User
Tags file info

Opencv training Classifier
I. Introduction
The target detection method was initially proposed by Paul Viola [vila01] and improved by Rainer lienhart [lienhart02. The basic steps of this method are as follows: First, use the Harr feature of the sample (about several hundred sample images) for Classifier Training to obtain a cascade boosted classifier.
In a classifier, "cascade" means that the final classifier is composed of several simple classifier Cascade. In image detection, the detected window uses each level of classifier in sequence, so that most of the candidate regions in the previous layers of detection are excluded, all the regions detected by each level of classifier are the target regions.
After the classifier is trained, it can be applied to the detection of the areas of interest (the same size as the training sample) in the input image. The output of the classifier in the target area (car or face) is 1. Otherwise, the output is 0. To detect the entire image, you can move the search window in the image and check each position to determine the possible target. In order to search for objects of different sizes, the classifier is designed to be able to change the size, which is more effective than changing the size of the image to be examined. Therefore, to detect an unknown target object in an image, scan Program You usually need to use a search window of different proportions to scan the image several times.
Currently, four types of boosting technologies are supported: discrete Adaboost, real Adaboost, gentle AdaBoost and logitboost.
"Boosted" means that each layer of the cascade classifier can select a boosting Algorithm (Weighted voting), and obtained through self-training of the basic classifier.
According to the above analysis, the target detection is divided into three steps:
1. Create a sample
2. Train the Classifier
3. Use a trained classifier for target detection.
Ii. Sample Creation
The training samples are divided into positive samples and inverse samples. The positive samples refer to the target samples to be checked (such as faces or automobiles), and the negative samples refer to any other images, all the sample images are normalized to the same size (for example, 20x20 ).
Negative sample
Negative samples can come from any image, but these images cannot contain the target features. Negative samples are described in the background description file. The background description file is a text file. Each line contains a negative sample image file name (based on the relative path of the description file ). This file must be created manually.
E. g: An example of a negative sample description file:
Assume that the directory structure is as follows:
The content of the Background File bg.txt is:
Positive sample
The positive sample is created by the program craatesample. The program's Source code It is provided by opencv and contains this executable program in the bin directory.
A positive sample can be created from a single target image or a series of pre-marked images.
The command line parameters of the createsamples program:
Command line parameters:
-VEC <vec_file_name>
Output file name of the trained positive sample.
-IMG <image_file_name>
Source Target Image (for example, a company icon)
-BG <background_file_name>
Background description file.
-Num <number_of_samples>
The number of positive samples to be produced is the same as the number of positive images.
-Bgcolor <background_color>
Background color (assuming that the current image is a grayscale image ). The background color is transparent. For compressed images, the color variance is specified by the bgthresh parameter. The pixels between bgcolor-bgthresh and bgcolor + bgthresh are considered transparent.
-Bgthresh <background_color_threshold>
If specified, the color will be reversed.
If specified, the color will be reversed.
-Maxidev <max_intensity_deviation>
The maximum deviation of the background color.
-Maxangel <max_x_rotation_angle>
-Maxangle <max_y_rotation_angle>,
-Maxzangle <max_x_rotation_angle>
The maximum rotation angle, in radians.
If this parameter is specified, each sample is displayed. Pressing "ESC" will disable this function, that is, the sample image is not displayed, And the creation process continues. This is a useful debug option.
-W <sample_width>
Width of the output sample (in pixels)
-H sample_height
The height of the output sample, in pixels.
Note: positive samples can also be obtained from a pre-labeled image set. This set is described by a text file, similar to a background description file. Each text line corresponds to an image. The first element in each row is the image file name, and the second element is the number of object objects. The following is the matching rectangle (X, Y, width, height ).
The following is an example of creating a sample:
Assume that we want to perform face detection. There are 5 positive sample image files img1.bmp ,... Img5.bmp; has two background image files: bg1.bmp and bg2.bmp. The directory structure is as follows:
Info. dat
The content of the positive sample description file info. dat is as follows:
Positive/imag1.bmp 1 0 0 24 28
Positive/imag5.bmp 1 0 0 24 28
The image img1.bmp contains a single object, and the rectangle is (, 24, 28 ).
Note: to create a positive sample from an image set, use the-Info parameter instead of the-IMG parameter.
-Info <collect_file_name>
Description file of the image set marked with features.
The content of the background (negative sample) description file is as follows:
We use a batch processing file run. BAT to create a positive sample: the content of this file is as follows:
Cd e: \ face \ bin
Createsamples-vec e: \ face \ A. VEC
-Info E: \ face \ info. dat
-Bg e: \ face \ bg.txt
-Num 5
-W 24
-H 28
The E: \ face \ bin directory contains the createsamples executable program. The generated positive sample file a. VEC is under the E: \ face directory.
Iii. Training Classifier
After the sample is created, train the classifier. This process is implemented by the haartraining program. The program source code is provided by opencv and the executable program is in the bin directory of the opencv installation directory.
The command line parameters of haartraining are as follows:
-Data <dir_name>
Stores the path name of the trained classifier.
-VEC <vec_file_name>
Positive sample file name (created by the trainingssamples program or by other methods)
-BG <background_file_name>
Background description file.
-NPOs <number_of_positive_samples>,
-Nneg <number_of_negative_samples>
A positive/negative sample used to train each classifier stage. Reasonable Value: NPOs = 7000; nneg = 3000
-Nstages <number_of_stages>
Number of training phases.
-Nsplits <number_of_splits>
Determines the weak Classifier Used for the phase classifier. If 1, a simple stump classifier is used. If it is 2 or more, the cart classifier with number_of_splits internal nodes is used.
-MEM <memory_in_mb>
Pre-calculated available memory in MB. The larger the memory, the faster the training speed.
-Sym (default)
Specifies whether the target object for training is vertically symmetric. Vertical symmetry increases the training speed of the target. For example, the front is vertical symmetric.
-Minhitrate: min_hit_rate
The minimum hit rate required by each stage classifier. The total hit rate is the number_of_stages power of min_hit_rate.
-Maxfalsealarm <max_false_alarm_rate>
The maximum error alarm rate for a stage classifier. The total error warning rate is the number_of_stages power of max_false_alarm_rate.
-Weighttrimming <weight_trimming>
Specifies whether or not to use the permission correction and how much to use the permission correction. A basic choice is 0.9
-Mode <Basic (default) | core | all>
Select the type of the Haar feature set used for training. Basic only uses vertical features. All uses vertical and 45-degree rotation features.
-W sample_width
-H sample_height
The size of the training sample, in pixels ). The size must be the same as that created by the training sample.
An example of training a classifier:
In the same example, a batch processing file run2.bat is used for Classifier Training:
Cd e: \ face \ bin
Haartraining-data E: \ face \ data
-Vec e: \ face \ A. VEC
-Bg e: \ face \ bg.txt
-NPOs 5
-Nneg 2
-W 24
-H 28
After training, some subdirectories are generated under the directory data, that is, the trained classifier.
Note: Some versions of opencv can directly convert the classifiers in these directories into XML files. However, in actual operations, the haartraining program never seems to stop, and does not generate an XML file. Later, I found a haarconv program on the Yahoo forum of opencv, the reason for converting the classifier into an XML file is still to be studied.

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.