createNormTrainingSet.cpp 9.0 KB

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