CodebookPrototypes.cpp 5.5 KB

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