statisticsTrainingSet.cpp 6.4 KB

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