CodebookPrototypes.cpp 4.1 KB

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