Codebook.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. /**
  2. * @file Codebook.cpp
  3. * @brief feature codebook
  4. * @author Erik Rodner, Alexander Freytag
  5. * @date 05-06-2013 (dd-mm-yyyy ) (original: 02/15/2008)
  6. */
  7. #include <iostream>
  8. #include "vislearning/features/simplefeatures/Codebook.h"
  9. using namespace OBJREC;
  10. using namespace std;
  11. using namespace NICE;
  12. Codebook::Codebook ( )
  13. {
  14. this->i_noOfNearestClustersToConsidere = 1;
  15. }
  16. Codebook::Codebook ( const int & _noOfNearestClustersToConsidere )
  17. {
  18. this->i_noOfNearestClustersToConsidere = _noOfNearestClustersToConsidere;
  19. }
  20. Codebook::Codebook( NICE::Config * _conf, const std::string & _section)
  21. {
  22. this->p_conf = _conf;
  23. this->s_section = _section;
  24. this->i_noOfNearestClustersToConsidere = this->p_conf->gI( _section, "noOfNearestClustersToConsidere", 1);
  25. }
  26. Codebook::~Codebook ( )
  27. {
  28. //NOTE the config does not need to be deleted, it is just a pointer to an external data structure
  29. }
  30. void Codebook::voteVQ (const NICE::Vector &feature, NICE::Vector &histogram, int &codebookEntry, double &weight, double &distance) const
  31. {
  32. this->voteVQ ( feature, codebookEntry, weight, distance );
  33. //VQ, but we directly increase the corresponding entry without setting histogram to zero before (useful if we work on multiple images)
  34. histogram[codebookEntry] += weight;
  35. }
  36. void Codebook::voteVQ ( const NICE::Vector & feature, NICE::SparseVector & histogram, int &codebookEntry, double &weight, double &distance ) const
  37. {
  38. this->voteVQ ( feature, codebookEntry, weight, distance );
  39. //is this cluster already non-zero in the histogram?
  40. NICE::SparseVector::iterator entryIt = histogram.find( codebookEntry ) ;
  41. if ( entryIt == histogram.end() )
  42. {
  43. //entry does not exist
  44. histogram.insert ( histogram.begin(), pair<int, double> ( codebookEntry, weight ) );
  45. }
  46. else
  47. {
  48. //entry already exists, so we increase the weight
  49. entryIt->second += weight;
  50. }
  51. }
  52. bool Codebook::allowsMultipleVoting () const
  53. {
  54. if ( this->i_noOfNearestClustersToConsidere > 1 )
  55. return true;
  56. else
  57. return false;
  58. }
  59. void Codebook::reinit ( int numCodebookEntries )
  60. {
  61. thresholds.resize ( numCodebookEntries );
  62. thresholds.set(0.0);
  63. informativeMeasure.resize ( numCodebookEntries );
  64. informativeMeasure.set(0.0);
  65. classnos.resize ( numCodebookEntries );
  66. classnos.resize(0);
  67. }
  68. void Codebook::clear ()
  69. {
  70. thresholds.clear();
  71. informativeMeasure.clear();
  72. classnos.clear();
  73. }
  74. void Codebook::restore ( istream & is, int format )
  75. {
  76. is >> thresholds;
  77. is >> informativeMeasure;
  78. // is >> classnos;
  79. //TODO use a flag for compatibility with old systems?
  80. /* try
  81. {
  82. is >> i_noOfNearestClustersToConsidere;
  83. is >> s_section;
  84. this->p_conf->restore( is, format );
  85. }
  86. catch( ... )
  87. {
  88. std::cerr << "something went wrong while Loading the codebook - use default values instead" << std::endl;
  89. //TODO use suitable default values
  90. } */
  91. }
  92. void Codebook::store ( ostream & os, int format ) const
  93. {
  94. os << this->thresholds << endl;
  95. os << this->informativeMeasure << endl;
  96. os << this->classnos << endl;
  97. // os << this->i_noOfNearestClustersToConsidere << endl;
  98. // os << this->s_section << endl;
  99. //
  100. // this->p_conf->store( os, format );
  101. }
  102. void Codebook::copy ( const Codebook *codebook )
  103. {
  104. this->reinit ( codebook->thresholds.size() );
  105. thresholds = codebook->thresholds;
  106. informativeMeasure = codebook->informativeMeasure;
  107. classnos = codebook->classnos;
  108. this->p_conf = codebook->p_conf;
  109. this->setNoOfNearestClustersToConsidere ( codebook->getNoOfNearestClustersToConsidere() );
  110. }
  111. void Codebook::setNoOfNearestClustersToConsidere ( const int & _noOfNearestClustersToConsidere )
  112. {
  113. this->i_noOfNearestClustersToConsidere = _noOfNearestClustersToConsidere;
  114. }
  115. int Codebook::getNoOfNearestClustersToConsidere ( ) const
  116. {
  117. return this->i_noOfNearestClustersToConsidere;
  118. }
  119. void Codebook::setHardAssignment( const bool & _hardAssignment )
  120. {
  121. this->b_hardAssignment = _hardAssignment;
  122. }
  123. bool Codebook::getHardAssignment ( ) const
  124. {
  125. return this->b_hardAssignment;
  126. }