opencv实例训练器traincascade源码学习-------traincascade.cpp解析

  1 #include "opencv2/core.hpp"
  2 #include "cascadeclassifier.h"
  3 
  4 using namespace std;
  5 using namespace cv;
  6 
  7 int main( int argc, char* argv[] )
  8 {
  9     //默认初始化
 10     CvCascadeClassifier classifier;
 11     string cascadeDirName, vecName, bgName;
 12     int numPos    = 2000;
 13     int numNeg    = 1000;
 14     int numStages = 20;
 15     int numThreads = getNumThreads();
 16     int precalcValBufSize = 1024,
 17         precalcIdxBufSize = 1024;
 18     bool baseFormatSave = false;
 19     double acceptanceRatioBreakValue = -1.0;
 20 
 21     CvCascadeParams cascadeParams;
 22     CvCascadeBoostParams stageParams;
 23     //三种分类方式的分类器指针
 24     Ptr<CvFeatureParams> featureParams[] = { makePtr<CvHaarFeatureParams>(),//Harr
 25                                              makePtr<CvLBPFeatureParams>(),//LBP
 26                                              makePtr<CvHOGFeatureParams>()//HOG
 27                                            };
 28     int fc = sizeof(featureParams)/sizeof(featureParams[0]);//计算分类器个数
 29     //输出默认参数
 30     if( argc == 1 )
 31     {
 32         cout << "Usage: " << argv[0] << endl;//获取执行的.exe文件
 33         cout << "  -data <cascade_dir_name>" << endl;
 34         cout << "  -vec <vec_file_name>" << endl;
 35         cout << "  -bg <background_file_name>" << endl;
 36         cout << "  [-numPos <number_of_positive_samples = " << numPos << ">]" << endl;
 37         cout << "  [-numNeg <number_of_negative_samples = " << numNeg << ">]" << endl;
 38         cout << "  [-numStages <number_of_stages = " << numStages << ">]" << endl;
 39         cout << "  [-precalcValBufSize <precalculated_vals_buffer_size_in_Mb = " << precalcValBufSize << ">]" << endl;
 40         cout << "  [-precalcIdxBufSize <precalculated_idxs_buffer_size_in_Mb = " << precalcIdxBufSize << ">]" << endl;
 41         cout << "  [-baseFormatSave]" << endl;
 42         cout << "  [-numThreads <max_number_of_threads = " << numThreads << ">]" << endl;
 43         cout << "  [-acceptanceRatioBreakValue <value> = " << acceptanceRatioBreakValue << ">]" << endl;
 44         cascadeParams.printDefaults();
 45         stageParams.printDefaults();
 46         for( int fi = 0; fi < fc; fi++ )
 47             featureParams[fi]->printDefaults();//打印各个分类器的默认参数
 48         return 0;
 49     }
 50     //命令解析
 51     for( int i = 1; i < argc; i++ )
 52     {
 53         bool set = false;
 54         if( !strcmp( argv[i], "-data" ) )
 55         {
 56             cascadeDirName = argv[++i];
 57         }
 58         else if( !strcmp( argv[i], "-vec" ) )
 59         {
 60             vecName = argv[++i];
 61         }
 62         else if( !strcmp( argv[i], "-bg" ) )
 63         {
 64             bgName = argv[++i];
 65         }
 66         else if( !strcmp( argv[i], "-numPos" ) )
 67         {
 68             numPos = atoi( argv[++i] );
 69         }
 70         else if( !strcmp( argv[i], "-numNeg" ) )
 71         {
 72             numNeg = atoi( argv[++i] );
 73         }
 74         else if( !strcmp( argv[i], "-numStages" ) )
 75         {
 76             numStages = atoi( argv[++i] );
 77         }
 78         else if( !strcmp( argv[i], "-precalcValBufSize" ) )
 79         {
 80             precalcValBufSize = atoi( argv[++i] );
 81         }
 82         else if( !strcmp( argv[i], "-precalcIdxBufSize" ) )
 83         {
 84             precalcIdxBufSize = atoi( argv[++i] );
 85         }
 86         else if( !strcmp( argv[i], "-baseFormatSave" ) )
 87         {
 88             baseFormatSave = true;
 89         }
 90         else if( !strcmp( argv[i], "-numThreads" ) )
 91         {
 92           numThreads = atoi(argv[++i]);
 93         }
 94         else if( !strcmp( argv[i], "-acceptanceRatioBreakValue" ) )
 95         {
 96           acceptanceRatioBreakValue = atof(argv[++i]);
 97         }
 98         else if ( cascadeParams.scanAttr( argv[i], argv[i+1] ) ) { i++; }
 99         else if ( stageParams.scanAttr( argv[i], argv[i+1] ) ) { i++; }
100         else if ( !set )
101         {
102             for( int fi = 0; fi < fc; fi++ )
103             {
104                 set = featureParams[fi]->scanAttr(argv[i], argv[i+1]);
105                 if ( !set )
106                 {
107                     i++;
108                     break;
109                 }
110             }
111         }
112     }
113 
114     setNumThreads( numThreads );
115     classifier.train( cascadeDirName,
116                       vecName,
117                       bgName,
118                       numPos, numNeg,
119                       precalcValBufSize, precalcIdxBufSize,
120                       numStages,
121                       cascadeParams,
122                       *featureParams[cascadeParams.featureType],
123                       stageParams,
124                       baseFormatSave,
125                       acceptanceRatioBreakValue );
126     return 0;
127 }

这部分主要是主函数对输入的命令参数的解析,及默认值输出。

主函数转向的线路图:

main()-------->cascadeclassifier.h -------->调用这部分的函数   train()进行数据训练。

 

初次尝试源码分析,多多指教!!

posted @ 2017-03-28 11:07  林梦然  阅读(2722)  评论(0编辑  收藏  举报