OpenCV创建示例示例

Seb*_*Seb 8 opencv

我试图createsamples从OpenCV库运行示例.我可以一次加载一个图像,它似乎工作正常.但是,当我尝试加载图像集合时,我得到一个解析错误.我不确定我的收藏文件中是否存在无效或我在其他地方遗漏的内容.以下是我的文本文档的确切格式.

文字文件详情:

Target1.JPG 1 0 0 1296 1152
Target2.jpg 1 0 0 1890 709
Run Code Online (Sandbox Code Playgroud)

命令行调用:

-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
Run Code Online (Sandbox Code Playgroud)

任何帮助是极大的赞赏.

Yiq*_* Hu 7

解析错误是因为当您未指定要生成的pos图像样本数时,createsamples将使用默认值1000.但是如果注释文本文档包含少于1000个对象的边界框,您将获得解析错误.您仍然可以使用.vec文件进行培训级联.唯一的问题是号码信息不正确.有两种方法可以解决它.

  1. 您手动计算文本文档中对象边界框的数量.并指定小于或等于选项"-num"的数字的值,例如createsamples -info xxxxx.txt -vec pos.vec -num [value]

  2. 您可以修改OPENCV_ROOT_DIR/modules/haartraining/createsamples.cpp.如果未指定-num,请将pos样本数设置为文本文档中对象边界框的数量

code snippet:在createsamples.cpp中int num = 0;

在cvsamples.cpp中

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;
}
Run Code Online (Sandbox Code Playgroud)


小智 5

我费尽心思opencv_createsamples在我的 Windows 机器上工作,但它运行得很成功。我想我会发布详细信息来帮助其他人。我在用opencv 2.4.8 on Windows 7

首先,我发现我需要使用opencv_createsamples.exe路径变量中列出的目录中的文件OPENCV_DIR. 我无法将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_positives_clean\1839_Boettiger_001.JPG 1 708 35 471 793

然后我收到错误:无法打开图像:text_classifier\text_classifier\text_positives_clean\1839_Boettiger_001.JPG

然后我注意到创建此文件的特殊自动化系统不知何故错过了将一些文件加载​​到目录中,因此 Positive_examples.txt 中列出的文件不在目录中。如果 exe 发现 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 个样本。哟呼!

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