Ask Your Question

How to training HOG and use my HOGDescriptor?

asked 2013-03-29 14:47:15 -0500

flammxy gravatar image

I want to training data and use HOG algorithm to detect pedestrian. Now I can use defaultHog.setSVMDetector(HOGDescriptor::getDefaultPeopleDetector()); in opencv to detection, but the result is not very good to my testing video. So I want to do training use my database.

I have prepared 1000+ positive sample, and 1000+ negative samples. They are cropped to size 50 * 100, and I have do the list file.

And I have read some tutorials on the internet, they all so complex, sometimes abstruse. Most of them are analyze the source code and the algorithm of HOG. But with only less examples and simple analysis.

Some instruction show that libsvm\windows\svm-train.exe .can be used to training, Can anyone gives an examples according to 1000+ 50*100 positive samples?
For example, like haartraing, we can do it from opencv, like haartraining.exe –a –b with some parameters, and get a *.xml as a result which will be used to people detection?

Or is there any other method to training, and detection? I prefer to know how to use it and the detail procedures. As the detail algorithm, it is not important to me. I just want to implement it.

If anyone know about it, please give me some tips. Thanks

edit retag flag offensive close merge delete

5 answers

Sort by » oldest newest most voted

answered 2015-12-28 09:22:37 -0500

updated 2017-09-05 07:28:19 -0500

OpenCV 3.x have a sample application train_HOG.cpp


to get an attention i marked my question as best answer.

i updated train_HOG.cpp you can try it to feel the difference.

any advice or critique is welcome about the Pull Request

example usage for test:

cpp-example-train_HOG -t -fn=my_detector.yml -tv=../data/vtest.avi

(you don't need INRIA database to test) you can use my_detector.yml which created using the cmd line below

cpp-example-train_HOG -d -dw=64 -dh=128 -pd=/INRIAPerson/96X160H96/Train/pos -nd=/INRIAPerson/neg -tv=../data/vtest.avi
edit flag offensive delete link more


I am getting the following error, please help me : ─> $ ./train --pd=/home/me/img_proc_learning/training/hog_svm/pos --nd=/home/me/img_proc_learning/training/hog_svm/neg --dw=64 --dh=64 --fn=bikes_all.yml Positive images are being loaded......[done] Negative images are being loaded...Floating point exception (core dumped)

I ran gdb it gave the following output Negative images are being loaded... Thread 1 "train" received signal SIGFPE, Arithmetic exception. 0x0000555555557df6 in sample_neg(std::vector<cv::Mat, std::allocator<cv::Mat> > const&, std::vector<cv::Mat, std::allocator<cv::Mat> >&, cv::Size_<int> const&) () (gdb) I suspect the error is because of some problem with mod (%) in line 107 and 108 . But my changes did not help much :(

vishwaprakash gravatar imagevishwaprakash ( 2018-04-07 11:30:48 -0500 )edit

BTW, I am giving 64x64. That is my requirement. I hope its not a problem

vishwaprakash gravatar imagevishwaprakash ( 2018-04-07 11:32:39 -0500 )edit

answered 2014-01-22 08:22:25 -0500

nkint gravatar image

Hi! I have found this repository:

It seems to be a nice tutorial on how to train an HOG detector using SVMlight 6.02. Even if I haven't tried myself I would give it a try!

edit flag offensive delete link more

answered 2013-04-18 02:27:03 -0500

sub_o gravatar image

updated 2013-04-18 02:27:44 -0500

In OpenCV, there's undocumented API

HOGDescriptor::compute(const cv::Mat &img, vector<float> &descriptors)

which you could pass in your image, and retrieve the descriptors for it. And then you could construct a large feature matrix containing both positive and negative features, and also label matrix that specifies which entry in that feature matrix is positive or negative.

Both the features and label matrices then could be passed into CvSVM class for training, and you could save it in HDD and load it afterwards. Later on you could extract features from a patch of an image, use compute to extract the features, and pass it into svm.predict to obtain the prediction whether it's positive or negative image. The downside from this is that you need to apply your own detection window algorithm, etc.

Below is some sample code to train your own SVM.

Mat labels( pos_count + neg_count, 1, CV_32FC1, Scalar(-1.0) );
labels.rowRange( 0, pos_count ) = Scalar( 1.0 );

CvSVM svm;
CvSVMParams params;
params.C = c;
params.svm_type = CvSVM::C_SVC;
params.kernel_type = CvSVM::LINEAR;
params.term_crit = cvTermCriteria( CV_TERMCRIT_ITER, 10000, 1e-6 );

Mat feat_matrix( pos_count + neg_count, pos_feats[0].cols, pos_feats[0].type() );
for( int i = 0; i < pos_count; i++ )
    pos_feats[i].copyTo( feat_matrix.rowRange(i, i+1) );
for( int i = 0; i < neg_count; i++ )
    neg_feats[i].copyTo( feat_matrix.rowRange(i + pos_count, i + pos_count + 1) );

cout << "Start training SVM" << endl;
svm.train( feat_matrix, labels, Mat(), Mat(), params ); (path + svm_file).c_str() );
edit flag offensive delete link more




do you know how i can convert the trained SVM to the detector vector required in cv::HOGDescriptor::void setSVMDetector(const vector<float>& detector); ? If i could convert the SVM, i could use the very powerful cv::HOGDescriptor::detectMultiScale() and cv::HOGDescriptor::detect() methods for object detection.

Best Regards Siegfried

Siegfried gravatar imageSiegfried ( 2013-04-18 02:48:44 -0500 )edit

Sadly no. I decided to create my own multi scale window detection, but still struggling with its speed.

sub_o gravatar imagesub_o ( 2013-04-18 02:50:24 -0500 )edit

Thanks a lot.

flammxy gravatar imageflammxy ( 2013-04-30 22:10:09 -0500 )edit

@Siegfried now the answer is yes. see me answer.

sturkmen gravatar imagesturkmen ( 2017-09-05 06:43:38 -0500 )edit

answered 2013-04-17 22:45:26 -0500

bonchenko gravatar image

updated 2013-04-24 23:15:44 -0500

You need to get the HOG features from all of your positive and negative sample images using HOGDescriptor:compute functions, then feed the result to SVM library such as SVMlight. This page will help you to compute the feature and get the resulting model from SVM Light. The model will be available in genfiles/descriptorVector

edit flag offensive delete link more

answered 2013-07-12 05:53:10 -0500

I have trained SVMlite with data, that I have generated with opencv, and get the model file. After that with got the weighted vector, but when run hog detector with that the result is awful... It detects some random images or only one in the center of the image... ;/

edit flag offensive delete link more


thats because the size of the trainning samples. the opencv version of hog, only detect 64x128... different ... is chaos

VonTalavang gravatar imageVonTalavang ( 2015-03-20 10:54:46 -0500 )edit
Login/Signup to Answer

Question Tools



Asked: 2013-03-29 14:47:15 -0500

Seen: 36,992 times

Last updated: Sep 05 '17