Ejemplo n.º 1
0
// Extract images from a vec file
void icvVec2Img( const char* vecname, const char* outformat, int width, int height )
{
    CvVecFile vec;
    CvMat* sample;
    char outfilename[PATH_MAX];
    short tmp;

    vec.input = fopen( vecname, "rb" );
    if ( vec.input == NULL )
    {
        fprintf( stderr, "ERROR: Input file %s does not exist or not readable.\n", vecname);
        exit(1);
    }
    fread( &vec.count, sizeof( vec.count ), 1, vec.input );
    fread( &vec.vecsize, sizeof( vec.vecsize ), 1, vec.input );
    fread( &tmp, sizeof( tmp ), 1, vec.input );
    fread( &tmp, sizeof( tmp ), 1, vec.input );

    if( !feof( vec.input ) )
    {
        vec.last = 0;
        vec.vector = (short*) cvAlloc( sizeof( *vec.vector ) * vec.vecsize );
        if( vec.vecsize != width * height )
        {
            fprintf( stderr, "ERROR: The size of images inside of vec files does not match with %d x %d, but %d. \n", height, width, vec.vecsize );
            exit(1);
        }
        sample = cvCreateMat( height, width, CV_8UC1 );
        //cvNamedWindow( "Sample", CV_WINDOW_AUTOSIZE );
        for( int i = 0; i < vec.count; i++ )
        {
            icvGetHaarTraininDataFromVecCallback( sample, &vec );
            sprintf( outfilename, outformat, i + 1 );
            printf( "%s\n", outfilename );
            cvSaveImage( outfilename, sample );
            //cvShowImage( "Sample", sample ); cvWaitKey( 0 );
        }
        cvReleaseMat( &sample );
        cvFree( (void**) &vec.vector );
    }
    fclose( vec.input );
}
Ejemplo n.º 2
0
// Append the body of the input vec to the ouput vec
void icvAppendVec( CvVecFile &in, CvVecFile &out, int *showsamples, int winwidth, int winheight )
{
    CvMat* sample;

    if( *showsamples )
    {
        cvNamedWindow( "Sample", CV_WINDOW_AUTOSIZE );
    }
    if( !feof( in.input ) )
    {
        in.last = 0;
        in.vector = (short*) cvAlloc( sizeof( *in.vector ) * in.vecsize );
        if ( *showsamples )
        {
            if ( in.vecsize != winheight * winwidth )
            {
                fprintf( stderr, "ERROR: -show: the size of images inside of vec files does not match with %d x %d, but %d\n", winheight, winwidth, in.vecsize );
                exit(1);
            }
            sample = cvCreateMat( winheight, winwidth, CV_8UC1 );
        } 
        else 
        {
            sample = cvCreateMat( in.vecsize, 1, CV_8UC1 );
        }
        for( int i = 0; i < in.count; i++ )
        {
            icvGetHaarTraininDataFromVecCallback( sample, &in );
            icvWriteVecSample ( out.input, sample );
            if( *showsamples )
            {
                cvShowImage( "Sample", sample );
                if( cvWaitKey( 0 ) == 27 )
                { 
                    *showsamples = 0; 
                }
            }
        }
        cvReleaseMat( &sample );
        cvFree( (void**) &in.vector );
    }
}
Ejemplo n.º 3
0
void cvShowVecSamples( const char* filename, int winwidth, int winheight,
                       double scale )
{
    CvVecFile file;
    short tmp;
    int i;
    CvMat* sample;

    tmp = 0;
    file.input = fopen( filename, "rb" );

    if( file.input != NULL )
    {
        size_t elements_read1 = fread( &file.count, sizeof( file.count ), 1, file.input );
        size_t elements_read2 = fread( &file.vecsize, sizeof( file.vecsize ), 1, file.input );
        size_t elements_read3 = fread( &tmp, sizeof( tmp ), 1, file.input );
        size_t elements_read4 = fread( &tmp, sizeof( tmp ), 1, file.input );
        CV_Assert(elements_read1 == 1 && elements_read2 == 1 && elements_read3 == 1 && elements_read4 == 1);

        if( file.vecsize != winwidth * winheight )
        {
            int guessed_w = 0;
            int guessed_h = 0;

            fprintf( stderr, "Warning: specified sample width=%d and height=%d "
                "does not correspond to .vec file vector size=%d.\n",
                winwidth, winheight, file.vecsize );
            if( file.vecsize > 0 )
            {
                guessed_w = cvFloor( sqrt( (float) file.vecsize ) );
                if( guessed_w > 0 )
                {
                    guessed_h = file.vecsize / guessed_w;
                }
            }

            if( guessed_w <= 0 || guessed_h <= 0 || guessed_w * guessed_h != file.vecsize)
            {
                fprintf( stderr, "Error: failed to guess sample width and height\n" );
                fclose( file.input );

                return;
            }
            else
            {
                winwidth = guessed_w;
                winheight = guessed_h;
                fprintf( stderr, "Guessed width=%d, guessed height=%d\n",
                    winwidth, winheight );
            }
        }

        if( !feof( file.input ) && scale > 0 )
        {
            CvMat* scaled_sample = 0;

            file.last = 0;
            file.vector = (short*) cvAlloc( sizeof( *file.vector ) * file.vecsize );
            sample = scaled_sample = cvCreateMat( winheight, winwidth, CV_8UC1 );
            if( scale != 1.0 )
            {
                scaled_sample = cvCreateMat( MAX( 1, cvCeil( scale * winheight ) ),
                                             MAX( 1, cvCeil( scale * winwidth ) ),
                                             CV_8UC1 );
            }
            cvNamedWindow( "Sample", CV_WINDOW_AUTOSIZE );
            for( i = 0; i < file.count; i++ )
            {
                icvGetHaarTraininDataFromVecCallback( sample, &file );
                if( scale != 1.0 ) cvResize( sample, scaled_sample, CV_INTER_LINEAR);
                cvShowImage( "Sample", scaled_sample );
                if( cvWaitKey( 0 ) == 27 ) break;
            }
            if( scaled_sample && scaled_sample != sample ) cvReleaseMat( &scaled_sample );
            cvReleaseMat( &sample );
            cvFree( &file.vector );
        }
        fclose( file.input );
    }
}