OpenCV Error: Bad argument (Can not get new positive sample. The most possible reason is insufficien
训练报错: OpenCV Error: Bad argument (Can not get new positive sample. The most possible reason is insufficient count of samples in given vec-file.) in get, file /home/opencv-.1.0/apps/traincascade/imagestorage.cpp, line 157 terminate called after throwing an instance of 'cv::Exception' what(): /home/opencv-3.1.0/apps/traincascade/imagestorage.cpp:157: error: (-5) Can not get new positive sample. The most possible reason is insufficient count of samples in given vec-file. in function get 已放弃 (核心已转储) 查找了资料,都基本上说的是NumPos设置太大的问题(但是我试了几次调小numPos依然报错),此前我的训练命令语句中参数如下: opencv\_traincascade -data classifier -vec samples.vec -bg negatives.txt\\ \-numStages 20 -minHitRate 0.999 -maxFalseAlarmRate 0.5 -numPos 1000\\ \-numNeg 600 -w 80 -h 40 -mode ALL -precalcValBufSize 1024\\ \-precalcIdxBufSize 1024 这段话是我直接复制粘贴的github项目源码步骤。我按照资料把 numPos 1000改为 500 以及numNeg 600 改为300,依然报错。 解决办法: 后来我发现这里面, “-w 80 -h 40” 分别表示正样本的宽高,但我想了一下,我的正样本图像都是20×20,然后我试着把命令参数都设置为“-w 20 -h 20”,如下: perl bin/createsamples.pl positives.txt negatives.txt samples 1500\\ "opencv\_createsamples -bgcolor 0 -bgthresh 0 -maxxangle 1.1\\ \-maxyangle 1.1 maxzangle 0.5 -maxidev 40 -w 20 -h 20" opencv\_traincascade -data classifier -vec samples.vec -bg negatives.txt\\ \-numStages 20 -minHitRate 0.999 -maxFalseAlarmRate 0.5 -numPos 500\\ \-numNeg 300 -w 20 -h 20 -mode ALL -precalcValBufSize 1024\\ \-precalcIdxBufSize 1024 成功跑通 最新补充:看到资料有说,保持正样本图像宽高比例跟上面的w,h比例一样就行,不一定相等。 https://blog.csdn.net/weixin\_39388032/article/details/118175930 运行opencv\_traincascade .exe 训练完第一层出现错误: OpenCV: terminate handler is called! The last OpenCV error is: OpenCV(3.4.6) Error: Bad argument (> Can not get new positive sample. The most possible reason is insufficient count of samples in given vec-file. 1 2 3 这个问题主要是numPos的值设置的不对, 很多资料都简单的把numPos解释为正样本,我也一直是这样认为的,导致numPos的值和创建vec样本的num数完全一样; 这是错误的,有一个公式可以来限定numPos的取值: vec-file number >= (numPos + (numStages-1) \* (1 – minHitRate) \* numPos) + S 1 vec-file number:创建vec文件的正样本的num S: 表示生成vec文件时负样本总数? (where S is a count of samples from vec-file that can be recognized as background right away) 实测下来,有几点肯定: 1.确实是numPos的问题; 2.numPos应小于设置的和生成vec文件的num数;修改的话可参考上面减小NumPos的值 3.观察第一层训练情况,第一层训练结束不报错后面就没问题了; ———————————————— https://blog.csdn.net/sazass/article/details/100275777