Object Recognition and scene understanding (6) Target Detection by hog + SVM in opencv

Source: Internet
Author: User
Tags svm

Reference: Pedestrian detection using hog features and SVM Classifier:Http://blog.csdn.net/carson2005/article/details/7841443

 

Hog + SVM has excellent Pedestrian detection effects due to its characteristics, but it also has good effects on other targets. Here we will expand the scope.

 

Carson2005's blog article describes how to use opencv to implement sample training and target detection. Libsvm can also be used for processing.

1. Calculate the weight using libsvm

Http://www.opencv.org.cn/forum/viewtopic.php? F = 1 & t = 9146

 

-------- Calculate the weight using libsvm, that is, detector [] ---------- In opencv

To directly use libsvm, You need to construct the data according to its format. The following describes how to use libsvm in MATLAB.

Download libsvm-mat-2.9-1
Method 1:
Switch to the directory where the libsvm-mat-2.9-1 is located, open MATLAB and type:
Mex-Setup

Method 2: MATLAB menu file --> set path Add the path of the libsvm-mat-2.9-1.
----------------------
The following introduces the heart_scale which comes with libsvm-mat-2.9-1 as an example.

----------- Kernel_type is linear ----------------------------------
Load heart_scale.mat
Train_data = heart_scale_inst (1:150 ,:);
Train_label = heart_scale_label (1:150 ,:);
Test_data = heart_scale_inst (151: 270 ,:);
Test_label = heart_scale_label (151: 270 ,:);
Model_linear = svmtrain (train_label, train_data, '-T 0 ');
[Predict_label_l, accuracy_l, dec_values_l] = svmpredict (test_label, test_data, model_linear );
---------- Obtain the model after training -------

Model_linear =

Parameters: [5x1 double]
Nr_class: 2
Totalsv: 58
ROCK:-1.1848
Label: [2x1 double]
PROBA: []
Probb: []
NSv: [2x1 double]
Sv_coef: [58x1 double]
SVS: [58x13 double]
----------- How to obtain the weight coefficient from the model ----
See the following website for details:
Http://www.csie.ntu.edu.tw /~ Cjlin/libsvm/faq.html # f804

For the following two types of problems, we can solve the linear problem (y = wx + B) by weight coefficient w and B.

W = model_linear.svs '* model_linear.sv_coef;
B =-model_linear.rov;

---------

The obtained W is the detector in opencv []

 

Next I will directly post the article carson2005.

2. Find Detector Using opencv

We have introduced hog feature (http://blog.csdn.net/carson2005/article/details/7782726) and SVM classifier (http://blog.csdn.net/carson2005/article/details/6453502) before.
). The purpose of this article is to introduce the use of hog features and SVM classifier for pedestrian detection.

At cvpr in 2005, French researchers navneet Dalal and Bill
Triggs proposes to use hog for Feature Extraction and linear SVM as classifier for pedestrian detection. Through a large number of tests, we found that hog + SVM is a pedestrian detection method with better comprehensive balance between speed and performance. Later, although many researchers have also proposed many improved pedestrian detection algorithms, the algorithms are basically based on the framework. Therefore, hog + SVM has become an ODPS algorithm written into opencv. In Versions later than opencv2.0, APIs for hog feature descriptive operators are available. SVM has been integrated as early as opencv1.0. Although opencv provides APIs for hog and SVM, we also provide sample for pedestrian detection. Unfortunately, opencv does not provide sample for sample training. This means that many people can only use the trained classifier provided by opencv for pedestrian detection. However, the built-in classifier of opencv uses navneet
The samples provided by Dalal and Bill triggs are not necessarily suitable for your application. Therefore, for your specific application scenarios, it is necessary to re-train your classifier. The purpose of this article is here.

Re-train the pedestrian detection process:

(1) prepare a training sample set, including a positive sample set and a negative sample set. Based on the basic knowledge of machine learning, we know that we need to use machine learning algorithms for sample training, to obtain a classifier with excellent performance, the training samples should be infinite, and the training samples should cover various situations that may occur during actual application. (Many of my friends use 10 positive samples and 10 negative samples for training. Then, they will perform tests. If they find that the results are not as good as they think, they will start to complain, complaints... I am sorry for these people, but you are still in the initial stage of machine learning and pattern recognition.) in practical application, the number of training samples cannot be infinite, however, in any case, three or five thousand positive samples and three or five thousand negative samples are not difficult? (If you cannot even do this, we recommend that you do not engage in machine learning, pattern recognition, or training materials. How can you make the machine learn enough information ?)

