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.