Ask Your Question

How to use train_HOG.cpp

asked 2016-06-20 14:34:00 -0600

enco gravatar image

updated 2018-10-29 06:49:15 -0600


I am using openCV since 2 weeks and I am having trouble getting along with train_HOG.cpp to train a human detector (

The issue I have is following, I am not quiet sure which inputs train_HOG.cpp needs. There is pd and nd for the positive and negative directories, and n and p for neg.lst and pos.lst. But how should pos.lst and neg.lst look like? Which values do I have to input there?

I'm glad on every help I get!

Thanks enco

PS: Is there any documentation on this? Or any documentation on how to train a svm completely on your own?

edit retag flag offensive close merge delete

2 answers

Sort by ยป oldest newest most voted

answered 2016-06-22 07:55:08 -0600

berak gravatar image

admittedly, this is confusing.

pd : the directory (+ a slash), where your positive images are
p  : a textfile *inside* that folder, with plain image names, like "my1.png"
nd : the directory with neg images (see pd)
n   : a textfile inside nd with negative image names.


train_HOG -pd=pos/ -p=pos.txt -nd=neg/ -n=neg.txt


(really, we should change that, so it only needs pd and nd, and it should be using cv::glob() instead of txtfiles...)

edit flag offensive delete link more



there is an unsuccessful Pull Request to improve it

sturkmen gravatar imagesturkmen ( 2016-06-22 20:06:07 -0600 )edit

^^ thanks for the notice ;)

berak gravatar imageberak ( 2016-06-22 20:10:25 -0600 )edit

@berak are you going to look for a fix? I can always pitch in with some help!

StevenPuttemans gravatar imageStevenPuttemans ( 2016-06-30 07:17:26 -0600 )edit

yea, right, i'll do.

berak gravatar imageberak ( 2016-06-30 07:50:33 -0600 )edit

@berak i played with previous PR maybe you will check it

sturkmen gravatar imagesturkmen ( 2016-06-30 10:14:25 -0600 )edit

@sturkmen - perfect, why not just take that ?

berak gravatar imageberak ( 2016-06-30 10:35:02 -0600 )edit

@sturkmen, @StevenPuttemans - just curious, - currently the training only takes a single random patch from each negative image. would taking more samples there improve the training ?

berak gravatar imageberak ( 2016-07-01 02:11:12 -0600 )edit

Hmm I am guessing that is because most SVMs supply negative patches instead of larger images like with the traincascade. So you could adapt it to sequentially grab negatives until no more are available, but do not hardcode more then 1 sample.

StevenPuttemans gravatar imageStevenPuttemans ( 2016-07-01 03:53:48 -0600 )edit

answered 2016-06-23 07:17:38 -0600

enco gravatar image

Alright, thank you! I got it!, but now it's throwing an exception like:

OpenCV Error: Assertion failed (0 <= roi.x && 0 <= roi.width && roi.x + roi.width <= m.cols && 0 <= roi.y && 0 <= roi.height && roi.y + roi.height <= m.rows) in Mat, file /home/ubuntu/Documents/opencv-3.1.0/modules/core/src/matrix.cpp, line 508 terminate called after throwing an instance of 'cv::Exception' what(): /home/ubuntu/Documents/opencv-3.1.0/modules/core/src/matrix.cpp:508: error: (-215) 0 <= roi.x && 0 <= roi.width && roi.x + roi.width <= m.cols && 0 <= roi.y && 0 <= roi.height && roi.y + roi.height <= m.rows in function Mat

Aborted (core dumped)

My images are all the size of 64x128. I can't figure out where the problem is. I'm sure that the inputs are o.k. right now.

edit flag offensive delete link more


most probably one of your images is empty/invalid. please check after imread()

berak gravatar imageberak ( 2016-06-24 00:48:24 -0600 )edit

Question Tools

1 follower


Asked: 2016-06-20 14:34:00 -0600

Seen: 1,466 times

Last updated: Jun 23 '16