如需转载请注明本博网址: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)利用........\opencv\sources\apps\haartraining\haartraining.cpp训练分类器。

二  建立project

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

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

2  在vs中新建project,把opencv库中的以下文件........\opencv\sources\apps\haartraining加入到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\n -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: %s\n", ((dirname == NULL) ? nullname : dirname ) );
printf( "Vec file name: %s\n", ((vecname == NULL) ? nullname : vecname ) );
printf( "BG file name: %s, is a vecfile: %s\n", ((bgname == NULL) ? nullname : bgname ), bg_vecfile ? "yes" : "no" );
printf( "Num pos: %d\n", npos );
printf( "Num neg: %d\n", nneg );
printf( "Num stages: %d\n", nstages );
printf( "Num splits: %d (%s as weak classifier)\n", nsplits,
(nsplits == 1) ? "stump" : "tree" );
printf( "Mem: %d MB\n", mem );
printf( "Symmetric: %s\n", (symmetric) ? "TRUE" : "FALSE" );
printf( "Min hit rate: %f\n", minhitrate );
printf( "Max false alarm rate: %f\n", maxfalsealarm );
printf( "Weight trimming: %f\n", weightfraction );
printf( "Equal weights: %s\n", (equalweights) ? "TRUE" : "FALSE" );
printf( "Mode: %s\n", ( (mode == 0) ? "BASIC" : ( (mode == 1) ? "CORE" : "ALL") ) );
printf( "Width: %d\n", width );
printf( "Height: %d\n", height );
//printf( "Max num of precalculated features: %d\n", numprecalculated );
printf( "Applied boosting algorithm: %s\n", boosttypes[boosttype] );
printf( "Error (valid only for Discrete and Real AdaBoost): %s\n",
stumperrors[stumperror] ); printf( "Max number of splits in tree cascade: %d\n", maxtreesplits );
printf( "Min number of positive samples per cluster: %d\n", 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:\vs2012\projects\train_opencv_main\train_cascade\Debug\test.exe" "-data"  "-vec.vec"  "-bg.txt"

。详细设置方法是  调试----属性----配置属性----调试---命令參数

watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="">

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></_>

    <_>

利用opencv源代码和vs编程序训练分类器haartraining.cpp的更多相关文章

  1. opencv 手写选择题阅卷 (三)训练分类器

    opencv 手写选择题阅卷 (三)训练分类器 1,分类器选择:SVM 本来一开始用的KNN分类器,但这个分类器目前没有实现保存训练数据的功能,所以选择了SVN分类器; 2,样本图像的预处理和特征提取 ...

  2. 如何利用OpenCV自带的级联分类器训练程序训练分类器

    介绍 使用级联分类器工作包括两个阶段:训练和检测. 检测部分在OpenCVobjdetect 模块的文档中有介绍,在那个文档中给出了一些级联分类器的基本介绍.当前的指南描述了如何训练分类器:准备训练数 ...

  3. 如何用OpenCV自带的adaboost程序训练并检测目标

    参考博文: 1.http://blog.csdn.net/wuxiaoyao12/article/details/39227189 2.http://www.cnblogs.com/easymind2 ...

  4. OpenCV训练分类器制作xml文档

    OpenCV训练分类器制作xml文档 (2011-08-25 15:50:06) 转载▼ 标签: 杂谈 分类: 学习 我的问题:有了opencv自带的那些xml人脸检测文档,我们就可以用cvLoad( ...

  5. opencv源代码分析之二:cvhaartraining.cpp

    我使用的是opencv2.4.9.安装后.我的cvboost..cpp文件的路径是........\opencv\sources\apps\haartraining\cvhaartraining.cp ...

  6. 利用opencv中的级联分类器进行人脸检測-opencv学习(1)

    OpenCV支持的目标检測的方法是利用样本的Haar特征进行的分类器训练,得到的级联boosted分类器(Cascade Classification).注意,新版本号的C++接口除了Haar特征以外 ...

  7. 利用ML&AI判定未知恶意程序——里面提到ssl恶意加密流检测使用N个payload CNN + 字节分布包长等特征综合判定

    利用ML&AI判定未知恶意程序 导语:0x01.前言 在上一篇ML&AI如何在云态势感知产品中落地中介绍了,为什么我们要预测未知恶意程序,传统的安全产品已经无法满足现有的安全态势.那么 ...

  8. 史上最全opencv源代码解读,opencv源代码具体解读文件夹

    本博原创,如有转载请注明本博网址http://blog.csdn.net/ding977921830/article/details/46799043. opencv源代码主要是基于adaboost算 ...

  9. C# 利用 OpenCV 进行视频捕获 (笔记)

    原文:C# 利用 OpenCV 进行视频捕获 (笔记) 简介 这个项目是关于如何从网络摄像头或者视频文件(*.AVI)中捕获视频的,这个项目是用C#和OPENCV编写的. 这将有助于那些喜欢C#和Op ...

随机推荐

  1. WordPress 自动草稿和文章修定版本

    写文章的时候发现 WordPress 有两个有意思的地方, WordPress 自动草稿和文章修定版本: 1.点击创建新文章的时候,会在数据库自动生成一条草稿数据: 2.修改数据的时候会将历史文章当做 ...

  2. HTML提交表单

    一.使用form提交表单 <form action="#" method="post"> {% csrf_token %} 班级<input ...

  3. python+selenium进行简单验证码获取

    # _*_ coding:utf-8 _*_from PIL import Imagefrom selenium import webdriverimport pytesseractimport ti ...

  4. 【codeforces 46C】Hamsters and Tigers

    [题目链接]:http://codeforces.com/problemset/problem/46/C [题意] 给你一个长度为n的01串; 让你把所有的0放在一起,把所有的1放在一起; (即0都是 ...

  5. 洛谷 P2758 编辑距离

    P2758 编辑距离 题目描述 设A和B是两个字符串.我们要用最少的字符操作次数,将字符串A转换为字符串B.这里所说的字符操作共有三种: 1.删除一个字符: 2.插入一个字符: 3.将一个字符改为另一 ...

  6. DataTable 数据导入MS ACCESS 数据库中 数字类型字段为空的解决办法

    string strSql = "insert into GongCheng (GCSY,GCBH,GCBHOLD,GCMC,GCKCJD,GCJSDW,GCSJDW,GCKCDW,GCSG ...

  7. [MST] Build Forms with React to Edit mobx-state-tree Models

    We will expand our UI, and give the user the possibility to edit his wishlist. We will use the earli ...

  8. Linux下查询CPU 缓存的工具

    在Linux下能够使用例如以下工具查询CPU缓存: 方式1: $ lscpu L1d cache: 32K <span style="white-space:pre"> ...

  9. 【试水CAS-4.0.3】第02节_CAS服务端登录页个性化

    完整版见https://jadyer.github.io/2015/07/16/sso-cas-login-diy/ /** * @see ------------------------------ ...

  10. CodeWars for JavaScript

    SubClass https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes Sub classing with ...