(2) After sufficient training samples are collected, You need to manually crop the samples. For example, if you want to use hog + SVM to perform Pedestrian detection on the Pedestrian Street monitoring screen, you should use the collected training sample set, manually crop pedestrians in the screen (you can write a simple program. You only need to select the box and save the selected area ).

(3) After cropping the training sample, place all positive samples in one folder, put all negative samples in another folder, and scale all training samples to the same size. The built-in opencv example scales the sample size to 64*128 during training;

(4) extract hog features of all positive samples;

(5) extract hog features of all negative samples;

(6) Add a sample label to all positive and negative samples. For example, if all positive samples are marked as 1, all negative samples are marked as 0;

(7) input the hog features of positive and negative samples and the tags of positive and negative samples into SVM for training. Considering the speed problem, linear SVM is recommended for training. Linear SVM may also be used here;

(8) after SVM training, save the result as a text file.

(9) In the text file obtained after linear SVM training, there is an array called support vector, and an array called Alpha, which has a floating point number, called rock; use the Alpha matrix with support
Multiply vector. Note that Alpha * supportvector will get a column vector. Then, add the final element of the column vector. In this way, a classifier is used to directly replace the default Classifier Used for pedestrian detection in opencv (CV: hogdescriptor: setsvmdetector ()), you can use the classifier trained by your training sample to perform Pedestrian detection.

The following is a sample training reference code:

 

 

 

