Update to 2.0.0 tree from current Fremantle build
[opencv] / apps / haartraining / createsamples.cpp
1 /*M///////////////////////////////////////////////////////////////////////////////////////
2 //
3 //  IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
4 //
5 //  By downloading, copying, installing or using the software you agree to this license.
6 //  If you do not agree to this license, do not download, install,
7 //  copy or use the software.
8 //
9 //
10 //                        Intel License Agreement
11 //                For Open Source Computer Vision Library
12 //
13 // Copyright (C) 2000, Intel Corporation, all rights reserved.
14 // Third party copyrights are property of their respective owners.
15 //
16 // Redistribution and use in source and binary forms, with or without modification,
17 // are permitted provided that the following conditions are met:
18 //
19 //   * Redistribution's of source code must retain the above copyright notice,
20 //     this list of conditions and the following disclaimer.
21 //
22 //   * Redistribution's in binary form must reproduce the above copyright notice,
23 //     this list of conditions and the following disclaimer in the documentation
24 //     and/or other materials provided with the distribution.
25 //
26 //   * The name of Intel Corporation may not be used to endorse or promote products
27 //     derived from this software without specific prior written permission.
28 //
29 // This software is provided by the copyright holders and contributors "as is" and
30 // any express or implied warranties, including, but not limited to, the implied
31 // warranties of merchantability and fitness for a particular purpose are disclaimed.
32 // In no event shall the Intel Corporation or contributors be liable for any direct,
33 // indirect, incidental, special, exemplary, or consequential damages
34 // (including, but not limited to, procurement of substitute goods or services;
35 // loss of use, data, or profits; or business interruption) however caused
36 // and on any theory of liability, whether in contract, strict liability,
37 // or tort (including negligence or otherwise) arising in any way out of
38 // the use of this software, even if advised of the possibility of such damage.
39 //
40 //M*/
41
42 /*
43  * createsamples.cpp
44  *
45  * Create test/training samples
46  */
47
48 #include <cstdio>
49 #include <cstring>
50 #include <cstdlib>
51 #include <cmath>
52
53 #include "cvhaartraining.h"
54
55 int main( int argc, char* argv[] )
56 {
57     int i = 0;
58     char* nullname   = (char*)"(NULL)";
59     char* vecname    = NULL; /* .vec file name */
60     char* infoname   = NULL; /* file name with marked up image descriptions */
61     char* imagename  = NULL; /* single sample image */
62     char* bgfilename = NULL; /* background */
63     int num = 1000;
64     int bgcolor = 0;
65     int bgthreshold = 80;
66     int invert = 0;
67     int maxintensitydev = 40;
68     double maxxangle = 1.1;
69     double maxyangle = 1.1;
70     double maxzangle = 0.5;
71     int showsamples = 0;
72     /* the samples are adjusted to this scale in the sample preview window */
73     double scale = 4.0;
74     int width  = 24;
75     int height = 24;
76
77     if( argc == 1 )
78     {
79         printf( "Usage: %s\n  [-info <collection_file_name>]\n"
80                 "  [-img <image_file_name>]\n"
81                 "  [-vec <vec_file_name>]\n"
82                 "  [-bg <background_file_name>]\n  [-num <number_of_samples = %d>]\n"
83                 "  [-bgcolor <background_color = %d>]\n"
84                 "  [-inv] [-randinv] [-bgthresh <background_color_threshold = %d>]\n"
85                 "  [-maxidev <max_intensity_deviation = %d>]\n"
86                 "  [-maxxangle <max_x_rotation_angle = %f>]\n"
87                 "  [-maxyangle <max_y_rotation_angle = %f>]\n"
88                 "  [-maxzangle <max_z_rotation_angle = %f>]\n"
89                 "  [-show [<scale = %f>]]\n"
90                 "  [-w <sample_width = %d>]\n  [-h <sample_height = %d>]\n",
91                 argv[0], num, bgcolor, bgthreshold, maxintensitydev,
92                 maxxangle, maxyangle, maxzangle, scale, width, height );
93
94         return 0;
95     }
96
97     for( i = 1; i < argc; ++i )
98     {
99         if( !strcmp( argv[i], "-info" ) )
100         {
101             infoname = argv[++i];
102         }
103         else if( !strcmp( argv[i], "-img" ) )
104         {
105             imagename = argv[++i];
106         }
107         else if( !strcmp( argv[i], "-vec" ) )
108         {
109             vecname = argv[++i];
110         }
111         else if( !strcmp( argv[i], "-bg" ) )
112         {
113             bgfilename = argv[++i];
114         }
115         else if( !strcmp( argv[i], "-num" ) )
116         {
117             num = atoi( argv[++i] );
118         }
119         else if( !strcmp( argv[i], "-bgcolor" ) )
120         {
121             bgcolor = atoi( argv[++i] );
122         }
123         else if( !strcmp( argv[i], "-bgthresh" ) )
124         {
125             bgthreshold = atoi( argv[++i] );
126         }
127         else if( !strcmp( argv[i], "-inv" ) )
128         {
129             invert = 1;
130         }
131         else if( !strcmp( argv[i], "-randinv" ) )
132         {
133             invert = CV_RANDOM_INVERT;
134         }
135         else if( !strcmp( argv[i], "-maxidev" ) )
136         {
137             maxintensitydev = atoi( argv[++i] );
138         }
139         else if( !strcmp( argv[i], "-maxxangle" ) )
140         {
141             maxxangle = atof( argv[++i] );
142         }
143         else if( !strcmp( argv[i], "-maxyangle" ) )
144         {
145             maxyangle = atof( argv[++i] );
146         }
147         else if( !strcmp( argv[i], "-maxzangle" ) )
148         {
149             maxzangle = atof( argv[++i] );
150         }
151         else if( !strcmp( argv[i], "-show" ) )
152         {
153             showsamples = 1;
154             if( i+1 < argc && strlen( argv[i+1] ) > 0 && argv[i+1][0] != '-' )
155             {
156                 double d;
157                 d = strtod( argv[i+1], 0 );
158                 if( d != -HUGE_VAL && d != HUGE_VAL && d > 0 ) scale = d;
159                 ++i;
160             }
161         }
162         else if( !strcmp( argv[i], "-w" ) )
163         {
164             width = atoi( argv[++i] );
165         }
166         else if( !strcmp( argv[i], "-h" ) )
167         {
168             height = atoi( argv[++i] );
169         }
170     }
171
172     printf( "Info file name: %s\n", ((infoname == NULL) ?   nullname : infoname ) );
173     printf( "Img file name: %s\n",  ((imagename == NULL) ?  nullname : imagename ) );
174     printf( "Vec file name: %s\n",  ((vecname == NULL) ?    nullname : vecname ) );
175     printf( "BG  file name: %s\n",  ((bgfilename == NULL) ? nullname : bgfilename ) );
176     printf( "Num: %d\n", num );
177     printf( "BG color: %d\n", bgcolor );
178     printf( "BG threshold: %d\n", bgthreshold );
179     printf( "Invert: %s\n", (invert == CV_RANDOM_INVERT) ? "RANDOM"
180                                 : ( (invert) ? "TRUE" : "FALSE" ) );
181     printf( "Max intensity deviation: %d\n", maxintensitydev );
182     printf( "Max x angle: %g\n", maxxangle );
183     printf( "Max y angle: %g\n", maxyangle );
184     printf( "Max z angle: %g\n", maxzangle );
185     printf( "Show samples: %s\n", (showsamples) ? "TRUE" : "FALSE" );
186     if( showsamples )
187     {
188         printf( "Scale: %g\n", scale );
189     }
190     printf( "Width: %d\n", width );
191     printf( "Height: %d\n", height );
192
193     /* determine action */
194     if( imagename && vecname )
195     {
196         printf( "Create training samples from single image applying distortions...\n" );
197
198         cvCreateTrainingSamples( vecname, imagename, bgcolor, bgthreshold, bgfilename,
199                                  num, invert, maxintensitydev,
200                                  maxxangle, maxyangle, maxzangle,
201                                  showsamples, width, height );
202
203         printf( "Done\n" );
204     }
205     else if( imagename && bgfilename && infoname )
206     {
207         printf( "Create test samples from single image applying distortions...\n" );
208
209         cvCreateTestSamples( infoname, imagename, bgcolor, bgthreshold, bgfilename, num,
210             invert, maxintensitydev,
211             maxxangle, maxyangle, maxzangle, showsamples, width, height );
212
213         printf( "Done\n" );
214     }
215     else if( infoname && vecname )
216     {
217         int total;
218
219         printf( "Create training samples from images collection...\n" );
220
221         total = cvCreateTrainingSamplesFromInfo( infoname, vecname, num, showsamples,
222                                                  width, height );
223
224         printf( "Done. Created %d samples\n", total );
225     }
226     else if( vecname )
227     {
228         printf( "View samples from vec file (press ESC to exit)...\n" );
229
230         cvShowVecSamples( vecname, width, height, scale );
231
232         printf( "Done\n" );
233     }
234     else
235     {
236         printf( "Nothing to do\n" );
237     }
238
239     return 0;
240 }