SVM + HOG learning and detection methods using HogDescriptor

Dear Erol çıtak

This posting is for cleanning up the SVM + HOG learning and detection methods to help you.

Step 1. Prepare Data.

Prepare Positive and Negative images
Same size and gray scale

And make xml file for a more convenient data management.
Refer to this page for this step

Step 2. Training by SVM

Load positive.xml and Negative.xml and train by SVM
Refer to this page for this step 2

Step 3. Test by SVM

After training, test other images.
Refer to this page for this step 3

Step 4. for using MultiScaleDetection() 

For using Hog.MultiScaleDetection() and other functions.
We have to change the value that result of svm training. 

Refer to this page for Step 4. (There is a method for converting the end of source code)

after training.
refer to this source code for using method
//Load trained SVM xml data
FileStorage svmDX_Xml("XXXXX.xml", FileStorage::READ);
Mat xMat;
svmDX_Xml["SecondSVMd"] >> xMat;
vector< float> VX;  
//copy mat to vector  
VX.assign((float*)xMat.datastart, (float*)xMat.dataend);

HOGDescriptor d( Size(64,64), Size(16,16), Size(8,8), Size(8,8), 9); //must be same with training setting.
d.detect(...) or d.detectMultiScale(...)


Thank you.


  1. 아래쪽에 있는 trained svm xml data의 경우 이전에 사용했던 hogdetectorxml을 사용하는것이 맞나요?
    hogdescriptor의 세팅을 training때처럼 Size(32, 16), Size(8, 8), Size(4, 4), Size(4, 4), 9 로 하고 진행하는데 d.setsvmdetector method에서 메모리 참조 오류가 발생하네요
    혹시나하고 trainedsvm.xml도 사용해보았지만...아닌걸 알기에
    dataset은 600개를 사용해서 데이터에 큰문제는 없을거 같은데 hogdetectorxml생성과정에서 차이가 벡터길이에 문제가있는건지 어떤건지 잘 모르겟네요ㅠㅠ

  2. Hi, could you give me some helps ? It works perfectly on step 123 but it has error when running to this line "d.setSVMDetector(VX);"
    Both the HOG descriptor was the same, image for training was converted to gray and same size for both pos and neg images.