DSP

利用opencv源码和vs编程序训练分类器haartraining.cpp

2019-07-13 18:32发布

如需转载请注明本博网址:http://blog.csdn.net/ding977921830/article/details/47733363

一  训练框架

训练人脸检测分类器需要三个步骤: (1) 准备正负样本集,分别放到两个文件夹里。我使用的是麻省理工的那个人脸库,大家可以网上搜一下。 (2)把正样本集生成正样本描述文件(*.vec),把负样本集生成负样本集合文件。具体怎么操作请参考我博客中的另外两篇文章,分别是http://blog.csdn.net/ding977921830/article/details/45913789http://blog.csdn.net/ding977921830/article/details/45914137。 (3)利用........opencvsourcesappshaartraininghaartraining.cpp训练分类器。

二  建立工程

我使用的是vs2012和opencv2.4.9,其实,使用其他的版本也差别不多大。 1  配置opencv2.4.9和vs2012,这个网上有很多资料,我就不啰嗦了哈; 2  在vs中新建工程,把opencv库中的下面文件........opencvsourcesappshaartraining添加到工程中,在解决方案资源管理器中,分别添加头文件和源文件,添加好后,内容如下:

三  程序

上面main.cpp的内容也就是haartraining.cpp中的程序,具体内容如下: //M*/ /* * haartraining.cpp *里面有部分参数我是稍作修改 *http://blog.csdn.net/ding977921830/article/details/47733363 * Train cascade classifier */ #include #include #include using namespace std; #include "cvhaartraining.h" int main( int argc, char* argv[] ) { int i = 0; char* nullname = (char*)"(NULL)"; char* vecname = NULL; char* dirname = NULL; char* bgname = NULL; bool bg_vecfile = false; int npos = 2000; //保证npos与nneg的比例为1:2至1::3之间比较好 int nneg = 4000; int nstages = 3; //为了节约时间可以把把设置为1,或2或3,当然也可以设置十几或二十几,不过,我没有耐心实验 int mem = 200; int nsplits = 1; float minhitrate = 0.995F; float maxfalsealarm = 0.5F; float weightfraction = 0.95F; int mode = 0; int symmetric = 1; int equalweights = 0; int width = 20; int height = 20; const char* boosttypes[] = { "DAB", "RAB", "LB", "GAB" }; int boosttype = 0; //选用DAB const char* stumperrors[] = { "misclass", "gini", "entropy" }; int stumperror = 0; //选用misclass int maxtreesplits = 0; int minpos = 500; if( argc == 1 ) { printf( "Usage: %s -data " " -vec " " -bg " " [-bg-vecfile] " " [-npos ] " " [-nneg ] " " [-nstages ] " " [-nsplits ] " " [-mem ] " " [-sym (default)] [-nonsym] " " [-minhitrate ] " " [-maxfalsealarm ] " " [-weighttrimming ] " " [-eqw] " " [-mode ] " " [-w ] " " [-h ] " " [-bt ] " " [-err ] " " [-maxtreesplits ] " " [-minpos ] ", argv[0], npos, nneg, nstages, nsplits, mem, minhitrate, maxfalsealarm, weightfraction, width, height, maxtreesplits, minpos ); return 0; } for( i = 1; i < argc; i++ ) { /*if( !strcmp( argv[i], "-data" ) ) { dirname = argv[++i]; } else if( !strcmp( argv[i], "-vec" ) ) { vecname = argv[++i]; } else if( !strcmp( argv[i], "-bg" ) ) { bgname = argv[++i]; }*/ if( !strcmp( argv[i], "-data" ) ) //前面这三个条件里面的内容我稍作修改 { dirname = argv[i]; } else if( !strcmp( argv[i], "-vec.vec" ) ) { vecname = argv[i]; } else if( !strcmp( argv[i], "-bg.txt" ) ) { bgname = argv[i]; } else if( !strcmp( argv[i], "-bg-vecfile" ) ) { bg_vecfile = true; } else if( !strcmp( argv[i], "-npos" ) ) { npos = atoi( argv[++i] ); } else if( !strcmp( argv[i], "-nneg" ) ) { nneg = atoi( argv[++i] ); } else if( !strcmp( argv[i], "-nstages" ) ) { nstages = atoi( argv[++i] ); } else if( !strcmp( argv[i], "-nsplits" ) ) { nsplits = atoi( argv[++i] ); } else if( !strcmp( argv[i], "-mem" ) ) { mem = atoi( argv[++i] ); } else if( !strcmp( argv[i], "-sym" ) ) { symmetric = 1; } else if( !strcmp( argv[i], "-nonsym" ) ) { symmetric = 0; } else if( !strcmp( argv[i], "-minhitrate" ) ) { minhitrate = (float) atof( argv[++i] ); } else if( !strcmp( argv[i], "-maxfalsealarm" ) ) { maxfalsealarm = (float) atof( argv[++i] ); } else if( !strcmp( argv[i], "-weighttrimming" ) ) { weightfraction = (float) atof( argv[++i] ); } else if( !strcmp( argv[i], "-eqw" ) ) { equalweights = 1; } else if( !strcmp( argv[i], "-mode" ) ) { char* tmp = argv[++i]; if( !strcmp( tmp, "CORE" ) ) { mode = 1; } else if( !strcmp( tmp, "ALL" ) ) { mode = 2; } else { mode = 0; } } else if( !strcmp( argv[i], "-w" ) ) { width = atoi( argv[++i] ); } else if( !strcmp( argv[i], "-h" ) ) { height = atoi( argv[++i] ); } else if( !strcmp( argv[i], "-bt" ) ) { i++; if( !strcmp( argv[i], boosttypes[0] ) ) { boosttype = 0; } else if( !strcmp( argv[i], boosttypes[1] ) ) { boosttype = 1; } else if( !strcmp( argv[i], boosttypes[2] ) ) { boosttype = 2; } else { boosttype = 3; } } else if( !strcmp( argv[i], "-err" ) ) { i++; if( !strcmp( argv[i], stumperrors[0] ) ) { stumperror = 0; } else if( !strcmp( argv[i], stumperrors[1] ) ) { stumperror = 1; } else { stumperror = 2; } } else if( !strcmp( argv[i], "-maxtreesplits" ) ) { maxtreesplits = atoi( argv[++i] ); } else if( !strcmp( argv[i], "-minpos" ) ) { minpos = atoi( argv[++i] ); } } printf( "Data dir name: %s ", ((dirname == NULL) ? nullname : dirname ) ); printf( "Vec file name: %s ", ((vecname == NULL) ? nullname : vecname ) ); printf( "BG file name: %s, is a vecfile: %s ", ((bgname == NULL) ? nullname : bgname ), bg_vecfile ? "yes" : "no" ); printf( "Num pos: %d ", npos ); printf( "Num neg: %d ", nneg ); printf( "Num stages: %d ", nstages ); printf( "Num splits: %d (%s as weak classifier) ", nsplits, (nsplits == 1) ? "stump" : "tree" ); printf( "Mem: %d MB ", mem ); printf( "Symmetric: %s ", (symmetric) ? "TRUE" : "FALSE" ); printf( "Min hit rate: %f ", minhitrate ); printf( "Max false alarm rate: %f ", maxfalsealarm ); printf( "Weight trimming: %f ", weightfraction ); printf( "Equal weights: %s ", (equalweights) ? "TRUE" : "FALSE" ); printf( "Mode: %s ", ( (mode == 0) ? "BASIC" : ( (mode == 1) ? "CORE" : "ALL") ) ); printf( "Width: %d ", width ); printf( "Height: %d ", height ); //printf( "Max num of precalculated features: %d ", numprecalculated ); printf( "Applied boosting algorithm: %s ", boosttypes[boosttype] ); printf( "Error (valid only for Discrete and Real AdaBoost): %s ", stumperrors[stumperror] ); printf( "Max number of splits in tree cascade: %d ", maxtreesplits ); printf( "Min number of positive samples per cluster: %d ", minpos ); cvCreateTreeCascadeClassifier( dirname, vecname, bgname, npos, nneg, nstages, mem, nsplits, minhitrate, maxfalsealarm, weightfraction, mode, symmetric, equalweights, width, height, boosttype, stumperror, maxtreesplits, minpos, bg_vecfile ); return 0; } 我的命令行参数为:"D:vs2012projects rain_opencv_main rain_cascadeDebug est.exe" "-data"  "-vec.vec"  "-bg.txt" ,具体设置方法是  调试----属性----配置属性----调试---命令参数
1 注意命令行参数中间要有空格的。 2 其中第一个你要修改为你自己电脑上工程的绝对路径; 3 "-data" 是存放训练好的分类器,需要预先建立好一个的空文件夹; 4 "-vec.vec" 是我的正样本描述文件; 5 "-bg.txt"是我的负样本集合文件。

