Ask Your Question

Why always OpenCV Error: Assertion failed (elements_read == 1) in unknown function ?

asked 2012-10-11 22:13:05 -0500

flammxy gravatar image

updated 2012-10-27 17:10:08 -0500

error infomation: OpenCV Error: Assertion failed (elements_read == 1) in unknown function, file .. ....\opencv\apps\haartraining\cvhaartraining.cpp, line 1859

I am doing pedestrain detection using haartraining. My positive samples are 900 samles, each is 40x80 , with information ( 1 0 0 40 80 ) and 500 negetive samples, each is 300x200

I have check the parameters, they are all ok. But when doing opencv_haartraining.exe it always shows this error. Could anyone help me?

Thank you

edit retag flag offensive close merge delete


I have EXACTLY the same issue when training some face feature classifiers with opencv 2.4.2, failing on both a mac (OSX10.8 64bit) and a PC (win7 64bit): "OpenCV Error: Assertion failed (elements_read == 1) in icvGetHaarTraininDataFromVecCallback, file ..../OpenCV-2.4.2/apps/haartraining/cvhaartraining.cpp, line 1859"

My basic setup: -npos 4000 -nneg 8000 -nstages 16 -minhitrate 0.999 -model ALL -mem 1000 -w 16 -h 16 -nonsym.

The error occurs at stage no. 2 or 3. I am pretty much following this example here:, but not sure what goes wrong.

TonyZ gravatar imageTonyZ ( 2012-10-12 23:05:39 -0500 )edit

TO TonyZ:

Me too. Same error information, apps/haartraining/cvhaartraining.cpp, line 1859.Here is a anwser of this problem: .at the end of the pages.

But I still have the problem. I do not know what's meaning of "The problem is that your vec-file has exactly the same samples count that you passed in command line -numPos "

Have you solved this problem yet ?

flammxy gravatar imageflammxy ( 2012-10-14 18:12:31 -0500 )edit

Hi all, I have no answer yet, but your question and its answers strengthens my view that relatively in recent refactoring an error was introduced in the vector file reading procedure. Actually, the writing procedure icvWriteVecSample writes a null char before each vec sample written. The assert that fails checks for this null. It is called from within a for(;;) loop that breaks only if an eof is found. However, somehow the eof never occurs (at my place, a mac using macports) before the assert is being thrown.

I already added a null char to the end of my .vec file in order to check if things run through if there is a 0 in the end. The answer is "no".

Having read your post i am inclined to fix icvGetHaarTraininDataFromVecCallback so that it reads vectors correctly. I suspect the problem is

wolfgang gravatar imagewolfgang ( 2012-11-04 04:59:23 -0500 )edit

I suspect the problem is some intricacy with eof. To be honest, currently I am more of a JAVA person and in my former c++ life I have rarely used eof. I preferred using file formats with announced lengths. So, please wish me luck ;-). I will post my findings when I have a fix.

wolfgang gravatar imagewolfgang ( 2012-11-04 05:00:11 -0500 )edit

Just a question: Why has this been voted negatively?

wolfgang gravatar imagewolfgang ( 2012-11-04 09:42:40 -0500 )edit

I don't know. I solve the problem by change the database. Maybe more negative samples and more different samples will work. Good luck : )

flammxy gravatar imageflammxy ( 2013-01-07 13:42:34 -0500 )edit

1 answer

Sort by ยป oldest newest most voted

answered 2013-01-26 18:58:07 -0500

cipher gravatar image

this issue was answered by creater of the utility on the OpenCV DevZone site in June 2012.

To quote Maria:

The problem is that your vec-file has exactly the same samples count that you passed in command line -numPos 979. Training application used all samples from the vec-file to train 0-stage and it can not get new positive samples for the next stage training because vec-file is over. The bug of traincascade is that it had assert() in such cases, but it has to throw an exception with error message for a user. It was fixed in r8913. -numPose is a samples count that is used to train each stage. Some already used samples can be filtered by each previous stage (ie recognized as background), but no more than (1 - minHitRate) * numPose on each stage. So vec-file has to contain >= (numPose + (numStages-1) * (1 - minHitRate) * numPose) + S, where S is a count of samples from vec-file that can be recognized as background right away. I hope it can help you to create vec-file of correct size and chose right numPos value.

It worked for me. I also had same problem, I was following the famous tutorial on HAAR training but wanted to try the newer training utility with -npos 7000 -nneg 2973

so i did following calcs:

vec-file has to contain >= (numPos + (numStages-1) * (1 - minHitRate) * numPos) + S

7000 >= (numPos + (20-1) * (1 - 0.999) * numPos) + 2973

(7000 - 2973)/(1 + 19*0.001) >= numPos

numPos <= 4027/1.019

numPos <= 3951 ~~ 3950

and used:

-npos 3950 -nneg 2973

works so far...

edit flag offensive delete link more

Question Tools


Asked: 2012-10-11 22:13:05 -0500

Seen: 7,929 times

Last updated: Jan 26 '13