createNormTrainingSet.cpp 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  1. /**
  2. * @file calcNormTrainingSet.cpp
  3. * @brief save normalized object images
  4. * @author Erik Rodner
  5. * @date 07/21/2008
  6. */
  7. #include "core/vector/VectorT.h"
  8. #include "core/vector/MatrixT.h"
  9. #include "core/image/ImageT.h"
  10. #include "core/imagedisplay/ImageDisplay.h"
  11. #include <core/image/Convert.h>
  12. #include <sys/errno.h>
  13. #include <sys/stat.h>
  14. #include <sys/types.h>
  15. #include <core/basics/Config.h>
  16. #include <vislearning/baselib/cmdline.h>
  17. #include <vislearning/baselib/Preprocess.h>
  18. #include <vislearning/cbaselib/MultiDataset.h>
  19. #include <vislearning/baselib/ProgressBar.h>
  20. #include <vislearning/baselib/Globals.h>
  21. using namespace OBJREC;
  22. using namespace NICE;
  23. using namespace std;
  24. /**
  25. save normalized object images
  26. */
  27. int main (int argc, char **argv)
  28. {
  29. std::set_terminate(__gnu_cxx::__verbose_terminate_handler);
  30. char configfile [300];
  31. char objectclass_c [1024];
  32. char setname_c [1024];
  33. bool scaleToAVG = false;
  34. struct CmdLineOption options[] = {
  35. {"config", "use config file", NULL, "%s", configfile},
  36. {"ds", "use data set", "train", "%s", setname_c},
  37. {"class", "create pictures of", "", "%s", objectclass_c},
  38. {"scale", "scale pictures to average sizes", NULL, NULL, &scaleToAVG},
  39. {NULL, NULL, NULL, NULL, NULL}
  40. };
  41. int ret;
  42. char *more_options[argc];
  43. ret = parse_arguments( argc, (const char**)argv, options, more_options);
  44. fprintf (stderr, "data set name: %s\n", setname_c );
  45. if ( ret != 0 )
  46. {
  47. if ( ret != 1 ) fprintf (stderr, "Error parsing command line !\n");
  48. exit (-1);
  49. }
  50. Config conf ( configfile );
  51. Preprocess::Init ( &conf );
  52. MultiDataset md ( &conf );
  53. // refactor-nice.pl: check this substitution
  54. // old: string setname ( setname_c );
  55. std::string setname ( setname_c );
  56. const LabeledSet & ls = *(md[setname]);
  57. map<int, double> maxwidth;
  58. map<int, double> maxheight;
  59. map<int, double> minwidth;
  60. map<int, double> minheight;
  61. map<int, double> avgheight;
  62. map<int, double> avgwidth;
  63. map<int, int> count;
  64. const ClassNames & classNames = md.getClassNames( setname );
  65. int objectclassno;
  66. // refactor-nice.pl: check this substitution
  67. // old: string objectclass ( objectclass_c );
  68. std::string objectclass ( objectclass_c );
  69. if ( objectclass.size() > 0 )
  70. {
  71. cerr << "Object class " << objectclass << endl;
  72. objectclassno = classNames.classno(objectclass);
  73. if ( objectclassno < 0 ) {
  74. fprintf (stderr, "Unknown object class %s\n", objectclass_c );
  75. exit(-1);
  76. }
  77. } else {
  78. objectclassno = -1;
  79. }
  80. ProgressBar pb ("Statistics");
  81. pb.show();
  82. LOOP_ALL_S(ls)
  83. {
  84. EACH_INFO(classno,info);
  85. pb.update ( ls.count() );
  86. fprintf (stderr, "Filename %s\n", info.img().c_str());
  87. if ( ! info.hasLocalizationInfo() ) {
  88. fprintf (stderr, "No localization information available !!\n");
  89. exit(-1);
  90. }
  91. const LocalizationResult *l = info.localization();
  92. if ( l->size() <= 0 ) {
  93. fprintf (stderr, "No objects found in this image !!\n");
  94. exit(-1);
  95. }
  96. fprintf (stderr, "Analyzing bounding boxes\n");
  97. for ( LocalizationResult::const_iterator i = l->begin();
  98. i != l->end(); i++ )
  99. {
  100. SingleLocalizationResult *slr = *i;
  101. fprintf (stderr, "checking classno\n");
  102. assert ( slr->r != NULL );
  103. int c = slr->r->classno;
  104. if ( (objectclassno < 0 ) || (c == objectclassno) )
  105. {
  106. fprintf (stderr, "getting bounding box\n");
  107. int xi, xa, yi, ya;
  108. slr->getBoundingBox ( xi, yi, xa, ya );
  109. if ( !NICE::isFinite(xi) || !NICE::isFinite(yi) || !NICE::isFinite(xa) || !NICE::isFinite(ya) )
  110. {
  111. fprintf (stderr, "illegal bounding box information: %s\n", info.img().c_str() );
  112. exit(-1);
  113. }
  114. double width = xa - xi;
  115. double height = ya - yi;
  116. if ( width <= 0 ) {
  117. fprintf (stderr, "negative width: %s !\n", info.img().c_str());
  118. exit(-1);
  119. }
  120. if ( height <= 0 ) {
  121. fprintf (stderr, "negative height %s !\n", info.img().c_str());
  122. exit(-1);
  123. }
  124. if ( (minwidth.find(c) == minwidth.end()) || (minwidth[c] > width ) )
  125. minwidth[c] = width;
  126. if ( (maxwidth.find(c) == maxwidth.end()) || (maxwidth[c] > width ) )
  127. maxwidth[c] = width;
  128. if ( (minheight.find(c) == minheight.end()) || (minheight[c] > height ) )
  129. minheight[c] = height;
  130. if ( (maxheight.find(c) == maxheight.end()) || (maxheight[c] > height ) )
  131. maxheight[c] = height;
  132. if ( avgheight.find(c) == avgheight.end() )
  133. avgheight[c] = height;
  134. else
  135. avgheight[c] += height;
  136. if ( avgwidth.find(c) == avgwidth.end() )
  137. avgwidth[c] = width;
  138. else
  139. avgwidth[c] += width;
  140. if ( count.find(c) == count.end() )
  141. count[c] = 0;
  142. else
  143. count[c] ++;
  144. }
  145. fprintf (stderr, "ready for the next file\n");
  146. }
  147. }
  148. if ( (objectclassno >= 0) && (count.find(objectclassno) == count.end() ) )
  149. {
  150. fprintf (stderr, "NO examples of class %s found !!\n", objectclass.c_str());
  151. exit(-1);
  152. }
  153. fprintf (stderr, "-- Object Statistics --\n");
  154. for ( map<int, int>::iterator i = count.begin();
  155. i != count.end();
  156. i++ )
  157. {
  158. int c = i->first;
  159. int count = i->second;
  160. avgheight[c] /= count;
  161. avgwidth[c] /= count;
  162. // refactor-nice.pl: check this substitution
  163. // old: string dir = classNames.text(c);
  164. std::string dir = classNames.text(c);
  165. int retcode = mkdir ( dir.c_str(), 0700 );
  166. if ( (retcode < 0) && (retcode != EEXIST ) ) {
  167. fprintf (stderr, "Failed to create directory: %s\n", dir.c_str() );
  168. exit(-1);
  169. }
  170. fprintf (stderr, "[%s]\n", classNames.text(c).c_str() );
  171. fprintf (stderr, "width: min %f max %f avg %f\n", minwidth[c], maxwidth[c], avgwidth[c] );
  172. fprintf (stderr, "height: min %f max %f avg %f\n", minheight[c], maxheight[c], avgheight[c] );
  173. }
  174. pb.reset("Crop");
  175. double borderx = conf.gD("crop", "borderx", 0.2);
  176. double bordery = conf.gD("crop", "bordery", 0.2);
  177. int counter = 0;
  178. LOOP_ALL_S(ls)
  179. {
  180. EACH_INFO(classno,info);
  181. pb.update ( ls.count() );
  182. // refactor-nice.pl: check this substitution
  183. // old: string filename = info.img();
  184. std::string filename = info.img();
  185. if ( ! info.hasLocalizationInfo() ) {
  186. fprintf (stderr, "createNormTrainingSet: file %s has no localization information\n",
  187. filename.c_str() );
  188. exit(-1);
  189. }
  190. // refactor-nice.pl: check this substitution
  191. // old: ImageRGB img = Preprocess::ReadImgAdvRGB ( filename );
  192. NICE::ColorImage img = Preprocess::ReadImgAdvRGB ( filename );
  193. Globals::setCurrentImgFN ( filename );
  194. const LocalizationResult *l = info.localization();
  195. for ( LocalizationResult::const_iterator i = l->begin();
  196. i != l->end(); i++ )
  197. {
  198. SingleLocalizationResult *slr = *i;
  199. int c = slr->r->classno;
  200. if ( (objectclassno < 0) || (c == objectclassno) )
  201. {
  202. int xi, xa, yi, ya;
  203. slr->getBoundingBox ( xi, yi, xa, ya );
  204. double w = xa - xi;
  205. double h = ya - yi;
  206. if ( (w < 1) || (h < 1) ) {
  207. fprintf (stderr, "Illegal width or height: %s\n", filename.c_str() );
  208. exit(-1);
  209. }
  210. double dstwidth;
  211. double dstheight;
  212. if ( scaleToAVG )
  213. {
  214. double normwidth = avgwidth[c]*(1.0+borderx);
  215. double normheight = avgheight[c]*(1.0+bordery);
  216. dstwidth = normwidth;
  217. dstheight = normheight;
  218. } else {
  219. dstwidth = w;
  220. dstheight = h;
  221. }
  222. double bxi = xi - borderx / 2.0;
  223. double bxa = xa + borderx / 2.0;
  224. double byi = yi - bordery / 2.0;
  225. double bya = ya + bordery / 2.0;
  226. if ( bxi < 0.0 ) bxi = 0.0;
  227. if ( byi < 0.0 ) byi = 0.0;
  228. // refactor-nice.pl: check this substitution
  229. // old: if ( bxa > img.xsize() - 1 ) bxa = img.xsize() - 1;
  230. if ( bxa > img.width() - 1 ) bxa = img.width() - 1;
  231. // refactor-nice.pl: check this substitution
  232. // old: if ( bya > img.ysize() - 1 ) bya = img.ysize() - 1;
  233. if ( bya > img.height() - 1 ) bya = img.height() - 1;
  234. RectT<int> rect ( bxi, byi,
  235. bxa - bxi + 1, bya - byi + 1 );
  236. NICE::ColorImage *subImage = img.createSubImage ( rect );
  237. NICE::ColorImage dst ( (int)round(dstwidth), (int)round(dstheight) );
  238. scale ( *subImage, &dst );
  239. #ifndef NOVISUAL
  240. showImage(dst);
  241. #endif
  242. std::string dir = classNames.text(c);
  243. char imgfilename_s [1024];
  244. sprintf ( imgfilename_s, "%s/image_%06d.jpg", dir.c_str(), counter );
  245. // refactor-nice.pl: check this substitution
  246. // old: fprintf (stderr, "%s: %d x %d\n", imgfilename_s, dst.xsize(), dst.ysize() );
  247. fprintf (stderr, "%s: %d x %d\n", imgfilename_s, dst.width(), dst.height() );
  248. ImageFile imgf ( imgfilename_s );
  249. try {
  250. imgf.writer ( &dst );
  251. } catch ( Exception ) {
  252. fprintf (stderr, "Failed to write filename %s\n", imgfilename_s );
  253. exit(-1);
  254. }
  255. counter++;
  256. }
  257. }
  258. }
  259. return 0;
  260. }