linux下编译mergevec和vec2img

来源:互联网 发布:华为p8软件搬家 编辑:程序博客网 时间:2024/06/03 17:20

项目主页:http://note.sonots.com/SciSoftware/haartraining.html#v6f077ba

项目主页上有编译好的exe文件,但没有Linux下面编译好的二进制文件。


mergevec用于合并多个vec文件。

用法:

[plain] view plain copy
  1. mergevec  
  2.   <collection_file_of_vecs> #类似于创建样本和训练的描述文件,一个存有要合并的vec文件路径列表的文件,可用find命令查找并重定向生成  
  3.   <output_vec_filename> #输出vec文件名  
  4.   [-show] #与创建样本时的show参数一样,  
  5.   [-w <sample_width = 24>] #不解释  
  6.   [-h <sample_height = 24>] #不解释  

vec2img用于从vec文件中释放图像文件

用法:

[html] view plain copy
  1. vec2img  
  2.   <input_vec_filename> #指定vec文件  
  3.   <output_filename_format> #指定输出文件格式,是一个含有%d格式的文件名字串,扩展名决定图像文件的输出格式,如test_%04d.png  
  4.   [-w <sample_widthsample_width = 24>] #不解释  
  5.   [-h <sample_heightsample_height = 24>] #不解释  

编译:

下载两个文件的源码,因为都是单个文件,这里我贴出来:

mergevec.cpp

