首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >OpenCV创建示例示例

OpenCV创建示例示例
EN

Stack Overflow用户
提问于 2011-07-01 22:08:12
回答 4查看 32.9K关注 0票数 9

我正在尝试运行OpenCV库中的createsamples示例。我可以一次加载一个图像,它似乎工作得很好。然而,当我试图加载一个图像集合时,我得到了一个解析错误。我不确定它是不是在我的集合文件中是无效的,还是我在其他地方遗漏了什么。下面是我的文本文档的确切格式。

文本文档详细信息:

代码语言:javascript
复制
Target1.JPG 1 0 0 1296 1152
Target2.jpg 1 0 0 1890 709

命令行调用:

代码语言:javascript
复制
-info "C:\Users\seb\Desktop\Learning Samples\Target\Target.txt" -num 10 -vec "C:\Users\seb\Desktop\Learning Samples\Target\Target.vec" -maxxangle 0.6 -maxyangle 0 -maxzangle 0.3 -maxidev 100 -bgcolor 0 -bgthresh 0 -w 20 -h 20

任何帮助都是非常感谢的。

EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2012-02-10 00:20:31

解析错误是因为当您没有指定要生成的pos图像样本的数量时,createsamples将使用默认值1000。但是,如果批注文本文档包含的对象边界框少于1000个,则会出现解析错误。您仍然可以使用.vec文件进行级联训练。唯一的问题是number的信息不正确。有两种方法可以修复它。

  1. 手动计算文本文档中对象边界框的数量。并为选项" -num“指定小于或等于数字的值,例如createsamples -info xxxxx.txt -vec pos.vec -num value
  2. 您可以修改示例如果未指定-num,请将位置采样数设置为文本文档

中的对象边界框数

代码片段:在createsamples.cpp中,int num = 0;

在cvsamples.cpp中

代码语言:javascript
复制
void icvWriteVecHeader( FILE* file, int count, int width, int height )
{
    int vecsize;
    short tmp;

    fseek ( file , 0 , SEEK_SET );

    /* number of samples */
    fwrite( &count, sizeof( count ), 1, file );
    /* vector size */
    vecsize = width * height;
    fwrite( &vecsize, sizeof( vecsize ), 1, file );
    /* min/max values */
    tmp = 0;
    fwrite( &tmp, sizeof( tmp ), 1, file );
    fwrite( &tmp, sizeof( tmp ), 1, file );

    fseek ( file , 0 , SEEK_END );
}

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;

    assert( infoname != NULL );
    assert( vecfilename != NULL );

    total = 0;
    if( !icvMkDir( vecfilename ) )
    {

#if CV_VERBOSE
        fprintf( stderr, "Unable to create directory hierarchy: %s\n", vecfilename );
#endif /* CV_VERBOSE */

        return total;
    }

    info = fopen( infoname, "r" );
    if( info == NULL )
    {

#if CV_VERBOSE
        fprintf( stderr, "Unable to open file: %s\n", infoname );
#endif /* CV_VERBOSE */

        return total;
    }

    vec = fopen( vecfilename, "wb" );
    if( vec == NULL )
    {

#if CV_VERBOSE
        fprintf( stderr, "Unable to open file: %s\n", vecfilename );
#endif /* CV_VERBOSE */

        fclose( info );

        return total;
    }

    sample = cvCreateImage( cvSize( winwidth, winheight ), IPL_DEPTH_8U, 1 );

    icvWriteVecHeader( vec, num, sample->width, sample->height );

    if( showsamples )
    {
        cvNamedWindow( "Sample", CV_WINDOW_AUTOSIZE );
    }

    strcpy( fullname, infoname );
    filename = strrchr( fullname, '\\' );
    if( filename == NULL )
    {
        filename = strrchr( fullname, '/' );
    }
    if( filename == NULL )
    {
        filename = fullname;
    }
    else
    {
        filename++;
    }

    while ( num<=0 || total<num )
    {
        int count;

        error = ( fscanf( info, "%s %d", filename, &count ) != 2 );
        if( !error )
        {
            src = cvLoadImage( fullname, 0 );
            error = ( src == NULL );
            if( error )
            {

#if CV_VERBOSE
                fprintf( stderr, "Unable to open image: %s\n", fullname );
#endif /* CV_VERBOSE */
            }
        }
        else
            if ( num <= 0 ) break;

        for( i = 0; i < count; i++, total++ )
        {
            error = ( fscanf( info, "%d %d %d %d", &x, &y, &width, &height ) != 4 );
            if( error ) break;
            cvSetImageROI( src, cvRect( x, y, width, height ) );
            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;
                }
            }
            icvWriteVecSample( vec, sample );

                        if ( num > 0 && total >= num ) break;
        }

        if ( num<=0 )
            icvWriteVecHeader( vec, total, sample->width, sample->height );

        if( src )
        {
            cvReleaseImage( &src );
        }

        if( error )
        {

#if CV_VERBOSE
            fprintf( stderr, "%s(%d) : parse error", infoname, line );
#endif /* CV_VERBOSE */

            break;
        }

        }

    if( sample )
    {
        cvReleaseImage( &sample );
    }

    fclose( vec );
    fclose( info );

    return total;
}
票数 8
EN

