statisticsTrainingSet.cpp 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  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/CrossT.h>
  12. #include <sys/errno.h>
  13. #include <core/basics/Config.h>
  14. #include <vislearning/baselib/cmdline.h>
  15. #include <vislearning/baselib/Preprocess.h>
  16. #include <core/vector/VVector.h>
  17. #include <vislearning/math/cluster/KMeans.h>
  18. #include <vislearning/cbaselib/MultiDataset.h>
  19. #include <vislearning/baselib/ProgressBar.h>
  20. #include <vislearning/baselib/Globals.h>
  21. using namespace OBJREC;
  22. // refactor-nice.pl: check this substitution
  23. // old: using namespace ice;
  24. using namespace NICE;
  25. using namespace std;
  26. /**
  27. save normalized object images
  28. */
  29. int main (int argc, char **argv)
  30. {
  31. #ifndef __clang__
  32. #ifndef __llvm__
  33. std::set_terminate(__gnu_cxx::__verbose_terminate_handler);
  34. #endif
  35. #endif
  36. char configfile [300];
  37. char objectclass_c [1024];
  38. char setname_c [1024];
  39. struct CmdLineOption options[] = {
  40. {"config", "use config file", NULL, "%s", configfile},
  41. {"ds", "use data set", "train", "%s", setname_c},
  42. {NULL, NULL, NULL, NULL, NULL}
  43. };
  44. int ret;
  45. char *more_options[argc];
  46. ret = parse_arguments( argc, (const char**)argv, options, more_options);
  47. fprintf (stderr, "data set name: %s\n", setname_c );
  48. if ( ret != 0 )
  49. {
  50. if ( ret != 1 ) fprintf (stderr, "Error parsing command line !\n");
  51. exit (-1);
  52. }
  53. Config conf ( configfile );
  54. Preprocess::Init ( &conf );
  55. MultiDataset md ( &conf );
  56. // refactor-nice.pl: check this substitution
  57. // old: string setname ( setname_c );
  58. std::string setname ( setname_c );
  59. const LabeledSet & ls = *(md[setname]);
  60. map<int, double> maxwidth;
  61. map<int, double> maxheight;
  62. map<int, double> minwidth;
  63. map<int, double> minheight;
  64. map<int, double> avgheight;
  65. map<int, double> avgwidth;
  66. map<int, int> count;
  67. map<int, VVector> objectpositions;
  68. const ClassNames & classNames = md.getClassNames( setname );
  69. // refactor-nice.pl: check this substitution
  70. // old: Image img = NewImg ( 1024, 1024, 255 );
  71. NICE::Image img (1024, 1024);
  72. // refactor-nice.pl: check this substitution
  73. // old: ClearImg(img);
  74. img.set(0);
  75. ProgressBar pb ("Statistics");
  76. pb.show();
  77. LOOP_ALL_S(ls)
  78. {
  79. EACH_INFO(classno,info);
  80. pb.update ( ls.count() );
  81. fprintf (stderr, "Filename %s\n", info.img().c_str());
  82. if ( ! info.hasLocalizationInfo() ) {
  83. fprintf (stderr, "No localization information available !!\n");
  84. exit(-1);
  85. }
  86. const LocalizationResult *l = info.localization();
  87. if ( l->size() <= 0 ) {
  88. fprintf (stderr, "No objects found in this image !!\n");
  89. exit(-1);
  90. }
  91. fprintf (stderr, "Analyzing bounding boxes\n");
  92. for ( LocalizationResult::const_iterator i = l->begin();
  93. i != l->end(); i++ )
  94. {
  95. SingleLocalizationResult *slr = *i;
  96. fprintf (stderr, "checking classno\n");
  97. assert ( slr->r != NULL );
  98. int c = slr->r->classno;
  99. fprintf (stderr, "getting bounding box\n");
  100. int xi, xa, yi, ya;
  101. slr->getBoundingBox ( xi, yi, xa, ya );
  102. if ( !NICE::isFinite(xi) || !NICE::isFinite(yi) || !NICE::isFinite(xa) || !NICE::isFinite(ya) )
  103. {
  104. fprintf (stderr, "illegal bounding box information: %s\n", info.img().c_str() );
  105. exit(-1);
  106. }
  107. double width = xa - xi;
  108. double height = ya - yi;
  109. if ( width <= 0 ) {
  110. fprintf (stderr, "negative width: %s !\n", info.img().c_str());
  111. exit(-1);
  112. }
  113. if ( height <= 0 ) {
  114. fprintf (stderr, "negative height %s !\n", info.img().c_str());
  115. exit(-1);
  116. }
  117. if ( objectpositions.find(c) == objectpositions.end() )
  118. objectpositions[c] = VVector();
  119. // refactor-nice.pl: check this substitution
  120. // old: objectpositions[c].push_back ( Vector(width, height) );
  121. // REFACTOR-FIXME Unable to std::map this statement
  122. if ( (minwidth.find(c) == minwidth.end()) || (minwidth[c] > width ) )
  123. minwidth[c] = width;
  124. if ( (maxwidth.find(c) == maxwidth.end()) || (maxwidth[c] < width ) )
  125. maxwidth[c] = width;
  126. if ( (minheight.find(c) == minheight.end()) || (minheight[c] > height ) )
  127. minheight[c] = height;
  128. if ( (maxheight.find(c) == maxheight.end()) || (maxheight[c] < height ) )
  129. maxheight[c] = height;
  130. if ( avgheight.find(c) == avgheight.end() )
  131. avgheight[c] = height;
  132. else
  133. avgheight[c] += height;
  134. if ( avgwidth.find(c) == avgwidth.end() )
  135. avgwidth[c] = width;
  136. else
  137. avgwidth[c] += width;
  138. if ( count.find(c) == count.end() )
  139. count[c] = 0;
  140. else
  141. count[c] ++;
  142. fprintf (stderr, "ready for the next file\n");
  143. }
  144. }
  145. fprintf (stderr, "-- Object Statistics --\n");
  146. for ( map<int, int>::iterator i = count.begin();
  147. i != count.end();
  148. i++ )
  149. {
  150. int c = i->first;
  151. int count = i->second;
  152. avgheight[c] /= count;
  153. avgwidth[c] /= count;
  154. fprintf (stderr, "[%s]\n", classNames.text(c).c_str() );
  155. fprintf (stderr, "width: min %f max %f avg %f\n", minwidth[c], maxwidth[c], avgwidth[c] );
  156. fprintf (stderr, "height: min %f max %f avg %f\n", minheight[c], maxheight[c], avgheight[c] );
  157. const VVector & pos = objectpositions[c];
  158. KMeans kmeans (4);
  159. VVector prototypes;
  160. vector<double> weights;
  161. vector<int> assignment;
  162. kmeans.cluster ( pos, prototypes, weights, assignment );
  163. // refactor-nice.pl: check this substitution
  164. // old: Image img = NewImg ( (int)maxwidth[c]+5, (int)maxheight[c]+5, 255 );
  165. NICE::Image img ((int)maxwidth[c]+5, (int)maxheight[c]+5);
  166. // refactor-nice.pl: check this substitution
  167. // old: ClearImg(img);
  168. img.set(0);
  169. for ( VVector::const_iterator j = pos.begin();
  170. j != pos.end(); j++ )
  171. {
  172. // refactor-nice.pl: check this substitution
  173. // old: const Vector & x = *j;
  174. const NICE::Vector & x = *j;
  175. Cross cross ( Coord((int)x[0], (int)x[1]), 4 );
  176. img.draw ( cross, 1 );
  177. }
  178. fprintf (stderr, "%s ", classNames.text(c).c_str() );
  179. for ( VVector::const_iterator j = prototypes.begin();
  180. j != prototypes.end(); j++ )
  181. {
  182. // refactor-nice.pl: check this substitution
  183. // old: const Vector & x = *j;
  184. const NICE::Vector & x = *j;
  185. Cross cross ( Coord((int)x[0], (int)x[1]), 4 );
  186. img.draw ( cross, 2 );
  187. fprintf (stderr, "%dx%d ", (int)round(x[0]), (int)round(x[1]) );
  188. }
  189. fprintf (stderr, "\n");
  190. #ifndef NOVISUAL
  191. NICE::showImageOverlay ( img, img );
  192. #endif
  193. }
  194. return 0;
  195. }