CodebookPrototypes.cpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. /**
  2. * @file CodebookPrototypes.cpp
  3. * @brief feature CodebookPrototypes
  4. * @author Erik Rodner, Alexander Freytag
  5. * @date 05-06-2013 (dd-mm-yyyy ) (original: 02/15/2008)
  6. */
  7. #include <iostream>
  8. #include <assert.h>
  9. #include <core/image/Convert.h>
  10. #include "vislearning/math/distances/genericDistance.h"
  11. #include "CodebookPrototypes.h"
  12. using namespace OBJREC;
  13. using namespace std;
  14. using namespace NICE;
  15. CodebookPrototypes::CodebookPrototypes()
  16. {
  17. this->clear();
  18. this->distanceType = "euclidean";
  19. this->distancefunction = GenericDistanceSelection::selectDistance(distanceType);
  20. }
  21. CodebookPrototypes::CodebookPrototypes( const std::string & filename )
  22. {
  23. Codebook::read(filename);
  24. }
  25. CodebookPrototypes::CodebookPrototypes( const NICE::VVector & vv )
  26. {
  27. this->append ( vv, true /* _copyData*/ );
  28. reinit ( vv.size() );
  29. this->distanceType = "euclidean";
  30. this->distancefunction = GenericDistanceSelection::selectDistance(distanceType);
  31. }
  32. CodebookPrototypes::CodebookPrototypes( NICE::Config * _conf, const std::string & _section) : Codebook ( _conf, _section )
  33. {
  34. this->distanceType = _conf->gS( _section, "distanceType", "euclidean" );
  35. this->distancefunction = GenericDistanceSelection::selectDistance(distanceType);
  36. this->clear();
  37. }
  38. CodebookPrototypes::~CodebookPrototypes()
  39. {
  40. //NOTE the config does not need to be deleted, it is just a pointer to an external data structure
  41. }
  42. void CodebookPrototypes::copy ( const Codebook *codebook )
  43. {
  44. Codebook::copy ( codebook );
  45. VVector::clear();
  46. const CodebookPrototypes *codebookp = dynamic_cast<const CodebookPrototypes *> ( codebook );
  47. assert ( codebookp != NULL );
  48. insert ( begin(), codebookp->begin(), codebookp->end() );
  49. this->distanceType = this->p_conf->gS( this->s_section, "distanceType", "euclidean" );
  50. this->distancefunction = GenericDistanceSelection::selectDistance(distanceType);
  51. }
  52. void CodebookPrototypes::voteVQ ( const NICE::Vector & feature, int & codebookEntry, double & weight, double & distance ) const
  53. {
  54. const double *xp = feature.getDataPointer();
  55. size_t k = 0;
  56. double mindist = numeric_limits<double>::max();
  57. size_t my_cluster = 0;
  58. int len = feature.size();
  59. // iterate over all cluster centers
  60. for ( std::vector<NICE::Vector>::const_iterator i = this->begin();
  61. i != this->end();
  62. i++,k++ )
  63. {
  64. const NICE::Vector & cluster = *i;
  65. //slow ICE variant
  66. //double distance = (x - cluster).Length();
  67. // const double *clusterp = cluster.getDataPointer();
  68. double distance = this->distancefunction->calculate(*i, feature);
  69. //old version without distance function
  70. // for ( int i = 0 ; i < len ; i++ )
  71. // {
  72. // double h = clusterp[i] - xp[i];
  73. // distance += h*h;
  74. // }
  75. if ( distance < mindist )
  76. {
  77. my_cluster = k;
  78. mindist = distance;
  79. }
  80. }
  81. codebookEntry = my_cluster;
  82. weight = 1.0;
  83. distance = mindist;
  84. }
  85. void CodebookPrototypes::voteVA ( const NICE::Vector & feature, NICE::Vector & votes ) const
  86. {
  87. votes.set( 0.0 );
  88. //TODO
  89. }
  90. void CodebookPrototypes::voteVA ( const NICE::Vector & feature, NICE::SparseVector & votes ) const
  91. {
  92. votes.clear();
  93. //TODO
  94. }
  95. void CodebookPrototypes::add ( const Codebook *codebook )
  96. {
  97. informativeMeasure.append ( codebook->getInformativeMeasure() );
  98. thresholds.append ( codebook->getThresholds() );
  99. classnos.append ( codebook->getClassNos() );
  100. const CodebookPrototypes *codebookp = dynamic_cast< const CodebookPrototypes *> ( codebook );
  101. assert ( codebookp != NULL );
  102. insert ( begin(), codebookp->begin(), codebookp->end() );
  103. }
  104. Codebook *CodebookPrototypes::clone () const
  105. {
  106. //TODO !
  107. return (new CodebookPrototypes());
  108. }
  109. void CodebookPrototypes::clear ()
  110. {
  111. VVector::clear();
  112. Codebook::clear();
  113. }
  114. void CodebookPrototypes::restore ( istream & is, int format )
  115. {
  116. Codebook::restore ( is, format );
  117. VVector::restore ( is, format );
  118. std::cerr << "initial Codebook : " << std::endl; VVector::print ( std::cerr );
  119. }
  120. void CodebookPrototypes::store ( ostream & os, int format ) const
  121. {
  122. Codebook::store ( os, format );
  123. VVector::store ( os, format );
  124. }
  125. void CodebookPrototypes::displayCodebook ( int xsize, int ysize ) const
  126. {
  127. NICE::Image bigimg ( xsize * 5 , ysize * this->size() );
  128. bigimg.set(0);
  129. NICE::Image img_s (xsize, ysize);
  130. for ( int k = 0 ; k < (int)this->size() ; k++ )
  131. {
  132. NICE::Vector vimg = *(this->begin() + k);
  133. NICE::Image img ((int)sqrt((double)vimg.size()), (int)sqrt((double)vimg.size()));
  134. int i = 0;
  135. double max = - numeric_limits<double>::max();
  136. double min = numeric_limits<double>::min();
  137. for ( int y = 0 ; y < img.height() ; y++ )
  138. {
  139. for ( int x = 0 ; x < img.width() ; x++,i++ )
  140. {
  141. if ( max < vimg[i] )
  142. max = vimg[i];
  143. if ( min > vimg[i] )
  144. min = vimg[i];
  145. }
  146. }
  147. i = 0;
  148. for ( int y = 0 ; y < img.height() ; y++ )
  149. {
  150. for ( int x = 0 ; x < img.width() ; x++,i++ )
  151. {
  152. img.setPixel( x, y, (int)((vimg[i] - min)*255/(max-min)) );
  153. }
  154. }
  155. NICE::scale ( img, &img_s );
  156. for ( int y = 0 ; y < ysize ; y++ )
  157. {
  158. for ( int x = 0 ; x < xsize ; x++ )
  159. {
  160. bigimg.setPixel(x, y+k*ysize, img_s.getPixel(x,y) );
  161. }
  162. }
  163. {
  164. std::ostringstream os;
  165. os << "no: " << k;
  166. if ( (int)informativeMeasure.size() > k )
  167. os << " " << informativeMeasure[k];
  168. // refactor-nice.pl: check this substitution
  169. // old: Text ( os.str().c_str(), xsize+1, k*ysize + 1, 255, 0, bigimg );
  170. // REFACTOR-FIXME Unable to map this statement
  171. }
  172. }
  173. bigimg.write ("/tmp/display.bmp");
  174. }