在我们开始训练我们的Haar分类器之前,首先要对样本进行处理。

人脸识别的尝试系列(一)中:http://blog.csdn.net/u011583927/article/details/44627493

我们已经提到了如何准备我们的样本,在如下图准备好样本之后

需要在cmd窗口中调用类似如下的命令生成vec文件

opencv_createsamples.exe–vec pos.vec –info pos_image.txt –bg neg_image.txt –w 24 –h 24 –num 400

那么具体是如何生成的vec文件的,下面是具体的实现代码以及根据我个人理解加上的详细注释

为方便大家理解,代码中插入了两张图片,分别展示了相应区域代码的执行结果

/** createsamples.cpp 生成vec文件的可执行程序的具体实现** Create test/training samples        利用描述正样本和负样本的txt文件创建vec文件* 命令行示例:
opencv_createsamples.exe –vec pos.vec –info pos_image.txt –bg neg_image.txt –w 24 –h 24 –num 400
*需要先将命令行的地址调到当前文件夹下*/#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <ctime>
#include <memory>using namespace std;#include "cvhaartraining.h"
#include "ioutput.h"int main( int argc, char* argv[] )
{//参数初始化int i = 0;char* nullname   = (char*)"(NULL)";char* vecname    = NULL; /* .vec file name */char* infoname   = NULL; //file name with marked up image descriptions正样本描述文件路径char* imagename  = NULL; /* single sample image  单个正样本图片*/char* bgfilename = NULL; /* background  负样本描述文件路径 */int num = 1000;int bgcolor = 0;int bgthreshold = 80;int invert = 0;int maxintensitydev = 40;// max_intensity_deviationdouble maxxangle = 1.1;double maxyangle = 1.1;double maxzangle = 0.5;bool showsamples = false;/* the samples are adjusted to this scale in the sample preview window */double scale = 4.0;int width  = 24;int height = 24;bool pngoutput = false; /* whether to make the samples in png or in jpg*/srand((unsigned int)time(0));//如果只输入opencv_createsamples.exe 相当于查看命令行参数使用if( argc == 1 ){printf( "Usage: %s\n  [-info <collection_file_name>]\n""  [-img <image_file_name>]\n""  [-vec <vec_file_name>]\n""  [-bg <background_file_name>]\n  [-num <number_of_samples = %d>]\n""  [-bgcolor <background_color = %d>]\n""  [-inv] [-randinv] [-bgthresh <background_color_threshold = %d>]\n""  [-maxidev <max_intensity_deviation = %d>]\n""  [-maxxangle <max_x_rotation_angle = %f>]\n""  [-maxyangle <max_y_rotation_angle = %f>]\n""  [-maxzangle <max_z_rotation_angle = %f>]\n""  [-show [<scale = %f>]]\n""  [-w <sample_width = %d>]\n  [-h <sample_height = %d>]\n""  [-pngoutput]",argv[0], num, bgcolor, bgthreshold, maxintensitydev,maxxangle, maxyangle, maxzangle, scale, width, height );return 0;}

 for( i = 1; i < argc; ++i )
{//strcmp(str1,str2)函数:比较两个字符串。
//相等返回0;str1>str2返回正数;str1<str2返回负数if( !strcmp( argv[i], "-info" ) ){//argv[i]==”-info”infoname = argv[++i];}else if( !strcmp( argv[i], "-img" ) ){imagename = argv[++i];}else if( !strcmp( argv[i], "-vec" ) ){vecname = argv[++i];}else if( !strcmp( argv[i], "-bg" ) ){bgfilename = argv[++i];}else if( !strcmp( argv[i], "-num" ) ){//atoi 把一个字符串转化成整形num = atoi( argv[++i] );}else if( !strcmp( argv[i], "-bgcolor" ) ){bgcolor = atoi( argv[++i] );}else if( !strcmp( argv[i], "-bgthresh" ) ){bgthreshold = atoi( argv[++i] );}else if( !strcmp( argv[i], "-inv" ) ){//输入含-inv指令invert = 1;}else if( !strcmp( argv[i], "-randinv" ) ){//输入含-randinv指令invert = CV_RANDOM_INVERT;}else if( !strcmp( argv[i], "-maxidev" ) ){maxintensitydev = atoi( argv[++i] );}else if( !strcmp( argv[i], "-maxxangle" ) ){maxxangle = atof( argv[++i] );}else if( !strcmp( argv[i], "-maxyangle" ) ){maxyangle = atof( argv[++i] );}else if( !strcmp( argv[i], "-maxzangle" ) ){maxzangle = atof( argv[++i] );}else if( !strcmp( argv[i], "-show" ) ){showsamples = true;if( i+1 < argc && strlen( argv[i+1] ) > 0 && argv[i+1][0] != '-' ){double d;d = strtod( argv[i+1], 0 );if( d != -HUGE_VAL && d != HUGE_VAL && d > 0 ) scale = d;++i;}}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], "-pngoutput" ) ){pngoutput = true;}}printf( "Info file name: %s\n", ((infoname == NULL) ?   nullname : infoname ) );printf( "Img file name: %s\n",  ((imagename == NULL) ?  nullname : imagename ) );printf( "Vec file name: %s\n",  ((vecname == NULL) ?    nullname : vecname ) );printf( "BG  file name: %s\n",  ((bgfilename == NULL) ? nullname : bgfilename ) );printf( "Num: %d\n", num );printf( "BG color: %d\n", bgcolor );printf( "BG threshold: %d\n", bgthreshold );printf( "Invert: %s\n", (invert == CV_RANDOM_INVERT) ? "RANDOM": ( (invert) ? "TRUE" : "FALSE" ) );printf( "Max intensity deviation: %d\n", maxintensitydev );printf( "Max x angle: %g\n", maxxangle );printf( "Max y angle: %g\n", maxyangle );printf( "Max z angle: %g\n", maxzangle );printf( "Show samples: %s\n", (showsamples) ? "TRUE" : "FALSE" );if( showsamples ){printf( "Scale applied to display : %g\n", scale );}if( !pngoutput){printf( "Original image will be scaled to:\n");printf( "\tWidth: $backgroundWidth / %d\n", width );printf( "\tHeight: $backgroundHeight / %d\n", height );}/* determine action  (通过关键命令)确定行为*/if( imagename && vecname ){printf( "Create training samples from single image applying distortions...\n" );cvCreateTrainingSamples( vecname, imagename, bgcolor, bgthreshold, bgfilename,num, invert, maxintensitydev,maxxangle, maxyangle, maxzangle,showsamples, width, height );printf( "Done\n" );}else if( imagename && bgfilename && infoname){printf( "Create data set from single image applying distortions...\n""Output format: %s\n",(( pngoutput ) ? "PNG" : "JPG") );std::auto_ptr<DatasetGenerator> creator;if( pngoutput ){creator = std::auto_ptr<DatasetGenerator>( new PngDatasetGenerator( infoname ) );}else{creator = std::auto_ptr<DatasetGenerator>( new JpgDatasetGenerator( infoname ) );}creator->create( imagename, bgcolor, bgthreshold, bgfilename, num,invert, maxintensitydev, maxxangle, maxyangle, maxzangle,showsamples, width, height );printf( "Done\n" );}else if( infoname && vecname )
{//生成vec文件,我们使用的这种命令//命令包括正样本描述文件的文件名,准备生成的vec文件的文件名int total;printf( "Create training samples from images collection...\n" );total = cvCreateTrainingSamplesFromInfo( infoname, vecname, num, showsamples,width, height );printf( "Done. Created %d samples\n", total );}else if( vecname )
{//查看vec文件//命令不包含正样本文件,背景样本文件,单个正样本图像,只包含vec文件路径

 printf( "View samples from vec file (press ESC to exit)...\n" );cvShowVecSamples( vecname, width, height, scale );printf( "Done\n" );}else{printf( "Nothing to do\n" );}return 0;
}cvCreateTrainingSamplesFromInfo函数的具体实现
为方便理解,对应于上面我们输入的命令来进行解释
-infoname    pos_image.txt 正样本描述文件文件名
-vecfilename pos.vec 这个参数相当于指定创建的vec文件的名字,函数执行前这个vec文件并不存在
-num         400 正样本总数
-showsamples false 是否显示样本
-width
-height
int cvCreateTrainingSamplesFromInfo( const char* infoname, const char* vecfilename,int num,int showsamples,int winwidth, int winheight )
{char fullname[PATH_MAX];char* filename;FILE* info;FILE* vec;IplImage* src=0;IplImage* sample;int line;int error;int i;int x, y, width, height;int total;/*
#include <assert.h>
void assert( int expression );
assert的作用是现计算表达式 expression ,如果其值为假(即为0),那么它先向stderr打印一条出错信息,然后通过调用 abort 来终止程序运行。*/assert( infoname != NULL );assert( vecfilename != NULL );total = 0;if( !icvMkDir( vecfilename ) )
{// icvMkDir()
//个人理解:判断文件名vecfilename是否只包含文件名,不包含路径//例如:pos.veg返回1  D:\\pos.veg返回0//若只包含文件名,返回1,否则返回0#if CV_VERBOSEfprintf( stderr, "Unable to create directory hierarchy: %s\n", vecfilename );
#endif /* CV_VERBOSE */return total;}info = fopen( infoname, "r" );//以只读方式打开文件infoname,若文件不存在不创建该文件(即返回null)if( info == NULL ){#if CV_VERBOSEfprintf( stderr, "Unable to open file: %s\n", infoname );
#endif /* CV_VERBOSE */return total;}vec = fopen( vecfilename, "wb" );// 以二进制写方式打开文件,若文件不存在则创建该文件if( vec == NULL ){#if CV_VERBOSEfprintf( stderr, "Unable to open file: %s\n", vecfilename );
#endif /* CV_VERBOSE */fclose( info );return total;}//创建一个单通道byte图像sample = cvCreateImage( cvSize( winwidth, winheight ), IPL_DEPTH_8U, 1 );//写vec文件头icvWriteVecHeader( vec, num, sample->width, sample->height );if( showsamples ){cvNamedWindow( "Sample", CV_WINDOW_AUTOSIZE );}strcpy( fullname, infoname );/*strrchr() 函数查找字符在指定字符串中从后面开始的第一次出现的位置,如果成功,则返回从该位置到字符串结尾的所有字符,如果失败,则返回 false。与之相对应的是strchr()函数,它查找字符串中首次出现指定字符的位置。*/filename = strrchr( fullname, '\\' );//获取正样本描述文件的文件名,剔除路径if( filename == NULL ){filename = strrchr( fullname, '/' );}if( filename == NULL ){filename = fullname;}else
{//正常情况,将指针指向‘\’后面的第一个字符filename++;}//遍历每张正样本图片,将其信息写入vec文件for( line = 1, error = 0, total = 0; total < num ;line++ ){int count;/*
fscanf功能: 从一个流中执行格式化输入,fscanf遇到空格和换行时结束,注意空格时也结束。
intfscanf(FILE*stream,constchar*format,[argument...]);
FILE *stream:文件指针;
char *format:格式字符串;
[argument...]:输入列表。
返回值:整型,成功读入的参数的个数
*/error = ( fscanf( info, "%s %d", filename, &count ) != 2 );
//info——正样本描述文件的文件流
// 正样本描述文件每一行的格式  pos_image/0.bmp 1 0 0 24 24
//注意!filename是指向fullname的指针。所以这里以filename作为读入数据的参数,实际上修改的是fullname的内容。读入信息后fullname表示某一个正样本图片的地址(相对路径)
//count 表示文件的个数if( !error )//说明读取是正确的,获取样本图片{src = cvLoadImage( fullname, 0 );//读取一副正样本图像(强制转化为灰度图像)error = ( src == NULL );if( error ){#if CV_VERBOSEfprintf( stderr, "Unable to open image: %s\n", fullname );
#endif /* CV_VERBOSE */}}//遍历当前样本图片中的所有子窗口样本。//一般情况下的使用方法是只有一个子窗口也就是整幅样本图片,
即count=1,且x=0 y=0 width height就是样本的宽和高 for( i = 0; (i < count) && (total < num); i++, total++ ){error = ( fscanf( info, "%d %d %d %d", &x, &y, &width, &height ) != 4 );//读取当前图像的顶点坐标以及长宽if( error ) break;// cvSetImageROI 基于给定的矩形设置图像的ROI(感兴趣区域)cvSetImageROI( src, cvRect( x, y, width, height ) );void cvResize( const CvArr* src, CvArr* dst, int interpolation=CV_INTER_LINEAR );函数cvResize()功能: 重新调整图像src(或它的ROI),使它精确匹配目标dst(或其ROI)。这里需要说明的是,cvResize可以用来调整3通道图像(如RGB图像)和单通道图像的大小。src 源图像;  dst 目标图像cvResize( src, sample, width >= sample->width &&height >= sample->height ? CV_INTER_AREA : CV_INTER_LINEAR );if( showsamples ){cvShowImage( "Sample", sample );if( cvWaitKey( 0 ) == 27 ){showsamples = 0;}}//将当前这幅样本图片信息写入vec文件中icvWriteVecSample( vec, sample );}//释放当前的图片占用的内存if( src ){cvReleaseImage( &src );}if( error ){#if CV_VERBOSEfprintf( stderr, "%s(%d) : parse error", infoname, line );
#endif /* CV_VERBOSE */break;}}if( sample ){cvReleaseImage( &sample );}fclose( vec );fclose( info );return total;
}

createsamples.cpp中生成vec文件的实现及详细注释、图解——人脸识别的尝试系列(三)相关推荐

  1. vector与结构体联合使用 在磁盘中生成.txt 文件

    一下纯属个人总结.欢迎拍砖!谢谢 我意思到以练促进学习C++编程基础是很有帮助的 这篇文章是我为了熟悉掌握文件流和STL中的vector以及结构体三个只知识点所写的代码: #include <s ...

  2. python在内存中生成Zip文件!

    python在内存中生成Zip文件! - 天真的好蓝啊 - 博客园 python在内存中生成Zip文件! import zipfile import StringIO class MemoryZipF ...

  3. android 构造xml,android 中生成xml文件

    在Android中生成xml文件真的很简单,下面提供2中方法,一种是通过String写入到文件,另外一种是通过XML 的 XmlSerializer. 以后遇到Android写xml内容就不会困惑了 ...

  4. 在 Apex 代码中生成 csv 文件

    在 Apex 代码中生成 csv 文件可以写一个 Visualforce 页面,设定类型为 excel 的格式,然后调用 PageReference 的 getContent()方法来获取 Blob ...

  5. 在F5 BIG IP版本9中生成CSR文件的教程

    在申请SSL证书时,申请者必须为您的 Web 服务器上的域名或主机名创建证书签名请求 (CSR).CSR是向证书颁发机构 (CA) 发送您的公钥的标准化方式,该公钥与服务器上的秘密私钥配对,并提供有关 ...

  6. opencv中的createsamples.exe生成vec文件注意事项

    1.查阅createsamples.cpp -info     输入正样本描述文件,默认NULL -img  输入图像文件名,默认NULL -bg    负样本描述文件,文件中包含一系列被选作背景的图 ...

  7. php输出PDF的文件流_怎么用PHP在HTML中生成PDF文件

    译文:使用PHP在html中生成PDF 译者:dwqs 利用PHP编码生成PDF文件是一个非常耗时的工作.在早期,开发者使用PHP并借助FPDF来生成PDF文件.但是如今,已经有很多函数库可以使用了, ...

  8. VB从程序中生成Exe文件

    这篇文章要讨论的是在一个VB程序中如何产生出另一个Exe文件. 要实现这个目的,必须符合以下几个条件: 第一.编写这样的程序时,具备欲生成的Exe文件 第二.事先知道欲生成的Exe文件大小 其实这两个 ...

  9. php excel 进度,在php中生成Excel文件时显示进度条

    我有一个 HTML表单,当您通过单击按钮提交表单时,应用程序使用 PHPExcel生成Excel文件.一切正常,但是当excel文件很大时,等待时间很长.我想添加进度条或显示完整百分比值.我的问题是我 ...

最新文章

  1. beanUtils操作bean的属性
  2. 抢椅子游戏java_游戏教案小班抢椅子
  3. 学习Kotlin(二)基本语法
  4. android 渠道号_亲测:安卓打渠道包神器,1分钟出自动出100个渠道包
  5. opengl2 vtk 编译_编译和使用VTK时值得注意的点(待续)
  6. 21世纪的设计模式:抽象工厂模式
  7. 从代码到部署微服务实战
  8. 原则 principles
  9. reflect动画_3DSMAX制作超时空未来动画场景-3D建模场景模型教程
  10. 如何阻止通过Outlook用户发送WORD或EXCEL变成带Winmail.dat文件附件的邮件
  11. VM中的Linux安装jdk和tomcat
  12. 辞职的新方式:一言不合就消失!
  13. hilbert滤波器 matlab,用MATLAB实现Hilbert变换
  14. 五种前端布局之table布局
  15. 用 Python 写出 Gameboy 模拟器,这位丹麦小哥的大学项目火了!
  16. 【日记】生活日记开篇里程碑
  17. 如何用Photoshop去制作一张质量高的banner(轮播图)?
  18. 对于初学者的JavaScript 教程
  19. 上火了该如何是好 五招让你轻松消火
  20. markman的下载与使用

热门文章

  1. Dcloud+mui 压缩上传图片到服务器
  2. Selenium-几种操作
  3. Python eclipse开发环境搭建
  4. 7.python xmlrpclib及allownone作用
  5. 浅谈Tuple之C#4.0新特性
  6. TCP的拥塞控制机制
  7. 【剑指offer】面试题07. 重建二叉树(Java)
  8. android ocr识别源码_身份证识别OCR解决手动输入繁琐问题
  9. html5离线储存不足,html5的离线存储问题
  10. mysql normsinv_在MySQL中实现NORMSINV函数