createNormTrainingSet.cpp 8.9 KB

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