statisticsTrainingSet.cpp 6.3 KB

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