Merge pull request #2887 from ilya-lavrenov:ipp_morph_fix
[platform/upstream/opencv.git] / 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 #include <ctime>
53
54 using namespace std;
55
56 #include "cvhaartraining.h"
57
58 int main( int argc, char* argv[] )
59 {
60     int i = 0;
61     char* nullname   = (char*)"(NULL)";
62     char* vecname    = NULL; /* .vec file name */
63     char* infoname   = NULL; /* file name with marked up image descriptions */
64     char* imagename  = NULL; /* single sample image */
65     char* bgfilename = NULL; /* background */
66     int num = 1000;
67     int bgcolor = 0;
68     int bgthreshold = 80;
69     int invert = 0;
70     int maxintensitydev = 40;
71     double maxxangle = 1.1;
72     double maxyangle = 1.1;
73     double maxzangle = 0.5;
74     int showsamples = 0;
75     /* the samples are adjusted to this scale in the sample preview window */
76     double scale = 4.0;
77     int width  = 24;
78     int height = 24;
79
80     srand((unsigned int)time(0));
81
82     if( argc == 1 )
83     {
84         printf( "Usage: %s\n  [-info <collection_file_name>]\n"
85                 "  [-img <image_file_name>]\n"
86                 "  [-vec <vec_file_name>]\n"
87                 "  [-bg <background_file_name>]\n  [-num <number_of_samples = %d>]\n"
88                 "  [-bgcolor <background_color = %d>]\n"
89                 "  [-inv] [-randinv] [-bgthresh <background_color_threshold = %d>]\n"
90                 "  [-maxidev <max_intensity_deviation = %d>]\n"
91                 "  [-maxxangle <max_x_rotation_angle = %f>]\n"
92                 "  [-maxyangle <max_y_rotation_angle = %f>]\n"
93                 "  [-maxzangle <max_z_rotation_angle = %f>]\n"
94                 "  [-show [<scale = %f>]]\n"
95                 "  [-w <sample_width = %d>]\n  [-h <sample_height = %d>]\n",
96                 argv[0], num, bgcolor, bgthreshold, maxintensitydev,
97                 maxxangle, maxyangle, maxzangle, scale, width, height );
98
99         return 0;
100     }
101
102     for( i = 1; i < argc; ++i )
103     {
104         if( !strcmp( argv[i], "-info" ) )
105         {
106             infoname = argv[++i];
107         }
108         else if( !strcmp( argv[i], "-img" ) )
109         {
110             imagename = argv[++i];
111         }
112         else if( !strcmp( argv[i], "-vec" ) )
113         {
114             vecname = argv[++i];
115         }
116         else if( !strcmp( argv[i], "-bg" ) )
117         {
118             bgfilename = argv[++i];
119         }
120         else if( !strcmp( argv[i], "-num" ) )
121         {
122             num = atoi( argv[++i] );
123         }
124         else if( !strcmp( argv[i], "-bgcolor" ) )
125         {
126             bgcolor = atoi( argv[++i] );
127         }
128         else if( !strcmp( argv[i], "-bgthresh" ) )
129         {
130             bgthreshold = atoi( argv[++i] );
131         }
132         else if( !strcmp( argv[i], "-inv" ) )
133         {
134             invert = 1;
135         }
136         else if( !strcmp( argv[i], "-randinv" ) )
137         {
138             invert = CV_RANDOM_INVERT;
139         }
140         else if( !strcmp( argv[i], "-maxidev" ) )
141         {
142             maxintensitydev = atoi( argv[++i] );
143         }
144         else if( !strcmp( argv[i], "-maxxangle" ) )
145         {
146             maxxangle = atof( argv[++i] );
147         }
148         else if( !strcmp( argv[i], "-maxyangle" ) )
149         {
150             maxyangle = atof( argv[++i] );
151         }
152         else if( !strcmp( argv[i], "-maxzangle" ) )
153         {
154             maxzangle = atof( argv[++i] );
155         }
156         else if( !strcmp( argv[i], "-show" ) )
157         {
158             showsamples = 1;
159             if( i+1 < argc && strlen( argv[i+1] ) > 0 && argv[i+1][0] != '-' )
160             {
161                 double d;
162                 d = strtod( argv[i+1], 0 );
163                 if( d != -HUGE_VAL && d != HUGE_VAL && d > 0 ) scale = d;
164                 ++i;
165             }
166         }
167         else if( !strcmp( argv[i], "-w" ) )
168         {
169             width = atoi( argv[++i] );
170         }
171         else if( !strcmp( argv[i], "-h" ) )
172         {
173             height = atoi( argv[++i] );
174         }
175     }
176
177     printf( "Info file name: %s\n", ((infoname == NULL) ?   nullname : infoname ) );
178     printf( "Img file name: %s\n",  ((imagename == NULL) ?  nullname : imagename ) );
179     printf( "Vec file name: %s\n",  ((vecname == NULL) ?    nullname : vecname ) );
180     printf( "BG  file name: %s\n",  ((bgfilename == NULL) ? nullname : bgfilename ) );
181     printf( "Num: %d\n", num );
182     printf( "BG color: %d\n", bgcolor );
183     printf( "BG threshold: %d\n", bgthreshold );
184     printf( "Invert: %s\n", (invert == CV_RANDOM_INVERT) ? "RANDOM"
185                                 : ( (invert) ? "TRUE" : "FALSE" ) );
186     printf( "Max intensity deviation: %d\n", maxintensitydev );
187     printf( "Max x angle: %g\n", maxxangle );
188     printf( "Max y angle: %g\n", maxyangle );
189     printf( "Max z angle: %g\n", maxzangle );
190     printf( "Show samples: %s\n", (showsamples) ? "TRUE" : "FALSE" );
191     if( showsamples )
192     {
193         printf( "Scale: %g\n", scale );
194     }
195     printf( "Width: %d\n", width );
196     printf( "Height: %d\n", height );
197
198     /* determine action */
199     if( imagename && vecname )
200     {
201         printf( "Create training samples from single image applying distortions...\n" );
202
203         cvCreateTrainingSamples( vecname, imagename, bgcolor, bgthreshold, bgfilename,
204                                  num, invert, maxintensitydev,
205                                  maxxangle, maxyangle, maxzangle,
206                                  showsamples, width, height );
207
208         printf( "Done\n" );
209     }
210     else if( imagename && bgfilename && infoname )
211     {
212         printf( "Create test samples from single image applying distortions...\n" );
213
214         cvCreateTestSamples( infoname, imagename, bgcolor, bgthreshold, bgfilename, num,
215             invert, maxintensitydev,
216             maxxangle, maxyangle, maxzangle, showsamples, width, height );
217
218         printf( "Done\n" );
219     }
220     else if( infoname && vecname )
221     {
222         int total;
223
224         printf( "Create training samples from images collection...\n" );
225
226         total = cvCreateTrainingSamplesFromInfo( infoname, vecname, num, showsamples,
227                                                  width, height );
228
229         printf( "Done. Created %d samples\n", total );
230     }
231     else if( vecname )
232     {
233         printf( "View samples from vec file (press ESC to exit)...\n" );
234
235         cvShowVecSamples( vecname, width, height, scale );
236
237         printf( "Done\n" );
238     }
239     else
240     {
241         printf( "Nothing to do\n" );
242     }
243
244     return 0;
245 }