OPENCV HOG+SVM Training Program Considerations

Source: Internet
Author: User
Tags svm

Pedestrian training:Http://

Character Recognition:

The approximate flow of training with OPENCV using hog features for SVM algorithm is  

1) Set up the training sample set

Two sets of data are required, one is the category of the data, and the other is the vector information of the data.

2) Set SVM parameters, refer to "machine mode->LIBSVM parameter description"

Note that you must use linear SVM for training, because the detection function only supports linear detection!!!

3) Calculate hog features using Hogdescriptor

4) Training SVM

Call the Cvsvm::train function to build the SVM model, the first parameter is the training data, the second parameter is the classification result, the last parameter is Cvsvmparams

5) Use this SVM to classify

Call function Cvsvm::predict Implementation classification, you can use the method of exhaustive training hardexample

6) Get support vector

Call the function Cvsvm::get_support_vector_count to get the number of support vectors, cvsvm::get_support_vector get the corresponding index number support vector.

7) Save support vector with Alpha, rho

After the completion of the SVM training, there is an array, called the support vector, and an array called alpha, with a floating-point number called Rho;

Multiply the alpha matrix with the support vector, and note that Alpha*supportvector will get a column vector that is multiplied by-1 before the vector. After that, the column is then added to the last element of the vector rho.

In this way, it becomes a classifier that uses the classifier to directly replace the default classifier (Cv::hogdescriptor::setsvmdetector ()) for pedestrian detection in OpenCV,

int supportvectornum = Svm_train->get_support_vector_count ();

cout<< "Support vector size of SVM:" << supportvectornum << "\ n";

Support Vector Matrix

Mat SV = Mat::zeros (Supportvectornum, Fet_num, CV_32FC1);

Alpha vector, length equals number of support vectors

Mat ALP = Mat::zeros (1, Supportvectornum, CV_32FC1);

Alpha vector multiplied by the result of the support vector matrix

Mat re = Mat::zeros (1, Fet_num, CV_32FC1);

Copy support vector data into the Supportvectormat matrix

for (int i=0; i<supportvectornum; i++)

{ //Returns the data pointer of the I support vector

Const FLOAT * Psvdata = svm_train->get_support_vector (i);

for (int j=0; j< Fet_num; j + +)<float> (I,J) = Psvdata[j];   


Copies the alpha Vector's data into Alphamat, returning the alpha vector in the decision function of the SVM

Double * Palphadata = Svm_train->get_alpha_vector ();

for (int i=0; i<supportvectornum; i++)<float> (0,i) = (float) palphadata[i];

Calculation-(Alphamat * supportvectormat), the result is placed in Resultmat, note that because Svm.predict uses alpha*sv*another-rho, if negative, it is considered a positive sample, In the hog detection function, the use of rho+alpha*sv*another if positive is a positive sample, so you need to change the latter to a negative number after saving

Re =-1 * ALP * SV;

Save the product.

Ofstream OFS (Hog_name.c_str (), ios::out);

if (!ofs.is_open ())

Cerr << "Open file" << hog_name << "failed\n";

for (int i=0; i<fet_num; i++)

OFS <<<float> (0, i) << "\ n";

Float rho = Svm_train->get_rho ();

OFS << Rho << "\ n";

Ofs.close ();

OPENCV HOG+SVM Training Program Considerations

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.