zoukankan      html  css  js  c++  java
  • 利用opencv源代码和vs编程序训练分类器haartraining.cpp

    如需转载请注明本博网址: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训练分类器。

    二  建立project

    我使用的是vs2012和opencv2.4.9,事实上,使用其它的版本号也区别不多大。

    1  配置opencv2.4.9和vs2012,这个网上有非常多资料,我就不啰嗦了哈。

    2  在vs中新建project,把opencv库中的以下文件........opencvsourcesappshaartraining加入到project中,在解决方式资源管理器中,分别加入头文件和源文件,加入好后,内容例如以下:


    三  程序

    上面main.cpp的内容也就是haartraining.cpp中的程序,详细内容例如以下:

    //M*/
    
    /*
     * haartraining.cpp
     *里面有部分參数我是稍作改动
     *<a target=_blank href="http://blog.csdn.net/ding977921830/article/details/47733363">http://blog.csdn.net/ding977921830/article/details/47733363</a>
     * Train cascade classifier
     */
    
    #include <cstdio>
    #include <cstring>
    #include <cstdlib>
    
    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 <dir_name>
    "
                    "  -vec <vec_file_name>
    "
                    "  -bg <background_file_name>
    "
                    "  [-bg-vecfile]
    "
                    "  [-npos <number_of_positive_samples = %d>]
    "
                    "  [-nneg <number_of_negative_samples = %d>]
    "
                    "  [-nstages <number_of_stages = %d>]
    "
                    "  [-nsplits <number_of_splits = %d>]
    "
                    "  [-mem <memory_in_MB = %d>]
    "
                    "  [-sym (default)] [-nonsym]
    "
                    "  [-minhitrate <min_hit_rate = %f>]
    "
                    "  [-maxfalsealarm <max_false_alarm_rate = %f>]
    "
                    "  [-weighttrimming <weight_trimming = %f>]
    "
                    "  [-eqw]
    "
                    "  [-mode <BASIC (default) | CORE | ALL>]
    "
                    "  [-w <sample_width = %d>]
    "
                    "  [-h <sample_height = %d>]
    "
                    "  [-bt <DAB | RAB | LB | GAB (default)>]
    "
                    "  [-err <misclass (default) | gini | entropy>]
    "
                    "  [-maxtreesplits <max_number_of_splits_in_tree_cascade = %d>]
    "
                    "  [-minpos <min_number_of_positive_samples_per_cluster = %d>]
    ",
                    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 当中第一个你要改动为你自己电脑上project的绝对路径;

    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目录部分)例如以下:

    <?xml version="1.0"?>
    <opencv_storage>
    <_-data type_id="opencv-haar-classifier">
      <size>
        20 20</size>
      <stages>
        <_>
          <!-- stage 0 -->
          <trees>
            <_>
              <!-- tree 0 -->
              <_>
                <!-- root node -->
                <feature>
                  <rects>
                    <_>
                      7 1 6 10 -1.</_>
                    <_>
                      9 1 2 10 3.</_></rects>
                  <tilted>0</tilted></feature>
                <threshold>4.7923330217599869e-002</threshold>
                <left_val>-1.8457030057907104e+000</left_val>
                <right_val>1.8457030057907104e+000</right_val></_></_>
            <_>
              <!-- tree 1 -->
              <_>
                <!-- root node -->
                <feature>
                  <rects>
                    <_>
                      1 3 18 12 -1.</_>
                    <_>
                      1 7 18 4 3.</_></rects>
                  <tilted>0</tilted></feature>
                <threshold>2.3897969722747803e-001</threshold>
                <left_val>-1.3966230154037476e+000</left_val>
                <right_val>1.3966230154037476e+000</right_val></_></_>
            <_>
              <!-- tree 2 -->
              <_>
                <!-- root node -->
                <feature>
                  <rects>
                    <_>
                      2 16 6 4 -1.</_>
                    <_>
                      2 16 3 2 2.</_>
                    <_>
                      5 18 3 2 2.</_></rects>
                  <tilted>0</tilted></feature>
                <threshold>6.9004269316792488e-003</threshold>
                <left_val>-9.7984451055526733e-001</left_val>
                <right_val>9.7984451055526733e-001</right_val></_></_>
            <_>
              <!-- tree 3 -->
              <_>
                <!-- root node -->
                <feature>
                  <rects>
                    <_>
                      10 0 10 1 -1.</_>
                    <_>
                      10 0 5 1 2.</_></rects>
                  <tilted>0</tilted></feature>
                <threshold>1.2191389687359333e-002</threshold>
                <left_val>-5.1561182737350464e-001</left_val>
                <right_val>5.1561182737350464e-001</right_val></_></_>
            <_>
              <!-- tree 4 -->
              <_>
                <!-- root node -->
                <feature>
                  <rects>
                    <_>
                      0 0 10 1 -1.</_>
                    <_>
                      5 0 5 1 2.</_></rects>
                  <tilted>0</tilted></feature>
                <threshold>1.0146640241146088e-002</threshold>
                <left_val>-7.3657321929931641e-001</left_val>
                <right_val>7.3657321929931641e-001</right_val></_></_>
            <_>
              <!-- tree 5 -->
              <_>
                <!-- root node -->
                <feature>
                  <rects>
                    <_>
                      9 14 5 3 -1.</_>
                    <_>
                      9 15 5 1 3.</_></rects>
                  <tilted>0</tilted></feature>
                <threshold>-6.5789339132606983e-003</threshold>
                <left_val>7.8852808475494385e-001</left_val>
                <right_val>-7.8852808475494385e-001</right_val></_></_></trees>
          <stage_threshold>-3.7585139274597168e+000</stage_threshold>
          <parent>-1</parent>
          <next>-1</next></_>
        <_>


  • 相关阅读:
    标准库:Number 对象
    标准库:Boolean 对象
    标准库:包装对象
    标准库:Array 对象
    git的基本操作
    子查询、联结、约束、索引
    sql基础、检索、过滤数据
    聚合函数和分组数据-GROUP BY、HAVING
    长期更新~java学习过程中遇到的英语单词
    3.(155)最小栈
  • 原文地址:https://www.cnblogs.com/jzssuanfa/p/6941643.html
Copyright © 2011-2022 走看看