[cpp] view plain copy
  1. #include <cv.h>  
  2. #include <highgui.h>  
  3. #include <stdio.h>  
  4. #include <stdlib.h>  
  5. #include <math.h>  
  6.   
  7. #include "cvhaartraining.h"  
  8. #include "_cvhaartraining.h" // Load CvVecFile  
  9. // Write a vec header into the vec file (located at cvsamples.cpp)  
  10. void icvWriteVecHeader( FILE* file, int count, int width, int height );  
  11. // Write a sample image into file in the vec format (located at cvsamples.cpp)  
  12. void icvWriteVecSample( FILE* file, CvArr* sample );  
  13. // Append the body of the input vec to the ouput vec  
  14. void icvAppendVec( CvVecFile &in, CvVecFile &out, int *showsamples, int winwidth, int winheight );  
  15. // Merge vec files  
  16. void icvMergeVecs( char* infoname, const char* outvecname, int showsamples, int width, int height );  
  17.   
  18. // Append the body of the input vec to the ouput vec  
  19. void icvAppendVec( CvVecFile &in, CvVecFile &out, int *showsamples, int winwidth, int winheight )  
  20. {  
  21.     CvMat* sample;  
  22.   
  23.     if( *showsamples )  
  24.     {  
  25.         cvNamedWindow( "Sample", CV_WINDOW_AUTOSIZE );  
  26.     }  
  27.     if( !feof( in.input ) )  
  28.     {  
  29.         in.last = 0;  
  30.         in.vector = (short*) cvAlloc( sizeof( *in.vector ) * in.vecsize );  
  31.         if ( *showsamples )  
  32.         {  
  33.             if ( in.vecsize != winheight * winwidth )  
  34.             {  
  35.                 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 );  
  36.                 exit(1);  
  37.             }  
  38.             sample = cvCreateMat( winheight, winwidth, CV_8UC1 );  
  39.         }   
  40.         else   
  41.         {  
  42.             sample = cvCreateMat( in.vecsize, 1, CV_8UC1 );  
  43.         }  
  44.         forint i = 0; i < in.count; i++ )  
  45.         {  
  46.             icvGetHaarTraininDataFromVecCallback( sample, &in );  
  47.             icvWriteVecSample ( out.input, sample );  
  48.             if( *showsamples )  
  49.             {  
  50.                 cvShowImage( "Sample", sample );  
  51.                 if( cvWaitKey( 0 ) == 27 )  
  52.                 {   
  53.                     *showsamples = 0;   
  54.                 }  
  55.             }  
  56.         }  
  57.         cvReleaseMat( &sample );  
  58.         cvFree( (void**) &in.vector );  
  59.     }  
  60. }  
  61.   
  62. void icvMergeVecs( char* infoname, const char* outvecname, int showsamples, int width, int height )  
  63. {  
  64.     char onevecname[PATH_MAX];  
  65.     int i = 0;  
  66.     int filenum = 0;  
  67.     short tmp;   
  68.     FILE *info;  
  69.     CvVecFile outvec;  
  70.     CvVecFile invec;  
  71.     int prev_vecsize;  
  72.   
  73.     // fopen input and output file  
  74.     info = fopen( infoname, "r" );  
  75.     if ( info == NULL )  
  76.     {  
  77.         fprintf( stderr, "ERROR: Input file %s does not exist or not readable.\n", infoname );  
  78.         exit(1);  
  79.     }  
  80.     outvec.input = fopen( outvecname, "wb" );  
  81.     if ( outvec.input == NULL )  
  82.     {  
  83.         fprintf( stderr, "ERROR: Output file %s is not writable.\n", outvecname );  
  84.         exit(1);  
  85.     }  
  86.   
  87.     // Header  
  88.     rewind( info );  
  89.     outvec.count = 0;  
  90.     for ( filenum = 0; ; filenum++ )  
  91.     {  
  92.         if ( fscanf( info, "%s", onevecname ) == EOF )  
  93.         {  
  94.             break;  
  95.         }  
  96.         invec.input = fopen( onevecname, "rb" );  
  97.         if ( invec.input == NULL )  
  98.         {  
  99.             fprintf( stderr, "ERROR: Input file %s does not exist or not readable.\n", onevecname );  
  100.             exit(1);  
  101.         }  
  102.         fread( &invec.count,   sizeof( invec.count )  , 1, invec.input );  
  103.         fread( &invec.vecsize, sizeof( invec.vecsize ), 1, invec.input );  
  104.         fread( &tmp, sizeof( tmp ), 1, invec.input );  
  105.         fread( &tmp, sizeof( tmp ), 1, invec.input );  
  106.   
  107.         outvec.count += invec.count;  
  108.         if( i > 0 &&  invec.vecsize != prev_vecsize )  
  109.         {  
  110.             fprintf( stderr, "ERROR: The size of images in %s(%d) is different with the previous vec file(%d).\n", onevecname, invec.vecsize, prev_vecsize );  
  111.             exit(1);  
  112.         }  
  113.         prev_vecsize = invec.vecsize;  
  114.         fclose( invec.input );  
  115.     }  
  116.     outvec.vecsize = invec.vecsize;  
  117.     icvWriteVecHeader( outvec.input, outvec.count, outvec.vecsize, 1);  
  118.   
  119.     // Contents  
  120.     rewind( info );  
  121.     outvec.count = 0;  
  122.     for ( i = 0; i < filenum ; i++ )  
  123.     {  
  124.         if (fscanf( info, "%s", onevecname ) == EOF) {  
  125.             break;  
  126.         }  
  127.         invec.input = fopen( onevecname, "rb" );  
  128.         fread( &invec.count,   sizeof( invec.count )  , 1, invec.input );  
  129.         fread( &invec.vecsize, sizeof( invec.vecsize ), 1, invec.input );  
  130.         fread( &tmp, sizeof( tmp ), 1, invec.input );  
  131.         fread( &tmp, sizeof( tmp ), 1, invec.input );  
  132.   
  133.         icvAppendVec( invec, outvec, &showsamples, width, height );  
  134.         fclose( invec.input );  
  135.     }  
  136.     fclose( outvec.input );  
  137. }  
  138.   
  139. int main( int argc, char **argv )   
  140. {  
  141.     int i;  
  142.     char *infoname   = NULL;  
  143.     char *outvecname = NULL;  
  144.     int showsamples  = 0;  
  145.     int width        = 24;  
  146.     int height       = 24;  
  147.   
  148.     if( argc == 1 )  
  149.     {  
  150.         printf( "Usage: %s\n  <collection_file_of_vecs>\n"  
  151.             "  <output_vec_filename>\n"  
  152.             "  [-show] [-w <sample_width = %d>] [-h <sample_height = %d>]\n",  
  153.             argv[0], width, height );  
  154.         return 0;  
  155.     }  
  156.     for( i = 1; i < argc; ++i )  
  157.     {  
  158.         if( !strcmp( argv[i], "-show" ) )  
  159.         {  
  160.             showsamples = 1;  
  161.             // width = atoi( argv[++i] ); // obsolete -show width height  
  162.             // height = atoi( argv[++i] );  
  163.         }   
  164.         else if( !strcmp( argv[i], "-w" ) )  
  165.         {  
  166.             width = atoi( argv[++i] );  
  167.         }   
  168.         else if( !strcmp( argv[i], "-h" ) )  
  169.         {  
  170.             height = atoi( argv[++i] );  
  171.         }  
  172.         else if( argv[i][0] == '-' )  
  173.         {  
  174.             fprintf( stderr, "ERROR: The option %s does not exist. n", argv[i] );  
  175.             exit(1);  
  176.         }  
  177.         else if( infoname == NULL )  
  178.         {  
  179.             infoname = argv[i];  
  180.         }  
  181.         else if( outvecname == NULL )  
  182.         {  
  183.             outvecname = argv[i];  
  184.         }  
  185.     }  
  186.     if( infoname == NULL )  
  187.     {  
  188.         fprintf( stderr, "ERROR: No input file\n" );  
  189.         exit(1);  
  190.     }  
  191.     if( outvecname == NULL )  
  192.     {  
  193.         fprintf( stderr, "ERROR: No output file\n" );  
  194.         exit(1);  
  195.     }  
  196.     icvMergeVecs( infoname, outvecname, showsamples, width, height );  
  197.     return 0;  
  198. }  


