Official Use of training and detection in opencv-opencv_createsamples, opencv_traincascade

Source: Internet
Author: User

I haven't written a blog for a long time, and my student's career ends. I will not summarize it. Today, I will record the Adaboost training and detection process in opencv, so that it is convenient for others ~~~ Ah, haha ~~~~


I. Basic Knowledge preparation

First, opencv currently only supports training and detection of three features: Haar, HSV, and hog. Choose which feature to add. This training algorithm of opencv is based on AdaBoost. Therefore, we need to add the basic knowledge of AdaBoost first. There is a lot of information on the Internet, so we can refer to it quickly. I also have some resources. You can download them. I don't think you can use them directly. I will tell you how to perform the training directly, and what details should be paid attention.


Ii. Preparation of positive samples

1. Collect positive sample images

Because the final size normalization is required for the positive sample, I directly extracted the sample from the source image when collecting the sample, which facilitates subsequent scaling, instead of simply saving the number of boxes and the box location information (the number of boxes and the box location information are described in the next step), we try to keep the sample length/width ratio consistent during the cropping process. For example, I finally want to convert it to 20x20. When cropping samples, I always crop 20x20, 21x21, and 22x22, I have no more than 30x30 at the maximum (not more than it is related to my own purposes. For face detection samples that require constant scaling, they can certainly exceed ), we also provide sample cropping programs that can be used directly in our resources.

2. Obtain the list of positive sample paths

In your image folder, write a bat Program (get route. bat is to avoid entering in the DOS box every time, where you can neither copy nor paste it !), As follows:








Run the BAT file to generate the following DAT file:


Delete all non-image paths in the DAT file. For example, replace the first two rows with BMP 1 0 0 20 20, as shown below:


(1 indicates the number, and the last four correspond to left top width height. If we didn't crop the sample, your dat may grow like this. BMP 3 1 3 24 24 26 28 25 60 80 26 26, 1.bmp is a complete source image. Your previous samples are deducted from this image)

3. Obtain the VEC file for training.

Here, we need to use a program named opencv_createsamples.exe in opencvto copy it. The command input for it is also written as a bat file, because the training for Cascade uses Vec. As follows:


Run BAT to generate the following VEC file in the POs Folder:

Preparations for this positive sample are complete.


Iii. Preparation of negative samples

This is especially simple. You can use the original image directly without cutting the image (the sample diversity can be guaranteed even if the image is not cropped ), there is no need to save the box (as long as the size of the positive sample is larger than that on the Internet), you just need to save the path. Similar to the positive sample, the steps are as follows:



Now, preparations for negative samples are complete.


4. Start Training

The feature contains more features and functions.) directly:


The bat file is also used for command input. Make sure the case sensitivity is the same. Otherwise, the parameter is not recognized. Run up ~~~


This is a parameter recognized by the program. If a letter is wrong, you will find that these parameters are different from your preset ones, so you must have a clear view of it ~~~~

Run, run, run, and run as follows:


The strong trainer at this level will go to the next level after reaching your preset ratio. Do not set the HR ratio too high for comrades. Otherwise, many samples will be required, and stagenum should not be set too small, otherwise, the detection speed will be slow.

When the bat is finished, my XML file is generated. As follows:


In fact, this training can be stopped midway through, because it will read these XML files the next time it starts, and then carry out the last unfinished training. Haha ~~~~ User-friendly!

The training is over. I need my cascade. xml file. Now I want to use it for detection! Call ~~~~


5. Start detection.

Opencvhas an opencv_performance.exeprogram used for detection, but opencv_haartraining.exe can only be used by users. Therefore, I want to detect some column images here. The detection code is as follows:

# Include <windows. h> # include <mmsystem. h> # include <stdio. h> # include <stdlib. h> # include "wininet. H "# include <direct. h> # include <string. h> # include <list> # pragma comment (Lib, "wininet. lib ") # include" opencv2/objdetect. HPP "# include" opencv2/highgui. HPP "# include" opencv2/imgproc. HPP "# include" opencv2/ml. HPP "# include <iostream> # include <stdio. h> using namespace STD; using namespa Ce CV; string cascadename = ". /cascade. XML "; // training data struct pathelem {tchar srcimgpath [max_path * 2]; tchar rstimgpath [max_path * 2];}; int findimgs (char * psrcimgpath, char * prstimgpath, STD: List <pathelem> & imglist); int main () {cascadeclassifier cascade; // create a cascade classifier object STD: List <pathelem> imglist; STD :: list <pathelem >:: iterator pimglisttemp; vector <rect> rects; vector <rect >:: const_iterator prect; double scale = 1 .; MAT image; Double T; If (! Cascade. load (cascadename) // load the cascade classifier {cerr <"error: cocould not load classifier Cascade" <Endl; return 0;} from the specified file directory ;} int nflag = findimgs ("H:/srcpic/", "H:/rstpic/", imglist); If (nflag! = 0) {cout <"read image error! Input 0 to exit \ n "; exit (0);} pimglisttemp = imglist. begin (); For (INT iik = 1; iik <= imglist. size (); iik ++, pimglisttemp ++) {image = imread (pimglisttemp-> srcimgpath); If (! Image. empty () // read image data cannot be blank {mat gray, smallimg (cvround (image. rows/scale), cvround (image. cols/scale), cv_8uc1); // scale down the image to accelerate the detection speed cvtcolor (image, gray, cv_bgr2gray); // because Haar-like features are used, therefore, they are all based on grayscale images. here we need to convert them to grayscale images resize (Gray, smallimg, smallimg. size (), 0, 0, inter_linear); // reduce the size to 1/scale and use linear interpolation equalizehist (smallimg, smallimg ); // histogram balancing // In the detectmultiscale function, smallimg indicates that the input image to be detected is smallimg, rects indicates the detected target sequence, and 1.1 indicates // The size of the secondary image is reduced by 1.1, 2 indicates that each target must be detected at least three times before it can be regarded as a real target (because the surrounding pixels and different window sizes can both detect the target ), cv_haar_scale_image indicates that the image is scaled instead of the zoom classifier. The size (30, 30) is the target // minimum maximum size rects. clear (); printf ("begin... \ n "); t = (double) cvgettickcount (); // used to calculate the algorithm execution time cascade. detectmultiscale (smallimg, rects, 1.1, 2, 0, size (20, 20), size (30, 30); // | bytes | cv_haar_scale_image, t = (double) cvgettickcount () -T; printf ("d Etection time = % G Ms \ n ", t/(double) cvgettickfrequency () * 1000.); For (prect = rects. Begin (); prect! = Rects. end (); prect ++) {rectangle (image, cvpoint (prect-> X, prect-> Y), cvpoint (prect-> X + prect-> width, prect-> Y + prect-> height), cvscalar (0,255, 0);} imwrite (pimglisttemp-> rstimgpath, image);} return 0 ;} int findimgs (char * psrcimgpath, char * prstimgpath, STD: List <pathelem> & imglist) {// directory of the source image tchar szfilet1 [max_path * 2]; lstrcpy (szfilet1, text (psrcimgpath); lstrcat (szfilet1, text ("*. * "); // The result image storage directory tchar RST ADDR [max_path * 2]; lstrcpy (rstaddr, text (prstimgpath); _ mkdir (rstaddr); // create a folder win32_find_data WFD; handle hfind = findfirstfile (szfilet1, & WFD); pathelem stpathelemtemp; If (hfind! = Invalid_handle_value) {do {If (WFD. cfilename [0] = text ('. ') continue; If (WFD. dwfileattributes & file_attribute_directory | strcmp ("thumbs. DB ", text (WFD. cfilename) = 0) {;}else {tchar srcimgpath [max_path * 2]; lstrcpy (srcimgpath, psrcimgpath); lstrcat (srcimgpath, text (WFD. cfilename); lstrcpy (stpathelemtemp. srcimgpath, direction); tchar adresstemp [max_path * 2]; lstrcpy (adresstemp, prstimgpath); // lstrcat (adresstemp, text ("/"); lstrcat (Direction, direction, text (WFD. cfilename); lstrcpy (stpathelemtemp. rstimgpath, adresstemp); imglist. push_back (stpathelemtemp) ;}} while (findnextfile (hfind, & WFD) ;}else {return-1 ;}return 0 ;}

Let's take a look at our testing results. For improved samples with poor results, adjust the training parameters ~~~ GA


I think I have enough idiots to help you use them directly. Here are some details for your consideration ~ 88

Official Use of training and detection in opencv-opencv_createsamples, opencv_traincascade

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.