四  训练结果

1  dos操作窗口
2  data文件夹的内容为: 我的0文件中训练了6个弱文类器,1文件中含有9个弱分类器,2文件夹下有17个弱分类器,每一个文件夹就是一个级联stage,显然是越来越复杂的哈。
3  以文件0为例,里面的内容为: 6
1
2
7 1 6 10 0 -1
9 1 2 10 0 3
haar_x3
4.792333e-002 0 -1
-1.845703e+000 1.845703e+000
1
2
1 3 18 12 0 -1
1 7 18 4 0 3
haar_y3
2.389797e-001 0 -1
-1.396623e+000 1.396623e+000
1
3
2 16 6 4 0 -1
2 16 3 2 0 2
5 18 3 2 0 2
haar_x2_y2
6.900427e-003 0 -1
-9.798445e-001 9.798445e-001
1
2
10 0 10 1 0 -1
10 0 5 1 0 2
haar_x2
1.219139e-002 0 -1
-5.156118e-001 5.156118e-001
1
2
0 0 10 1 0 -1
5 0 5 1 0 2
haar_x2
1.014664e-002 0 -1
-7.365732e-001 7.365732e-001
1
2
9 14 5 3 0 -1
9 15 5 1 0 3
haar_y3
-6.578934e-003 0 -1
7.885281e-001 -7.885281e-001
-3.758514e+000