Stack Overflow用户

发布于 2014-04-04 16:27:19

我费了很大力气才让opencv_createsamples在我的windows机器上工作,但它还是成功地工作了。我想我会把这些细节贴出来帮助其他人。我正在使用opencv 2.4.8 on Windows 7

首先,我发现需要使用path变量中作为OPENCV_DIR列出的目录中的opencv_createsamples.exe文件。我无法将exe文件复制到某个更方便的位置。

我在这个目录中为我的图像和文本文件设置了一个子目录text_classifier。我在命令行中使用了以下命令:

F:\Apps\opencv\build\x64\vc10\bin>opencv_createsamples.exe -vec text_classifier\text_binary_desc -info text_classifier\positive_examples.txt -num 146 -w 1350 -h 900 -bg text_classifier\negative_samples.txt

请注意,我必须列出图像中选定区域的数量(-num 146)。我还列出了阳性样本的宽度和高度。

在文件positive_examples.txt中,我需要列出这些文件,如下所示:

text_positives_clean\1839_Boettiger_001.JPG 1 708 35 471 793

换句话说,必须相对于文件positive_examples.txt而不是相对于exe文件(opencv_createsamples.exe)列出这些文件。当我尝试列出与exe相关的文件时,例如:

text_classifier\text_positives_clean\1839_Boettiger_001.JPG 1 708 35 471 793

然后我收到错误: Unable to open image: text_classifier\text_classifier\text_positives_clean\1839_Boettiger_001.JPG

然后我注意到,我的创建这个文件的特殊自动化系统不知何故错过了将一些文件加载到目录中,因此positive_examples.txt中列出的文件不在目录中。如果发现positive_examples.txt中列出的内容不在目录中,可执行文件就会崩溃。我填补了图像目录中的空白。

然后我收到一个奇怪的错误:无法打开图像: 129

我发现我犯了这样的错误:

text_positives_clean\1862_Streckfuss_0006.JPG 1 813 502 382 353 129 46 526 798 682 780 117 67

你是否注意到这里所说的选择区域的数量是1(紧跟在‘JPG’后面的数字),而选择区域的数量实际上是3?因此,在获得单个选定区域后,opencv_createsamples.exe尝试打开它找到的下一个内容作为图像,即'129‘。然后它又倒下了。

所以我把1改成了3,然后我得到了一个解析错误,实际上在我的positive_examples.txt文件中给了我一个行号。我转到该行,发现我的一个条目在所选区域之间没有空格,例如:

949 315 157 67131 30 513 806

我修复了这个问题,添加了空格,最后exe完成了我所有的146个样本。哇哦!

希望这对某些人有帮助。:-)

票数 4
EN

Stack Overflow用户

发布于 2011-07-01 23:01:03

Target1.JPG -它必须是createsamples.exe的相对路径。不是给Target.txt的。

我已经在我的机器上测试过了,得到了这样的结果:

代码语言:javascript
复制
d:\Programs\OpenCV-2.2.0\msvs-build\bin\Release>opencv_createsamples.exe -info "d:\Programs\OpenCV-2.2.0\msvs-build\bin\Release\Target.dat" -num 10 -vec "d:\Programs\OpenCV-2.2.0\msvs-build\bin\Release\Target.vec" -maxxangle 0.6 -maxyangle 0 -maxzangle 0.3 -maxidev 100 -bgcolor 0 -bgthresh 0 -w 20 -h 20
Info file name: d:\Programs\OpenCV-2.2.0\msvs-build\bin\Release\Target.dat
Img file name: (NULL)
Vec file name: d:\Programs\OpenCV-2.2.0\msvs-build\bin\Release\Target.vec
BG  file name: (NULL)
Num: 10
BG color: 0
BG threshold: 0
Invert: FALSE
Max intensity deviation: 100
Max x angle: 0.6
Max y angle: 0
Max z angle: 0.3
Show samples: FALSE
Width: 20
Height: 20
Create training samples from images collection...
d:\Programs\OpenCV-2.2.0\msvs-build\bin\Release\Target.dat(3) : parse errorDone. **Created 2 samples**
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/6549052

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档