Class mysvm: Public cvsvm {public: int get_alpha_count () {return this-> sv_total;} int get_sv_dim () {return this-> var_all;} int get_sv_count () {return this-> decision_func-> sv_count;} double * get_alpha () {return this-> decision_func-> alpha;} float ** get_sv () {return this-> Sv;} float get_rov () {return this-> decision_func-> rho;}}; void train () {char classifiersavepath [256] = "C: /pedestrianDetect-peopleFlow.txt "; string positivepath =" E: \ pictures \ train1 \ pos \ "; string negativepath =" E: \ pictures \ train1 \ neg \ "; int positivesamplecount = 4900; int negativesamplecount = 6192; int totalsamplecount = positivesamplecount + negativesamplecount; cout <"//////////////////////////////////// /// // "<Endl; cout <"totalsamplecount:" <totalsamplecount <Endl; cout <"positivesamplecount:" <positivesamplecount <Endl; cout <"negativesamplecount: "<negativesamplecount <Endl; cvmat * samplefeaturesmat = cvcreatemat (totalsamplecount, 1764, cv_32fc1); // 64*128 training samples, this matrix will be a training sample of totalsample * 3780,64*64. this matrix will be totalsample * 1764 cvsetzero (samplefeaturesmat); cvmat * samplelabelmat = cvcreatemat (totalsamplecount, 1, cv_32fc1 ); // sample ID cvsetzero (samplelabelmat ); cout <"************************************ * *********************** "<Endl; cout <"start to training positive samples... "<Endl; char positiveimgname [256]; string path; For (INT I = 0; I <positivesamplecount; I ++) {memset (positiveimgname, '\ 0 ', 256 * sizeof (char); sprintf (positiveimgname, "d.jpg", I); int Len = strlen (positiveimgname); string tempstr = positiveimgname; Path = positivepath + tempstr; CV:: mat IMG = CV: imread (PATH); If (IMG. data = NULL) {cout <"positive image sample load error:" <I <"" <path <Endl; System ("pause "); continue;} CV: hogdescriptor hog (CV: size (64, 64), CV: size (16, 16), CV: size (8, 8), CV :: size (8, 8), 9); vector <float> featurevec; hog. compute (IMG, featurevec, CV: size (8, 8); int featurevecsize = featurevec. size (); For (Int J = 0; j <featurevecsize; j ++) {cv_mat_elem (* samplefeaturesmat, float, I, j) = featurevec [J];} samplelabelmat-> data. FL [I] = 1;} cout <"End of training for positive samples... "<Endl; cout <"************************************ * ******************** "<Endl; cout <"start to train negative samples... "<Endl; char negativeimgname [256]; for (INT I = 0; I <negativesamplecount; I ++) {memset (negativeimgname, '\ 0 ', 256 * sizeof (char); sprintf (negativeimgname, "mongod.jpg", I); Path = negativepath + negativeimgname; CV: mat IMG = CV: imread (PATH ); if (IMG. data = NULL) {cout <"negative image sample load error:" <path <Endl; continue;} CV: hogdescriptor hog (CV :: size (64), CV: size (16, 16), CV: size (8, 8), CV: size (8, 8), 9); vector <float> featurevec; hog. compute (IMG, featurevec, CV: size (8, 8); // calculate the hog feature int featurevecsize = featurevec. size (); For (Int J = 0; j <featurevecsize; j ++) {cv_mat_elem (* samplefeaturesmat, float, I + positivesamplecount, j) = featurevec [J];} samplelabelmat-> data. FL [I + positivesamplecount] =-1;} cout <"End of training for negative samples... "<Endl; cout <"************************************ * ******************* "<Endl; cout <"start to train for SVM classifier... "<Endl; cvsvmparams Params; Params. svm_type = cvsvm: c_svc; Params. kernel_type = cvsvm: Linear; Params. term_crit = cvtermcriteria (cv_termcrit_iter, 1000, flt_epsilon); Params. C = 0.01; mysvm SVM; SVM. train (samplefeaturesmat, samplelabelmat, null, null, Params); // train SVM Using SVM linear classifier. save (classifiersavepath); cvreleasemat (& samplefeaturesmat); cvreleasemat (& samplelabelmat); int supportvectorsize = SVM. get_support_vector_count (); cout <"Support Vector size of SVM:" <supportvectorsize <Endl; cout <"************************ end of training for SVM ******* * *********** "<Endl; cvmat * SV, * phosphatase, * Re; // all sample feature vectors SV = cvcreatemat (supportvectorsize, 1764, cv_32fc1); phosphatase = cvcreatemat (1, supportvectorsize, cv_32fc1 ); re = cvcreatemat (1, 1764, cv_32fc1); cvmat * res = cvcreatemat (1, 1, cv_32fc1); cvsetzero (SV); cvsetzero (re ); for (INT I = 0; I <supportvectorsize; I ++) {memcpy (float *) (SV-> data. FL + I * 1764), SVM. get_support_vector (I), 1764 * sizeof (float);} double * alphago r = SVM. get_alpha (); int alphacount = SVM. get_alpha_count (); For (INT I = 0; I <supportvectorsize; I ++) {Alb-> data. FL [I] = alph316r [I];} cvmatmul (ALB, SV, RE); int poscount = 0; For (INT I = 0; I <1764; I ++) {re-> data. FL [I] * =-1;} file * fp = fopen ("C:/hogSVMDetector-peopleFlow.txt", "WB"); If (null = FP) {return 1 ;} for (INT I = 0; I <1764; I ++) {fprintf (FP, "% F \ n", re-> data. FL [I]);} float ROV = SVM. get_rov (); fprintf (FP, "% F", rock); cout <"C:/hogsvmdetector.txt saved" <Endl; // save fclose (FP); return 1 ;}

 

Next, we will provide the reference code for pedestrian detection using the trained classifier:

void Detect(){CvCapture* cap = cvCreateFileCapture("E:\\02.avi");if (!cap){cout<<"avi file load error..."<<endl;system("pause");exit(-1);}vector<float> x;ifstream fileIn("c:/hogSVMDetector-peopleFlow.txt", ios::in);float val = 0.0f;while(!fileIn.eof()){fileIn>>val;x.push_back(val);}fileIn.close();vector<cv::Rect>  found;cv::HOGDescriptor hog(cv::Size(64,64), cv::Size(16,16), cv::Size(8,8), cv::Size(8,8), 9);hog.setSVMDetector(x);IplImage* img = NULL;cvNamedWindow("img", 0);while(img=cvQueryFrame(cap)){hog.detectMultiScale(img, found, 0, cv::Size(8,8), cv::Size(32,32), 1.05, 2);if (found.size() > 0){for (int i=0; i<found.size(); i++){CvRect tempRect = cvRect(found[i].x, found[i].y, found[i].width, found[i].height);cvRectangle(img, cvPoint(tempRect.x,tempRect.y),cvPoint(tempRect.x+tempRect.width,tempRect.y+tempRect.height),CV_RGB(255,0,0), 2);}}}cvReleaseCapture(&cap);}

 

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.