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