FCCodebookHistBin.cpp 4.1 KB

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