createsamples.cpp 8.8 KB

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