How to Get Past the Infamous “Insufficient Count of Samples” Error in OpenCV Cascade Training

Many people who have tried OpenCV’s traincascade tool know about a very disturbing error which (at least at the moment) seems to have no good solution, but nevertheless this issue has a solution (even if not convenient) so we need to stick to what we have at hand. My Cascade Trainer GUI app which also uses OpenCV as the underlying framework is also seen to produce this error with many users so I decided to discuss this and share my solution with you all, but please make sure you put your comment below if you’ve got any questions or better suggestions. So, let’s get down to it.


First of all, here’s the actual monster itself, the error that makes me want to smash my computer’s screen after hours of training time:

OpenCV Error: Bad argument (Can not get new positive sample. The most possible reason is insufficient count of samples in given vec-file.
) in CvCascadeImageReader::PosReader::get, file \path_to_opencv\apps\traincascade\imagestorage.cpp, line X

With some older versions of OpenCV the error message (assert) might be even different but at the moment and with OpenCV 3.2.0 this is more or less the error message that you’ll see.


Next you are probably asking: what’s the explanation for this error? Why is it happening? And the answer is “Good Question”, and just that.

Well there is the official discussion in this page  but what it’s basically saying is (I’m quoting one of OpenCV people, see this for more info):

-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.


Well I assume a lot of you are confused, but fear not, because we all are. Basically what they are saying is that you need to have good samples with a low “S” number but that’s not the answer you’d expect, right? You’d like a concrete solution, like me, but unfortunately it’s not that simple for now. Next comes what I usually do besides making sure I have a “Good Sample Set”.

What do we need to do, you’ll ask if you’re still not disappointed enough. Well I’ve added a feature to Cascade Trainer GUI app to simplify this but if you are insisting on using opencv_traincascade then the answer is also more or less lies here. Just make sure you use a percentage lower than 100% or force a Positive Sample Count which is actually lower than your actual number of positive samples. If you get the same error yet again, then try entering even a lower percentage or lower positive sample count. I’m afraid at the moment there’s no other way but to try lowering the number until  it works. Screenshot below shows where you need to set what I just mentioned in the Cascade Trainer GUI app:

Obviously if you have a lot of samples (and I mean a lot) then you are safe with entering some 90% or 80% but it becomes a real pain if your sample count is limited. But anyway that’s it for the moment. Share your comments with me please, if you’ve got any.

Leave a Reply

Your email address will not be published. Required fields are marked *