Train yourself to Haar-like feature classifier and identify objects (1) _ Training

Source: Internet
Author: User
Tags prepare rand

This series of articles is designed to learn how to train your classifier based on the Haar-like feature in OpenCV and use that classifier for pattern recognition. The process can be roughly divided into a few big steps:

1. Prepare the training sample picture, including the positive example and the counter sample

2. Generate Sample Description file

3. Training samples

4. Target Recognition

=================

This article mainly describes step 1, step 2.

1. Prepare the training sample picture, including the positive example and the counter sample

1) Sampling of positive samples:

A positive sample is a picture that contains only objects to be identified, usually some local pictures, and preferably converted to grayscale. For example, if you want to recognize a person's face, the positive sample should contain only the human face as much as possible, leaving a little surrounding background but not too much. In the sample collection, we have two kinds of graphics calibration tools to use: (1) OpenCV Imageclipper (2) Objectmarker. Both tools support a fool-type image of the object in the rectangular calibration, you can automatically generate a sample description file, automatically frame the next Frame reading folder. I'm using a objectmarker. If you can't find this software, you can leave a mailbox and I'll send it to you.
In the calibration of the time to maintain the same length and width, that is, as far as possible with close to square rectangle to identify the object to be identified, as the size of the square has little effect. Although the optimal size of the OPENCV recommended training sample is 20x20, it is easy to scale the other dimensions to 20x20 in the next step when generating a sample description file. After the calibration is completed, the sample description document Info.txt The following examples:

1 2 3 4 5 rawdata/(1). BMP 1 118-Bayi rawdata/(a). bmp 2 MB 0 rawdata/(one). BMP 1 105 87 43 rawdata/(a). BMP 2 1 70 34 38 105 87 41 44 ...

The RawData folder contains all the large images to be calibrated, ObjectMarker.exe with the RawData folder. The format of this description file is already very close to what OPENCV requires.

2) Collection of negative samples:

A negative sample is any picture that does not contain objects to be identified, so you can take the sky, the beach, the Dashan and everything as negative samples. However, many times you do it with a lot of effort. Most pattern recognition problems are used in the field of video surveillance, camera angle and height are relatively fixed. If you know what your camera is doing in your project, the negative sample can be very targeted and can be used with less effort. For example, you now want to do anomaly detection in the railway station Square, where pedestrian testing is a must. And the background of the video frame is basically the floor of the square, building and so on. Then you can choose to take a picture when the person is empty, different illumination at different time each take a picture, and then on these graphs randomly take image block, each block 20x20, each block is a negative sample. These pictures can be wrapped around thousands of negative samples. and targeted strong. Because the ocean, Dashan and other things to your identification a little help, but also will increase the training time, thankless thing or less to do as well. I wrote a small program that automatically randomly generates a negative sample of a specified number of specified dimensions based on the background picture:

#include "stdafx.h" #include "cv.h" #include "highgui.h" #include <iostream> #include <string> using Namespac
e std;

using namespace CV;                The image block is randomly extracted from the background picture, which is used to generate negative sample #define KIMAGEBLOCKWIDTH 40//image block size #define Kimageblockheight #define KLOOPTIMES 1000//Expected sample number int _tmain (int argc, _tchar* argv[]) {int Ori
    Ginx = 0, originy = 0;
    int width_limited = 0, height_limited = 0;
    int width = 0, height = 0;
    Iplimage *bgimage = cvloadimage ("Neg\\bg1.bmp"); Iplimage *blockimage = Cvcreateimage (Cvsize (Kimageblockwidth, Kimageblockheight), Bgimage->depth, bgImage->
    Nchannels);
    width = bgimage->width;
    Height = bgimage->height;
    width_limited = Width-kimageblockwidth;
    height_limited = Height-kimageblockheight;
    cout<<width_limited<< "" < 


