概述
如需转载请注明本博网址:http://blog.csdn.net/ding977921830/article/details/47733363。
一 训练框架
训练人脸检测分类器需要三个步骤:
(1) 准备正负样本集,分别放到两个文件夹里。我使用的是麻省理工的那个人脸库,大家可以网上搜一下。
(2)把正样本集生成正样本描述文件(*.vec),把负样本集生成负样本集合文件。具体怎么操作请参考我博客中的另外两篇文章,分别是http://blog.csdn.net/ding977921830/article/details/45913789和http://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
*里面有部分参数我是稍作修改
*<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: %sn -data <dir_name>n"
" -vec <vec_file_name>n"
" -bg <background_file_name>n"
" [-bg-vecfile]n"
" [-npos <number_of_positive_samples = %d>]n"
" [-nneg <number_of_negative_samples = %d>]n"
" [-nstages <number_of_stages = %d>]n"
" [-nsplits <number_of_splits = %d>]n"
" [-mem <memory_in_MB = %d>]n"
" [-sym (default)] [-nonsym]n"
" [-minhitrate <min_hit_rate = %f>]n"
" [-maxfalsealarm <max_false_alarm_rate = %f>]n"
" [-weighttrimming <weight_trimming = %f>]n"
" [-eqw]n"
" [-mode <BASIC (default) | CORE | ALL>]n"
" [-w <sample_width = %d>]n"
" [-h <sample_height = %d>]n"
" [-bt <DAB | RAB | LB | GAB (default)>]n"
" [-err <misclass (default) | gini | entropy>]n"
" [-maxtreesplits <max_number_of_splits_in_tree_cascade = %d>]n"
" [-minpos <min_number_of_positive_samples_per_cluster = %d>]n",
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: %sn", ((dirname == NULL) ? nullname : dirname ) );
printf( "Vec file name: %sn", ((vecname == NULL) ? nullname : vecname ) );
printf( "BG file name: %s, is a vecfile: %sn", ((bgname == NULL) ? nullname : bgname ), bg_vecfile ? "yes" : "no" );
printf( "Num pos: %dn", npos );
printf( "Num neg: %dn", nneg );
printf( "Num stages: %dn", nstages );
printf( "Num splits: %d (%s as weak classifier)n", nsplits,
(nsplits == 1) ? "stump" : "tree" );
printf( "Mem: %d MBn", mem );
printf( "Symmetric: %sn", (symmetric) ? "TRUE" : "FALSE" );
printf( "Min hit rate: %fn", minhitrate );
printf( "Max false alarm rate: %fn", maxfalsealarm );
printf( "Weight trimming: %fn", weightfraction );
printf( "Equal weights: %sn", (equalweights) ? "TRUE" : "FALSE" );
printf( "Mode: %sn", ( (mode == 0) ? "BASIC" : ( (mode == 1) ? "CORE" : "ALL") ) );
printf( "Width: %dn", width );
printf( "Height: %dn", height );
//printf( "Max num of precalculated features: %dn", numprecalculated );
printf( "Applied boosting algorithm: %sn", boosttypes[boosttype] );
printf( "Error (valid only for Discrete and Real AdaBoost): %sn",
stumperrors[stumperror] );
printf( "Max number of splits in tree cascade: %dn", maxtreesplits );
printf( "Min number of positive samples per cluster: %dn", 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:vs2012projectstrain_opencv_maintrain_cascadeDebugtest.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文件夹部分)如下:
<?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></_>
<_>
最后
以上就是野性睫毛膏为你收集整理的利用opencv源码和vs编程序训练分类器haartraining.cpp一 训练框架 二 建立工程三 程序 四 训练结果的全部内容,希望文章能够帮你解决利用opencv源码和vs编程序训练分类器haartraining.cpp一 训练框架 二 建立工程三 程序 四 训练结果所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复