FCCodebookHistBin.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. /**
  2. * @file FCCodebookHistBin.cpp
  3. * @brief create features with codebook
  4. * @author Erik Rodner
  5. * @date 02/05/2008
  6. */
  7. #ifdef NOVISUAL
  8. #include <vislearning/nice_nonvis.h>
  9. #else
  10. #include <vislearning/nice.h>
  11. #endif
  12. #include <iostream>
  13. #include <fstream>
  14. #include "vislearning/features/localfeatures/LFGenericLocal.h"
  15. #include "vislearning/features/simplefeatures/FCCodebookHistBin.h"
  16. using namespace OBJREC;
  17. using namespace std;
  18. using namespace NICE;
  19. FCCodebookHistBin::FCCodebookHistBin(
  20. const Config *conf,
  21. const LocalFeatureRepresentation *_lfrep,
  22. const std::string & normalizationMethod,
  23. const Codebook *_codebook )
  24. : FeatureFactory ( conf ), lfrep(_lfrep),
  25. codebook(_codebook)
  26. {
  27. if ( normalizationMethod == "sum" )
  28. n_method = NORMALIZE_SUM;
  29. else if ( normalizationMethod == "binzero" )
  30. n_method = NORMALIZE_BINZERO;
  31. else if ( normalizationMethod == "raw" )
  32. n_method = NORMALIZE_RAW;
  33. else if ( normalizationMethod == "thresh" )
  34. n_method = NORMALIZE_THRESH;
  35. else {
  36. fprintf (stderr, "FCCodebookHistBin::FCCodebookHistBin: unknown normalization method\n");
  37. exit(-1);
  38. }
  39. }
  40. FCCodebookHistBin::~FCCodebookHistBin()
  41. {
  42. }
  43. void FCCodebookHistBin::calcHistogram ( const VVector & features,
  44. NICE::Vector & histogram )
  45. {
  46. histogram.set(0);
  47. fprintf (stderr, "FCCodebookHistBin: features size = %d, codebook size = %d\n",
  48. (int)features.size(), (int)codebook->getCodebookSize());
  49. int cluster_index = 0;
  50. double weight = 0;
  51. double distance = 0.0;
  52. for ( vector<Vector>::const_iterator i = features.begin();
  53. i != features.end();
  54. i++ )
  55. {
  56. const NICE::Vector & x = *i;
  57. codebook->vote ( x, histogram, cluster_index, weight, distance );
  58. }
  59. }
  60. void FCCodebookHistBin::calcHistogram ( const VVector & features,
  61. NICE::Vector & histogram,
  62. NICE::Matrix & assignments )
  63. {
  64. histogram.set(0);
  65. size_t l = 0;
  66. int cluster_index = 0;
  67. double weight = 0;
  68. double distance;
  69. assignments.resize ( 3, features.size() );
  70. for ( vector<Vector>::const_iterator i = features.begin();
  71. i != features.end();
  72. i++,l++ )
  73. {
  74. const NICE::Vector & x = *i;
  75. codebook->vote ( x, histogram, cluster_index, weight, distance );
  76. histogram[cluster_index]++;
  77. assignments(0,l) = distance;
  78. assignments(1,l) = cluster_index;
  79. assignments(2,l) = l;
  80. }
  81. }
  82. void FCCodebookHistBin::normalizeHistogram ( NICE::Vector & histogram )
  83. {
  84. if ( n_method == NORMALIZE_RAW ) {
  85. // do nothing
  86. } else if ( n_method == NORMALIZE_BINZERO ) {
  87. for ( size_t i = 0 ; i < histogram.size() ; i++ )
  88. if ( histogram[i] > 0 ) histogram[i] = 1.0;
  89. } else if ( n_method == NORMALIZE_SUM ) {
  90. double sum = 0.0;
  91. for ( size_t i = 0 ; i < histogram.size() ; i++ )
  92. {
  93. assert ( histogram[i] >= 0.0 );
  94. sum += histogram[i];
  95. }
  96. if ( sum < 1e-5 ) {
  97. fprintf (stderr, "FCCodebookHistBin::normalizeHistogram: WARNING histogram is zero !!\n");
  98. return;
  99. }
  100. for ( size_t i = 0 ; i < histogram.size() ; i++ )
  101. histogram[i] /= sum;
  102. } else if ( n_method == NORMALIZE_THRESH ) {
  103. const NICE::Vector & thresholds = codebook->getThresholds();
  104. if ( thresholds.size() <= 0 ) {
  105. fprintf (stderr, "FCCodebookHistBin:: This is maybe an OLD codebook ! \n");
  106. exit(-1);
  107. }
  108. for ( size_t i = 0 ; i < histogram.size() ; i++ )
  109. histogram[i] = (histogram[i] > thresholds[i]) ? 1.0 : 0.0;
  110. }
  111. }
  112. int FCCodebookHistBin::convert ( const NICE::Image & img, NICE::Vector & vec )
  113. {
  114. VVector features;
  115. VVector positions;
  116. lfrep->extractFeatures ( img, features, positions );
  117. if ( features.size() <= 0 ) return -1;
  118. vec.resize(codebook->getCodebookSize());
  119. calcHistogram ( features, vec );
  120. normalizeHistogram ( vec );
  121. return 0;
  122. }
  123. int FCCodebookHistBin::calcAssignments ( const VVector & features, NICE::Vector & vec, NICE::Matrix & assignments )
  124. {
  125. if ( features.size() <= 0 ) return -1;
  126. assignments = Matrix(0,3);
  127. vec.resize(codebook->getCodebookSize());
  128. calcHistogram ( features, vec, assignments );
  129. normalizeHistogram ( vec );
  130. return 0;
  131. }
  132. int FCCodebookHistBin::getNormalizationMethod () const
  133. {
  134. return n_method;
  135. }
  136. void FCCodebookHistBin::setNormalizationMethod ( int normalizationMethod )
  137. {
  138. n_method = normalizationMethod;
  139. }