CodebookPrototypes.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. /**
  2. * @file CodebookPrototypes.cpp
  3. * @brief feature CodebookPrototypes
  4. * @author Erik Rodner
  5. * @date 02/15/2008
  6. */
  7. #include <core/image/Convert.h>
  8. #include <iostream>
  9. #include <assert.h>
  10. #include "CodebookPrototypes.h"
  11. using namespace OBJREC;
  12. using namespace std;
  13. using namespace NICE;
  14. CodebookPrototypes::CodebookPrototypes()
  15. {
  16. }
  17. CodebookPrototypes::CodebookPrototypes( const std::string & filename )
  18. {
  19. Codebook::read(filename);
  20. }
  21. CodebookPrototypes::CodebookPrototypes( const VVector & vv )
  22. {
  23. for ( const_iterator i = vv.begin(); i != vv.end(); i++ )
  24. push_back (*i);
  25. reinit ( vv.size() );
  26. }
  27. CodebookPrototypes::~CodebookPrototypes()
  28. {
  29. }
  30. void CodebookPrototypes::copy ( const Codebook *codebook )
  31. {
  32. Codebook::copy ( codebook );
  33. VVector::clear();
  34. const CodebookPrototypes *codebookp = dynamic_cast<const CodebookPrototypes *> ( codebook );
  35. assert ( codebookp != NULL );
  36. insert ( begin(), codebookp->begin(), codebookp->end() );
  37. }
  38. void CodebookPrototypes::vote ( const NICE::Vector & feature, int & codebookEntry, double & weight, double & distance ) const
  39. {
  40. const double *xp = feature.getDataPointer();
  41. size_t k = 0;
  42. double mindist = numeric_limits<double>::max();
  43. size_t my_cluster = 0;
  44. int len = feature.size();
  45. for ( vector<Vector>::const_iterator i = begin();
  46. i != end();
  47. i++,k++ )
  48. {
  49. const NICE::Vector & cluster = *i;
  50. //slow ICE variant
  51. //double distance = (x - cluster).Length();
  52. double distance = 0.0;
  53. const double *clusterp = cluster.getDataPointer();
  54. for ( int i = 0 ; i < len ; i++ )
  55. {
  56. double h = clusterp[i] - xp[i];
  57. distance += h*h;
  58. }
  59. if ( distance < mindist )
  60. {
  61. my_cluster = k;
  62. mindist = distance;
  63. }
  64. }
  65. codebookEntry = my_cluster;
  66. weight = 1.0;
  67. distance = mindist;
  68. }
  69. void CodebookPrototypes::add ( const Codebook *codebook )
  70. {
  71. informativeMeasure.append ( codebook->getInformativeMeasure() );
  72. thresholds.append ( codebook->getThresholds() );
  73. classnos.append ( codebook->getClassNos() );
  74. const CodebookPrototypes *codebookp = dynamic_cast< const CodebookPrototypes *> ( codebook );
  75. assert ( codebookp != NULL );
  76. insert ( begin(), codebookp->begin(), codebookp->end() );
  77. }
  78. Codebook *CodebookPrototypes::clone () const
  79. {
  80. return (new CodebookPrototypes());
  81. }
  82. void CodebookPrototypes::clear ()
  83. {
  84. VVector::clear();
  85. Codebook::clear();
  86. }
  87. void CodebookPrototypes::restore ( istream & is, int format )
  88. {
  89. Codebook::restore ( is, format );
  90. VVector::restore ( is, format );
  91. }
  92. void CodebookPrototypes::store ( ostream & os, int format ) const
  93. {
  94. Codebook::store ( os, format );
  95. VVector::store ( os, format );
  96. }
  97. void CodebookPrototypes::displayCodebook ( int xsize, int ysize ) const
  98. {
  99. NICE::Image bigimg ( xsize * 5 , ysize * size() );
  100. bigimg.set(0);
  101. NICE::Image img_s (xsize, ysize);
  102. for ( int k = 0 ; k < (int)size() ; k++ )
  103. {
  104. NICE::Vector vimg = *(begin() + k);
  105. NICE::Image img ((int)sqrt((double)vimg.size()), (int)sqrt((double)vimg.size()));
  106. int i = 0;
  107. double max = - numeric_limits<double>::max();
  108. double min = numeric_limits<double>::min();
  109. for ( int y = 0 ; y < img.height() ; y++ )
  110. for ( int x = 0 ; x < img.width() ; x++,i++ )
  111. {
  112. if ( max < vimg[i] ) max = vimg[i];
  113. if ( min > vimg[i] ) min = vimg[i];
  114. }
  115. i = 0;
  116. for ( int y = 0 ; y < img.height() ; y++ )
  117. for ( int x = 0 ; x < img.width() ; x++,i++ )
  118. {
  119. img.setPixel( x, y, (int)((vimg[i] - min)*255/(max-min)) );
  120. }
  121. NICE::scale ( img, &img_s );
  122. for ( int y = 0 ; y < ysize ; y++ )
  123. for ( int x = 0 ; x < xsize ; x++ )
  124. bigimg.setPixel(x, y+k*ysize, img_s.getPixel(x,y) );
  125. {
  126. std::ostringstream os;
  127. os << "no: " << k;
  128. if ( (int)informativeMeasure.size() > k )
  129. os << " " << informativeMeasure[k];
  130. // refactor-nice.pl: check this substitution
  131. // old: Text ( os.str().c_str(), xsize+1, k*ysize + 1, 255, 0, bigimg );
  132. // REFACTOR-FIXME Unable to map this statement
  133. }
  134. }
  135. bigimg.write ("/tmp/display.bmp");
  136. }