The negative sample size here is set to 40x40 because the object to be identified in my application is almost this size. You can analyze your Info.txt file specifically. After the file is generated, open the Cmd.exe CD to the directory, then run dir/b > Neg_sample.dat, open. Dat, replace BMP with EditPlus with BMP 1 0 0 40 40. This negative sample description file is produced.

For negative samples, I also have a point to note: Negative sample image size as long as not less than the positive sample can be. OpenCV, when using a negative sample image you provide, will automatically pull out an image of the same size as the positive sample as a negative sample, and the specific function can be seen OPENCV system function cvgetnextfrombackgrounddata ().

2. Generate Sample Description file

The sample description file, also known as the. vec file, contains binary data that is prepared for OPENCV training. Only positive samples are required to generate. vec files, negative samples are not needed, and negative samples are sufficient for. dat files. In the process of generating the description file, we need to use the OpenCV opencv_createsamples.exe executable file. This file is generally stored in the/bin folder of the OpenCV installation directory (please use Ctrl+f search). If not, you can compile it yourself again soon. Here is the lazy version: http://en.pudn.com/downloads204/sourcecode/graph/texture_mapping/detail958471_en.html This is someone else's compiled OPENCV project , the exe file can be found underneath the bin. Note that the EXE relies on the three dynamic libraries of Cv200.dll, Cxcore200.dll, and Highgui200.dll to keep the four files in the same directory.

Now we start to generate a description file. Create a new folder Pos, neg separate the positive samples and negative sample pictures, here refers to the large image without calibration.

1) Modify the format of the sample description file:

In the 1th step we use Objectmarker to complete the calibration will automatically generate Info.txt, now we need to do some fine-tuning of its format, through EditPlus or ultraedit to replace the path information rawdata, and named Sample_ Pos.dat, you can also customize the name. 1 2 3 4 5 6 (1). BMP 1 118-BA (a). BMP 2 Mb 0 (one). BMP 1 2 1 70 34 38 + 105. 1 102-BMP (1 104 86 45 47. bmp)

2 use Opencv_createsamples.exe to create the sample description file:

Open CMD.EXE,CD to Opencv_createsamples.exe directory, execute command: 1 opencv_createsamples.exe <span style= "color: #ff0000;" >- Info ./pos/sample_pos.dat</span>-vec./pos/sample_pos.vec <strong>-num 17</strong>-W 20-h 20-show Y Es

Parameter description:-info, refer to sample documentation

-vec, the name and path of the sample description file

-num, a total of a few samples, to note that the number of samples here is the number of 20x20 after the index, rather than the number of large graphs, in fact, is the sample description of the 2nd column of all the numbers add up and.

-w-h indicate what size you want the sample to scale. The trick here is that you don't have to deal with the size of the picture in the 1th step, which is rectangular, because this parameter helps you zoom in a uniform way.

-show whether to display each sample. Fewer samples can be set to Yes, if the sample is much better set to No, or do not explicitly set, because the closed window will be closed to you cry

Done to create success, if the creation is not successful will be an error, most will prompt you Sample.dat pars error, is generally the description file format error, or num set too large 1 2 create training samples from images Collecti On ..... Done. Created samples

Summarize

Summarize and extend the above content:

1. The sample picture best uses the gray chart, and best can do certain pretreatment according to the actual situation

2. The principle of sample selection is: The greater the number the better, as far as possible above 1000; the greater the difference between samples, the better.

3. Positive and negative sample ratio of 1:3 best, size for 20x20 best

That ' s all.

==================

Attached to the reference, see this is enough, online information too much easy to see eye.

http://blog.csdn.net/think_embed/article/details/9959569

Http://www.docin.com/p-80649093.html

Http://jingyan.baidu.com/article/4dc40848f50689c8d946f197.html

http://blog.csdn.net/carson2005/article/details/8171571

Objectmarker download Link "20151218 Update"

http://download.csdn.net/download/lglgaigogo/1197957


From:http://www.cnblogs.com/wengzilin/p/3845271.html

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.