createNormTrainingSet.cpp 8.5 KB

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