HOGFeature.cpp 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  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) );
  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::MultiChannelImageT<double> & img =
  33. example->ce->getDChannel ( CachedExample::D_INTEGRALEOH );
  34. int tm_xsize = img.xsize;
  35. int tm_ysize = img.ysize;
  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. wsx2 = window_size_x * tm_xsize / (2*xsize);
  44. wsy2 = window_size_y * tm_ysize / (2*ysize);
  45. } else {
  46. int exheight = example->height;
  47. wsx2 = exwidth * tm_xsize / (2*xsize);
  48. wsy2 = exheight * tm_ysize / (2*ysize);
  49. }
  50. int xx, yy;
  51. xx = ( example->x ) * tm_xsize / xsize;
  52. yy = ( example->y ) * tm_ysize / ysize;
  53. assert ( (wsx2 > 0) && (wsy2 > 0) );
  54. int xtl = xx - wsx2;
  55. int ytl = yy - wsy2;
  56. int xrb = xx + wsx2;
  57. int yrb = yy + wsy2;
  58. #define BOUND(x,min,max) (((x)<(min))?(min):((x)>(max)?(max):(x)))
  59. xtl = BOUND ( xtl, 0, tm_xsize - 1 );
  60. ytl = BOUND ( ytl, 0, tm_ysize - 1 );
  61. xrb = BOUND ( xrb, 0, tm_xsize - 1 );
  62. yrb = BOUND ( yrb, 0, tm_ysize - 1 );
  63. #undef BOUND
  64. double stepx = (xrb - xtl) / (double)( cellcountx );
  65. double stepy = (yrb - ytl) / (double)( cellcounty );
  66. int cxtl = (int)(xtl + stepx*cellx1);
  67. int cytl = (int)(ytl + stepy*celly1);
  68. int cxrb = (int)(xtl + stepx*cellx2);
  69. int cyrb = (int)(ytl + stepy*celly2);
  70. if ( cxrb <= cxtl ) cxrb = cxtl+1;
  71. if ( cyrb <= cytl ) cyrb = cytl+1;
  72. double A,B,C,D;
  73. assert ( bin < (int)img.numChannels );
  74. assert ( img.data[bin] != NULL );
  75. if ( (cxtl < 0) || (cxtl >= tm_xsize) )
  76. {
  77. fprintf (stderr, "cellcountx %d cellcounty %d\n", cellcountx, cellcounty );
  78. fprintf (stderr, "cxtl %d tm_xsize %d xsize %d\n", cxtl, tm_xsize, xsize );
  79. fprintf (stderr, "cellx1 %d stepx %f xtl %d xrb %d\n", cellx1, stepx, xtl, xrb );
  80. }
  81. if ( (cxrb < 0) || (cxrb >= tm_xsize) )
  82. {
  83. fprintf (stderr, "cellcountx %d cellcounty %d\n", cellcountx, cellcounty );
  84. fprintf (stderr, "cxrb %d tm_xsize %d xsize %d\n", cxrb, tm_xsize, xsize );
  85. fprintf (stderr, "cellx1 %d stepx %f xtl %d xrb %d\n", cellx1, stepx, xtl, xrb );
  86. }
  87. if ( (cytl < 0) || (cytl >= tm_ysize) )
  88. {
  89. fprintf (stderr, "cellcountx %d cellcounty %d\n", cellcountx, cellcounty );
  90. fprintf (stderr, "cytl %d tm_ysize %d ysize %d\n", cytl, tm_ysize, ysize );
  91. fprintf (stderr, "celly1 %d stepy %f ytl %d yrb %d\n", celly1, stepy, ytl, yrb );
  92. }
  93. if ( (cyrb < 0) || (cyrb >= tm_ysize) )
  94. {
  95. fprintf (stderr, "cellcountx %d cellcounty %d\n", cellcountx, cellcounty );
  96. fprintf (stderr, "cyrb %d tm_ysize %d ysize %d\n", cyrb, tm_ysize, ysize );
  97. fprintf (stderr, "celly1 %d stepy %f ytl %d yrb %d\n", celly1, stepy, ytl, yrb );
  98. }
  99. long kA = cxtl + cytl * tm_xsize;
  100. long kB = cxrb + cytl * tm_xsize;
  101. long kC = cxtl + cyrb * tm_xsize;
  102. long kD = cxrb + cyrb * tm_xsize;
  103. A = img.data[bin][ kA ];
  104. B = img.data[bin][ kB ];
  105. C = img.data[bin][ kC ];
  106. D = img.data[bin][ kD ];
  107. double val1 = (D - B - C + A);
  108. double sum = val1*val1;
  109. for ( int b = 0 ; b < (int)img.numChannels ; b++)
  110. {
  111. if ( b == bin ) continue;
  112. A = img.data[b][ kA ];
  113. B = img.data[b][ kB ];
  114. C = img.data[b][ kC ];
  115. D = img.data[b][ kD ];
  116. double val = ( D - B - C + A );
  117. sum += val*val;
  118. }
  119. // FIXME: maybe L_1 normalization is sufficient
  120. sum = sqrt(sum);
  121. return ( val1 + epsilon ) / ( sum + epsilon );
  122. }
  123. void HOGFeature::explode ( FeaturePool & featurePool, bool variableWindow ) const
  124. {
  125. int nScales = (variableWindow ? numScales : 1 );
  126. double weight = 1.0 / ( numBins * nScales );
  127. if ( flexibleGrid )
  128. weight *= 4.0 / ( cellcountx * (cellcountx - 1) * (cellcounty - 1) * cellcounty );
  129. else
  130. weight *= 1.0 / (cellcountx * cellcounty);
  131. for ( int i = 0 ; i < nScales ; i++ )
  132. {
  133. int wsy = window_size_y;
  134. int wsx = window_size_x;
  135. for ( int _cellx1 = 0 ; _cellx1 < cellcountx ; _cellx1++ )
  136. for ( int _celly1 = 0 ; _celly1 < cellcounty ; _celly1++ )
  137. for ( int _cellx2 = _cellx1+1 ;
  138. _cellx2 < (flexibleGrid ? cellcountx : _cellx1+2) ;
  139. _cellx2++ )
  140. for ( int _celly2 = _celly1+1 ;
  141. _celly2 < (flexibleGrid ? cellcounty :
  142. _celly1+2) ; _celly2++ )
  143. for ( int _bin = 0 ; _bin < numBins ; _bin++ )
  144. {
  145. HOGFeature *f = new HOGFeature();
  146. f->window_size_x = wsx;
  147. f->window_size_y = wsy;
  148. f->bin = _bin;
  149. f->cellx1 = _cellx1;
  150. f->celly1 = _celly1;
  151. f->cellx2 = _cellx2;
  152. f->celly2 = _celly2;
  153. f->cellcountx = cellcountx;
  154. f->cellcounty = cellcounty;
  155. featurePool.addFeature ( f, weight );
  156. }
  157. wsx = (int) (scaleStep * wsx);
  158. wsy = (int) (scaleStep * wsy);
  159. }
  160. }
  161. Feature *HOGFeature::clone() const
  162. {
  163. HOGFeature *f = new HOGFeature();
  164. f->window_size_x = window_size_x;
  165. f->window_size_y = window_size_y;
  166. f->bin = bin;
  167. f->cellx1 = cellx1;
  168. f->celly1 = celly1;
  169. f->cellx2 = cellx2;
  170. f->celly2 = celly2;
  171. f->cellcountx = cellcountx;
  172. f->cellcounty = cellcounty;
  173. f->flexibleGrid = flexibleGrid;
  174. return f;
  175. }
  176. Feature *HOGFeature::generateFirstParameter () const
  177. {
  178. return clone();
  179. }
  180. void HOGFeature::restore (istream & is, int format)
  181. {
  182. is >> window_size_x;
  183. is >> window_size_y;
  184. is >> bin;
  185. is >> cellx1;
  186. is >> celly1;
  187. is >> cellx2;
  188. is >> celly2;
  189. is >> cellcountx;
  190. is >> cellcounty;
  191. }
  192. void HOGFeature::store (ostream & os, int format) const
  193. {
  194. os << "HOGFEATURE "
  195. << window_size_x << " "
  196. << window_size_y << " "
  197. << bin << " "
  198. << cellx1 << " "
  199. << celly1 << " ";
  200. os << cellx2 << " "
  201. << celly2 << " ";
  202. os << cellcountx << " "
  203. << cellcounty;
  204. }
  205. void HOGFeature::clear ()
  206. {
  207. }