HistFeature.cpp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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.0f ) );
  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::MultiChannelImage3DT<double> & img = example->ce->getDChannel ( histtype );
  36. int tm_xsize = img.width();
  37. int tm_ysize = img.height();
  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.channels() );
  76. A = img.get ( cxtl, cytl, bin );
  77. B = img.get ( cxrb, cytl, bin );
  78. C = img.get ( cxtl, cyrb, bin );
  79. D = img.get ( cxrb, cyrb, bin );
  80. double val1 = ( D - B - C + A );
  81. double sum = val1 * val1;
  82. for ( int b = 0 ; b < ( int ) img.channels() ; b++ )
  83. {
  84. if ( b == bin ) continue;
  85. A = img.get ( cxtl, cytl, b );
  86. B = img.get ( cxrb, cytl, b );
  87. C = img.get ( cxtl, cyrb, b );
  88. D = img.get ( cxrb, cyrb, b );
  89. double val = ( D - B - C + A );
  90. if ( normalizationMethod == HISTFEATURE_NORMMETHOD_L2 )
  91. sum += val * val;
  92. else if ( normalizationMethod == HISTFEATURE_NORMMETHOD_L1 )
  93. sum += val;
  94. }
  95. if ( normalizationMethod == HISTFEATURE_NORMMETHOD_L2 )
  96. sum = sqrt ( sum );
  97. return ( val1 + epsilon ) / ( sum + epsilon );
  98. }
  99. void HistFeature::explode ( FeaturePool & featurePool, bool variableWindow ) const
  100. {
  101. int nScales = ( variableWindow ? numScales : 1 );
  102. double weight = 1.0 / ( numBins * nScales );
  103. if ( flexibleGrid )
  104. weight *= 4.0 / ( cellcountx * ( cellcountx - 1 ) * ( cellcounty - 1 ) * cellcounty );
  105. else
  106. weight *= 1.0 / ( cellcountx * cellcounty );
  107. for ( int i = 0 ; i < nScales ; i++ )
  108. {
  109. int wsy = window_size_y;
  110. int wsx = window_size_x;
  111. for ( int _cellx1 = 0 ; _cellx1 < cellcountx ; _cellx1++ )
  112. for ( int _celly1 = 0 ; _celly1 < cellcounty ; _celly1++ )
  113. for ( int _cellx2 = _cellx1 + 1 ;
  114. _cellx2 < ( flexibleGrid ? cellcountx : _cellx1 + 2 ) ;
  115. _cellx2++ )
  116. for ( int _celly2 = _celly1 + 1 ;
  117. _celly2 < ( flexibleGrid ? cellcounty :
  118. _celly1 + 2 ) ; _celly2++ )
  119. for ( int _bin = 0 ; _bin < numBins ; _bin++ )
  120. {
  121. HistFeature *f = new HistFeature();
  122. f->histtype = histtype;
  123. f->window_size_x = wsx;
  124. f->window_size_y = wsy;
  125. f->bin = _bin;
  126. f->cellx1 = _cellx1;
  127. f->celly1 = _celly1;
  128. f->cellx2 = _cellx2;
  129. f->celly2 = _celly2;
  130. f->cellcountx = cellcountx;
  131. f->cellcounty = cellcounty;
  132. featurePool.addFeature ( f, weight );
  133. }
  134. wsx = ( int ) ( scaleStep * wsx );
  135. wsy = ( int ) ( scaleStep * wsy );
  136. }
  137. }
  138. Feature *HistFeature::clone() const
  139. {
  140. HistFeature *f = new HistFeature();
  141. f->histtype = histtype;
  142. f->window_size_x = window_size_x;
  143. f->window_size_y = window_size_y;
  144. f->bin = bin;
  145. f->cellx1 = cellx1;
  146. f->celly1 = celly1;
  147. f->cellx2 = cellx2;
  148. f->celly2 = celly2;
  149. f->cellcountx = cellcountx;
  150. f->cellcounty = cellcounty;
  151. return f;
  152. }
  153. Feature *HistFeature::generateFirstParameter () const
  154. {
  155. return clone();
  156. }
  157. void HistFeature::restore ( istream & is, int format )
  158. {
  159. is >> histtype;
  160. is >> window_size_x;
  161. is >> window_size_y;
  162. is >> bin;
  163. is >> cellx1;
  164. is >> celly1;
  165. is >> cellx2;
  166. is >> celly2;
  167. is >> cellcountx;
  168. is >> cellcounty;
  169. }
  170. void HistFeature::store ( ostream & os, int format ) const
  171. {
  172. os << "HistFeature "
  173. << histtype << " "
  174. << window_size_x << " "
  175. << window_size_y << " "
  176. << bin << " "
  177. << cellx1 << " "
  178. << celly1 << " "
  179. << cellx2 << " "
  180. << celly2 << " "
  181. << cellcountx << " "
  182. << cellcounty;
  183. }
  184. void HistFeature::clear ()
  185. {
  186. }