-1
-1 4  xml文件 到这里我们的训练分类器终于出来的,XML文件可以在在vs中直接调用了,xml文件的内容你看是跟上面data文件中的内容是严格一一对应的,我摘录其中部分内容(也就是0文件夹部分)如下:

<_-data type_id="opencv-haar-classifier">
 
    20 20

 
    <_>
     
     
        <_>
         
          <_>
           
           
             
                <_>
                  7 1 6 10 -1.
                <_>
                  9 1 2 10 3.

              0

            4.7923330217599869e-002
            -1.8457030057907104e+000
            1.8457030057907104e+000
        <_>
         
          <_>
           
           
             
                <_>
                  1 3 18 12 -1.
                <_>
                  1 7 18 4 3.

              0

            2.3897969722747803e-001
            -1.3966230154037476e+000
            1.3966230154037476e+000
        <_>
         
          <_>
           
           
             
                <_>
                  2 16 6 4 -1.
                <_>
                  2 16 3 2 2.
                <_>
                  5 18 3 2 2.

              0

            6.9004269316792488e-003
            -9.7984451055526733e-001
            9.7984451055526733e-001
        <_>
         
          <_>
           
           
             
                <_>
                  10 0 10 1 -1.
                <_>
                  10 0 5 1 2.

              0

            1.2191389687359333e-002
            -5.1561182737350464e-001
            5.1561182737350464e-001
        <_>
         
          <_>
           
           
             
                <_>
                  0 0 10 1 -1.
                <_>
                  5 0 5 1 2.

              0

            1.0146640241146088e-002
            -7.3657321929931641e-001
            7.3657321929931641e-001
        <_>
         
          <_>
           
           
             
                <_>
                  9 14 5 3 -1.
                <_>
                  9 15 5 1 3.

              0

            -6.5789339132606983e-003
            7.8852808475494385e-001
            -7.8852808475494385e-001

      -3.7585139274597168e+000
      -1
      -1
    <_>