OpenCV 源码解读——createsample

11 篇文章 0 订阅
10 篇文章 3 订阅

  • 最近在学习,网上有很多训练的教程,但是不够深入,照葫芦画瓢会导致遇到问题无从下手。遂将源码创建工程,通过看注释和调试程序对源码有了一定认识,了解什么是瓢,分享在这里,从createsample开始。
  • 在VS中,通过项目属性->配置属性->调试 设置命令行参数,如 -img .\pos\2.bmp  -vec vec.txt  -num 9 -w 20 -h 20 -show 。
  • 对源码增加了在生成vec文件之前,保存各个样本的功能,这样就知道样本的具体情况,遇到问题更好解决,如有需要欢迎交流。
  • 本文首先对生成样本需要的用到的参数进行说明,又对四种不同的参数输入的程序所做的处理进行注释说明,如有偏差欢迎指教。


1.参数:

[-info <collection_file_name>]:/* file name with marked up image descriptions */
 正样本描述文件,格式:2).bmp 2 125 72 48 46 0 70 35 43  //图片名+正样本数目+每个正样本的矩形左上点坐标,长宽。
                
 [-img <image_file_name>]:正样本图片名,一般对纯刚性图片识别时候,只需要一个正样本,这里就是指定一个正样本的名字。

 [-vec <vec_file_name>]:生成的vec文件

 [-bg <background_file_name>]:背景图片的存储文件,文件内容为文件路径。

 [-num <number_of_samples = 1000>]:desired number of samples,期望的正样本数目

 [-bgcolor <background_color = 0>]:背景颜色,表示透明颜色

 [-inv] :if not 0 sample foreground pixels will be inverted  if invert == CV_RANDOM_INVERT then samples will be inverted randomly
 前景图像颜色翻转标志,如果指定颜色反转,默认0(不翻转)

 [-randinv] :如果指定颜色将随机翻转,默认0
 
 [-bgthresh <background_color_threshold = 80>]:颜色容差,所有处于bgcolor-bgthresh和bgcolor+bgthresh之间的像素被置为透明像素,
 也就是将白噪声加到前景图像上。

 [-maxidev <max_intensity_deviation = 40>]:desired max intensity deviation of foreground samples pixels
 前景图像中像素的亮度梯度最大值

 [-maxxangle <max_x_rotation_angle = 1.100000>]:X轴最大旋转角度,以弧度为单位,默认1.1

 [-maxyangle <max_y_rotation_angle = 1.100000>]:Y轴最大旋转角度,以弧度为单位,默认1.1
 
 [-maxzangle <max_z_rotation_angle = 0.500000>]:Z轴最大旋转角度,以弧度为单位,默认0.5

 [-show [<scale = 4.000000>]]:显示每个样本

 [-w <sample_width = 24>]:输出样本宽度

 [-h <sample_height = 24>]:输出样本高度
 
2.   参数: -img .\pos\2.bmp  -vec vec.txt  -num 9 -w 20 -h 20 -show 

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 );
Create training samples applying random distortions to sample image and store them in .vec file。
        printf( "Done\n" );
    }
    当只有一张图片作为正样本时候,多为刚性物体,通过多种变化,生成num个正样本集合,保存在vec文件中。该函数中
    bgfilename      - background description file name. If not NULL samples will be put on arbitrary background,
    也就是说没有bgfilename时候,随机生成背景图。


 3.参数:-img .\pos\2.bmp  -bg bg.txt -info info.txt  -num 9 -w 20 -h 20 -show 
 
 else if( imagename && bgfilename && infoname )//根据一张图片和一些背景图片生成测试样例
    {
        printf( "Create test samples from single image applying distortions...\n" );
testsample就是将正样本放在背景图片上形成的图片,并将图片保存到目录中个,图片名保存在infoname中
        cvCreateTestSamples( infoname, imagename, bgcolor, bgthreshold, bgfilename, num,
            invert, maxintensitydev,
            maxxangle, maxyangle, maxzangle, showsamples, width, height );
        printf( "Done\n" );
    }
注:
其中,count = MIN( count, cvbgdata->count );count是期望生成的正样本数目,cvbgdata->count是背景图片的数目,
最后生成的testsample的数目是二者的最小值。


4.参数:-info pos.txt -vec vec.txt  -num 9 -w 20 -h 20 -show 

else if( infoname && vecname )
    {
        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 );
    }
    注:最后生成的样本的个数取min(正样本个数,期望的正样本数目)。


 5.参数:-vec vec.txt  -w 20 -h 20 
查看vec文件内容。


  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值