利用opencv训练于Haar特征、LBP特征、Hog特征的分类器及问题解决
****中相关文章很多,我主要参考以下几篇,并解决了在实践中遇到的几个问题。
参考:
https://blog.****.net/lql0716/article/details/72566839
问题解决:
第一次使用opencv_haartraining.exe进行测试,可是总是出现
如上图的错误,经搜索无可用解决方案。
第二次使用opencv_traincascade.exe进行测试,出现
如上图错误,经搜索有解决方案,先几篇常见错误的博客,把正负样本准备好,再进行测试。
第三次,样本准备完毕(正2000,负5000,使用python进行样本扩充),进行训练出现如下问题:
原因如下:http://villager5.blog.163.com/blog/static/8273636201462452037527/
解决方案如下:
So,there are two solutions:
1)generate your bt.txt (negative sample file) on Ubuntu, make sure that there isa newline for each image file no empty line in the whole file;
2) orgenerate your bt.txt (negative sample file) on Windows, but add one line in theoriginal opencv code "imagestorage.cpp":
beforeline 45, add "
if(str[str.size() - 1] == '\r')
str.resize(str.size() - 1); // to erase \rif the file is generated by windows"; then, recompile (cmake .. -> make-> make install) your opencv for a new opencv_traincascade application.
使用第一种解决方案,成功。(使用windows下的opencv训练的朋友,用ubuntu生成这个文件,再考过去就行了)
但又出现了如下问题
原因如下:
级联分类器训练时,到某个级数时报错:
Traincascade Error: Bad argument (Can not get new positivesample. Themost possible reason is insufficient count of samples in givenvec-file.)
解析:设置的numPos过大,由于训练时POScount 会从你设置的numPos增大,每一级都按一定的次序增大,后来可能会超过样本库中正样本的个数,就会报这个错误。
将命令行中的numPos改小一些,即可接着现有的层级训练
于是改为opencv_traincascade.exe -data xml -vec pos.vec -bgneg\neg.txt -numPos 1000 -numNeg 4000 -numStages 20 -featureType LBP -w 24 -h24
训练成功进行
最终训练好使2h24m56s,共十八级,训练完毕。
Cascade.xml即为所得。