HOGFeature.cpp 6.7 KB

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