HistFeature.cpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. /**
  2. * @file HistFeature.cpp
  3. * @brief histogram integral feature
  4. * @author Erik Rodner
  5. * @date 05/07/2008
  6. */
  7. #include <iostream>
  8. #include "HistFeature.h"
  9. #include "vislearning/cbaselib/FeaturePool.h"
  10. using namespace OBJREC;
  11. using namespace std;
  12. using namespace NICE;
  13. const double epsilon = 10e-8;
  14. /** simple constructor */
  15. HistFeature::HistFeature( const Config *conf,
  16. const std::string & section,
  17. int _histtype,
  18. int _numBins )
  19. {
  20. window_size_x = conf->gI(section, "window_size_x", 21 );
  21. window_size_y = conf->gI(section, "window_size_y", 21 );
  22. scaleStep = conf->gD(section, "scale_step", sqrt(2) );
  23. numScales = conf->gI(section, "num_scales", 5 );
  24. flexibleGrid = conf->gB(section, "flexible_grid", false );
  25. cellcountx = conf->gI(section, "cellcountx", 10 );
  26. cellcounty = conf->gI(section, "cellcounty", 10 );
  27. histtype = _histtype;
  28. }
  29. /** simple destructor */
  30. HistFeature::~HistFeature()
  31. {
  32. }
  33. double HistFeature::val( const Example *example ) const
  34. {
  35. const NICE::MultiChannelImageT<double> & img = example->ce->getDChannel ( histtype );
  36. int tm_xsize = img.xsize;
  37. int tm_ysize = img.ysize;
  38. int xsize;
  39. int ysize;
  40. example->ce->getImageSize ( xsize, ysize );
  41. /** without overlap: normalized cell and bin **/
  42. int wsx2, wsy2;
  43. int exwidth = example->width;
  44. if ( exwidth == 0 ) {
  45. wsx2 = window_size_x * tm_xsize / (2*xsize);
  46. wsy2 = window_size_y * tm_ysize / (2*ysize);
  47. } else {
  48. int exheight = example->height;
  49. wsx2 = exwidth * tm_xsize / (2*xsize);
  50. wsy2 = exheight * tm_ysize / (2*ysize);
  51. }
  52. int xx, yy;
  53. xx = ( example->x ) * tm_xsize / xsize;
  54. yy = ( example->y ) * tm_ysize / ysize;
  55. assert ( (wsx2 > 0) && (wsy2 > 0) );
  56. int xtl = xx - wsx2;
  57. int ytl = yy - wsy2;
  58. int xrb = xx + wsx2;
  59. int yrb = yy + wsy2;
  60. #define BOUND(x,min,max) (((x)<(min))?(min):((x)>(max)?(max):(x)))
  61. xtl = BOUND ( xtl, 0, tm_xsize - 1 );
  62. ytl = BOUND ( ytl, 0, tm_ysize - 1 );
  63. xrb = BOUND ( xrb, 0, tm_xsize - 1 );
  64. yrb = BOUND ( yrb, 0, tm_ysize - 1 );
  65. #undef BOUND
  66. double stepx = (xrb - xtl) / (double)( cellcountx );
  67. double stepy = (yrb - ytl) / (double)( cellcounty );
  68. int cxtl = (int)(xtl + stepx*cellx1);
  69. int cytl = (int)(ytl + stepy*celly1);
  70. int cxrb = (int)(xtl + stepx*cellx2);
  71. int cyrb = (int)(ytl + stepy*celly2);
  72. if ( cxrb <= cxtl ) cxrb = cxtl+1;
  73. if ( cyrb <= cytl ) cyrb = cytl+1;
  74. double A,B,C,D;
  75. assert ( bin < (int)img.numChannels );
  76. assert ( img.data[bin] != NULL );
  77. long kA = cxtl + cytl * tm_xsize;
  78. long kB = cxrb + cytl * tm_xsize;
  79. long kC = cxtl + cyrb * tm_xsize;
  80. long kD = cxrb + cyrb * tm_xsize;
  81. A = img.data[bin][ kA ];
  82. B = img.data[bin][ kB ];
  83. C = img.data[bin][ kC ];
  84. D = img.data[bin][ kD ];
  85. double val1 = (D - B - C + A);
  86. double sum = val1*val1;
  87. for ( int b = 0 ; b < (int)img.numChannels ; b++)
  88. {
  89. if ( b == bin ) continue;
  90. A = img.data[b][ kA ];
  91. B = img.data[b][ kB ];
  92. C = img.data[b][ kC ];
  93. D = img.data[b][ kD ];
  94. double val = ( D - B - C + A );
  95. if ( normalizationMethod == HISTFEATURE_NORMMETHOD_L2 )
  96. sum += val*val;
  97. else if ( normalizationMethod == HISTFEATURE_NORMMETHOD_L1 )
  98. sum += val;
  99. }
  100. if ( normalizationMethod == HISTFEATURE_NORMMETHOD_L2 )
  101. sum = sqrt(sum);
  102. return ( val1 + epsilon ) / ( sum + epsilon );
  103. }
  104. void HistFeature::explode ( FeaturePool & featurePool, bool variableWindow ) const
  105. {
  106. int nScales = (variableWindow ? numScales : 1 );
  107. double weight = 1.0 / ( numBins * nScales );
  108. if ( flexibleGrid )
  109. weight *= 4.0 / ( cellcountx * (cellcountx - 1) * (cellcounty - 1) * cellcounty );
  110. else
  111. weight *= 1.0 / (cellcountx * cellcounty);
  112. for ( int i = 0 ; i < nScales ; i++ )
  113. {
  114. int wsy = window_size_y;
  115. int wsx = window_size_x;
  116. for ( int _cellx1 = 0 ; _cellx1 < cellcountx ; _cellx1++ )
  117. for ( int _celly1 = 0 ; _celly1 < cellcounty ; _celly1++ )
  118. for ( int _cellx2 = _cellx1+1 ;
  119. _cellx2 < (flexibleGrid ? cellcountx : _cellx1+2) ;
  120. _cellx2++ )
  121. for ( int _celly2 = _celly1+1 ;
  122. _celly2 < (flexibleGrid ? cellcounty :
  123. _celly1+2) ; _celly2++ )
  124. for ( int _bin = 0 ; _bin < numBins ; _bin++ )
  125. {
  126. HistFeature *f = new HistFeature();
  127. f->histtype = histtype;
  128. f->window_size_x = wsx;
  129. f->window_size_y = wsy;
  130. f->bin = _bin;
  131. f->cellx1 = _cellx1;
  132. f->celly1 = _celly1;
  133. f->cellx2 = _cellx2;
  134. f->celly2 = _celly2;
  135. f->cellcountx = cellcountx;
  136. f->cellcounty = cellcounty;
  137. featurePool.addFeature ( f, weight );
  138. }
  139. wsx = (int) (scaleStep * wsx);
  140. wsy = (int) (scaleStep * wsy);
  141. }
  142. }
  143. Feature *HistFeature::clone() const
  144. {
  145. HistFeature *f = new HistFeature();
  146. f->histtype = histtype;
  147. f->window_size_x = window_size_x;
  148. f->window_size_y = window_size_y;
  149. f->bin = bin;
  150. f->cellx1 = cellx1;
  151. f->celly1 = celly1;
  152. f->cellx2 = cellx2;
  153. f->celly2 = celly2;
  154. f->cellcountx = cellcountx;
  155. f->cellcounty = cellcounty;
  156. return f;
  157. }
  158. Feature *HistFeature::generateFirstParameter () const
  159. {
  160. return clone();
  161. }
  162. void HistFeature::restore (istream & is, int format)
  163. {
  164. is >> histtype;
  165. is >> window_size_x;
  166. is >> window_size_y;
  167. is >> bin;
  168. is >> cellx1;
  169. is >> celly1;
  170. is >> cellx2;
  171. is >> celly2;
  172. is >> cellcountx;
  173. is >> cellcounty;
  174. }
  175. void HistFeature::store (ostream & os, int format) const
  176. {
  177. os << "HistFeature "
  178. << histtype << " "
  179. << window_size_x << " "
  180. << window_size_y << " "
  181. << bin << " "
  182. << cellx1 << " "
  183. << celly1 << " "
  184. << cellx2 << " "
  185. << celly2 << " "
  186. << cellcountx << " "
  187. << cellcounty;
  188. }
  189. void HistFeature::clear ()
  190. {
  191. }