Codebook.h 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. /**
  2. * @file Codebook.h
  3. * @brief feature codebook
  4. * @author Erik Rodner, Alexander Freytag
  5. * @date 05-06-2013 (dd-mm-yyyy ) (original: 02/15/2008)
  6. */
  7. #ifndef CODEBOOKINCLUDE
  8. #define CODEBOOKINCLUDE
  9. #include <string>
  10. #include <core/basics/Config.h>
  11. #include "core/basics/Persistent.h"
  12. //
  13. #include "core/image/ImageT.h"
  14. #include "core/imagedisplay/ImageDisplay.h"
  15. //
  16. #include "core/vector/VectorT.h"
  17. #include "core/vector/MatrixT.h"
  18. #include "core/vector/SparseVectorT.h"
  19. namespace OBJREC {
  20. /** feature codebook */
  21. class Codebook : virtual public NICE::Persistent
  22. {
  23. protected:
  24. NICE::Vector thresholds;
  25. NICE::Vector informativeMeasure;
  26. NICE::VectorT<int> classnos;
  27. /** if Soft Assignment or Hard Assignment instead of Vector Quantization, how many entries are allowed to be non-zero?*/
  28. int i_noOfNearestClustersToConsidere;
  29. NICE::Config * p_conf;
  30. std::string s_section;
  31. /** hard or soft assignment? */
  32. bool b_hardAssignment;
  33. public:
  34. /**
  35. * @brief simple constructor
  36. */
  37. Codebook ( );
  38. /**
  39. * @brief standard constructor
  40. */
  41. Codebook ( const int & _noOfNearestClustersToConsidere);
  42. /**
  43. * @brief recommended constructor
  44. */
  45. Codebook( NICE::Config * _conf, const std::string & _section);
  46. /** simple destructor */
  47. virtual ~Codebook();
  48. //vote for a single feature vector
  49. /**
  50. * @brief Vector Quantization for a single vector
  51. * @date 11-06-2013 (dd-mm-yyyy)
  52. * @author Erik Rodner, Alexander Freytag
  53. * @param feature the feature that shall be quantized
  54. * @param codebookEntry the corresponding cluster index for the quantized feature
  55. * @param weight the weight of the nearest cluster
  56. * @param distance the distance to its nearest cluster
  57. */
  58. virtual void voteVQ ( const NICE::Vector & feature, int & codebookEntry,
  59. double & weight, double & distance ) const = 0;
  60. /**
  61. * @brief Vector Quantization for a single vector, directly increases the corresponding entry in histogram
  62. * @date 11-06-2013 (dd-mm-yyyy)
  63. * @author Erik Rodner, Alexander Freytag
  64. * @param feature the feature that shall be quantized
  65. * @param histogram a BoW-histogram already given (does not need to be empty before) - the corresponding entry for feature will be increased
  66. * @param codebookEntry the corresponding cluster index for the quantized feature
  67. * @param weight the weight of the nearest cluster
  68. * @param distance the distance to its nearest cluster
  69. */
  70. virtual void voteVQ (const NICE::Vector &feature, NICE::Vector &histogram, int & codebookEntry, double & weight, double & distance ) const;
  71. /**
  72. * @brief Vector Quantization for a single vector, directly increases the corresponding entry in histogram
  73. * @date 11-06-2013 (dd-mm-yyyy)
  74. * @author Erik Rodner, Alexander Freytag
  75. * @param feature the feature that shall be quantized
  76. * @param histogram a BoW-histogram already given (does not need to be empty before) - the corresponding entry for feature will be increased
  77. * @param codebookEntry the corresponding cluster index for the quantized feature
  78. * @param weight the weight of the nearest cluster
  79. * @param distance the distance to its nearest cluster
  80. */
  81. virtual void voteVQ (const NICE::Vector &feature, NICE::SparseVector &histogram, int &codebookEntry, double &weight, double &distance ) const;
  82. /**
  83. * @brief Hard or Soft Assignment into NICE::Vector for a single vector
  84. * @date 11-06-2013 (dd-mm-yyyy)
  85. * @author Alexander Freytag
  86. * @param feature the feature that shall be quantized
  87. * @param votes BoW histogram adding non-zero entries at the k nearest clusters (hard or soft), (does not need to be empty before)
  88. */
  89. virtual void voteVA ( const NICE::Vector & feature, NICE::Vector & votes ) const = 0;
  90. /**
  91. * @brief Hard or Soft Assignment into NICE::SparseVector for a single vector
  92. * @date 11-06-2013 (dd-mm-yyyy)
  93. * @author Erik Rodner, Alexander Freytag
  94. * @param feature the feature that shall be quantized
  95. * @param votes BoW histogram adding non-zero entries for the k nearest clusters (hard or soft), (does not need to be empty before)
  96. */
  97. virtual void voteVA ( const NICE::Vector & feature, NICE::SparseVector & votes ) const = 0;
  98. //for backward-compatibility
  99. virtual void vote (const NICE::Vector &feature, NICE::Vector &histogram, int &codebookEntry , double &weight , double &distance ) const {
  100. this->voteVQ ( feature, histogram, codebookEntry, weight, distance );
  101. }
  102. virtual void vote (const NICE::Vector &feature, NICE::SparseVector &histogram ) const {
  103. int codebookEntry;
  104. double weight, distance;
  105. this->voteVQ ( feature, histogram, codebookEntry, weight, distance );
  106. }
  107. virtual void add ( const Codebook *codebook ) = 0;
  108. virtual void copy ( const Codebook *codebook );
  109. virtual Codebook *clone () const = 0;
  110. size_t getCodebookSize() const {
  111. return informativeMeasure.size();
  112. };
  113. void reinit ( int numCodebookEntries );
  114. const NICE::Vector & getThresholds () const {
  115. return thresholds;
  116. };
  117. const NICE::Vector & getInformativeMeasure () const {
  118. return informativeMeasure;
  119. };
  120. const NICE::VectorT<int> & getClassNos () const {
  121. return classnos;
  122. };
  123. void setThresholds ( const NICE::Vector & _thresholds ) {
  124. thresholds = _thresholds;
  125. };
  126. void setInformativeMeasure ( const NICE::Vector & _informativeMeasure ) {
  127. informativeMeasure = _informativeMeasure;
  128. };
  129. void setClassNos ( const NICE::VectorT<int> & _classnos ) {
  130. classnos = _classnos;
  131. };
  132. void setHardAssignment( const bool & _hardAssignment );
  133. bool getHardAssignment ( ) const ;
  134. /**
  135. * @brief false, if only a single entry for soft or hard assignment can be larger than zero (=> effectively vector quantization)
  136. */
  137. virtual bool allowsMultipleVoting () const ;
  138. void setNoOfNearestClustersToConsidere ( const int & _noOfNearestClustersToConsidere );
  139. int getNoOfNearestClustersToConsidere ( ) const ;
  140. virtual void clear ();
  141. virtual void restore ( std::istream & is, int format );
  142. virtual void store ( std::ostream & os, int format ) const;
  143. };
  144. } // namespace
  145. #endif