HOGFeature.cpp 6.9 KB

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