vec2img.cpp
[cpp] view plain copy
  1. #include <cv.h>  
  2. #include <highgui.h>  
  3. #include <stdio.h>  
  4. #include <stdlib.h>  
  5. #include <math.h>  
  6.   
  7. #include "cvhaartraining.h"  
  8. #include "_cvhaartraining.h" // Load CvVecFile  
  9. void icvVec2Img( const char* vecname, const char* outformat, int width, int height );  
  10.   
  11. // Extract images from a vec file  
  12. void icvVec2Img( const char* vecname, const char* outformat, int width, int height )  
  13. {  
  14.     CvVecFile vec;  
  15.     CvMat* sample;  
  16.     char outfilename[PATH_MAX];  
  17.     short tmp;  
  18.   
  19.     vec.input = fopen( vecname, "rb" );  
  20.     if ( vec.input == NULL )  
  21.     {  
  22.         fprintf( stderr, "ERROR: Input file %s does not exist or not readable.\n", vecname);  
  23.         exit(1);  
  24.     }  
  25.     fread( &vec.count, sizeof( vec.count ), 1, vec.input );  
  26.     fread( &vec.vecsize, sizeof( vec.vecsize ), 1, vec.input );  
  27.     fread( &tmp, sizeof( tmp ), 1, vec.input );  
  28.     fread( &tmp, sizeof( tmp ), 1, vec.input );  
  29.   
  30.     if( !feof( vec.input ) )  
  31.     {  
  32.         vec.last = 0;  
  33.         vec.vector = (short*) cvAlloc( sizeof( *vec.vector ) * vec.vecsize );  
  34.         if( vec.vecsize != width * height )  
  35.         {  
  36.             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 );  
  37.             exit(1);  
  38.         }  
  39.         sample = cvCreateMat( height, width, CV_8UC1 );  
  40.         //cvNamedWindow( "Sample", CV_WINDOW_AUTOSIZE );  
  41.         forint i = 0; i < vec.count; i++ )  
  42.         {  
  43.             icvGetHaarTraininDataFromVecCallback( sample, &vec );  
  44.             sprintf( outfilename, outformat, i + 1 );  
  45.             printf( "%s\n", outfilename );  
  46.             cvSaveImage( outfilename, sample );  
  47.             //cvShowImage( "Sample", sample ); cvWaitKey( 0 );  
  48.         }  
  49.         cvReleaseMat( &sample );  
  50.         cvFree( (void**) &vec.vector );  
  51.     }  
  52.     fclose( vec.input );  
  53. }  
  54.   
  55. int main(int argc, char **argv){  
  56.     char *vecname   = NULL;  
  57.     char *outformat = NULL;  
  58.     int width       = 24;  
  59.     int height      = 24;  
  60.     int i           = 0;  
  61.   
  62.     if( argc == 1 )  
  63.     {  
  64.         printf( "Usage: %s\n  <input_vec_filename>\n"  
  65.             "  <output_filename_format>\n"  
  66.             "  [-w <sample_width = %d>] [-h <sample_height = %d>]\n",  
  67.             argv[0], width, height );  
  68.         printf( "The output filename format is a string having one %%d such as 'samples%%04d.png'.\n"  
  69.             "The image file format is automatically determined by the extension.\n" );  
  70.         return 0;  
  71.     }  
  72.     for( i = 1; i < argc; ++i )  
  73.     {  
  74.         if( !strcmp( argv[i], "-w" ) )  
  75.         {  
  76.             width = atoi( argv[++i] );  
  77.         }   
  78.         else if( !strcmp( argv[i], "-h" ) )  
  79.         {  
  80.             height = atoi( argv[++i] );  
  81.         }   
  82.         else if( vecname == NULL )  
  83.         {  
  84.             vecname = argv[i];  
  85.         }  
  86.         else if( outformat == NULL )  
  87.         {  
  88.             outformat = argv[i];  
  89.         }  
  90.     }  
  91.     if ( vecname == NULL )  
  92.     {  
  93.         fprintf( stderr, "ERROR: No input vec file. \n" );  
  94.         exit(1);  
  95.     }  
  96.     if ( outformat == NULL )  
  97.     {  
  98.         fprintf( stderr, "ERROR: Not output filename format. \n" );  
  99.         exit(1);  
  100.     }  
  101.     icvVec2Img( vecname, outformat, width, height );  
  102.     return 0;  
  103. }  


编译的话,将两个文件复制到opencv源码中的apps/haartraining/路径下

编译前,代码需要调整一下,将头文件

#include <cvhaartraining.h>
#include <_cvhaartraining.h> // Load CvVecFile

中的<>换成“”,让其在当前路径中寻找头文件,上面我贴出来的代码是已经调整过的。

编译命令:

mergevec.cpp

[plain] view plain copy
  1. $ g++ `pkg-config --cflags --libs opencv` -o mergevec mergevec.cpp cvboost.cpp cvcommon.cpp cvsamples.cpp cvhaarclassifier.cpp cvhaartraining.cpp  
vec2img.cpp
[plain] view plain copy
  1. $ g++ `pkg-config --cflags --libs opencv` -o vec2img vec2img.cpp cvboost.cpp cvcommon.cpp cvsamples.cpp cvhaarclassifier.cpp cvhaartraining.cpp