HaarFeature.cpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. /**
  2. * @file HaarFeature.cpp
  3. * @brief simple haar like feature
  4. * @author Erik Rodner
  5. * @date 04/21/2008
  6. */
  7. #include <iostream>
  8. #include "vislearning/features/fpfeatures/HaarFeature.h"
  9. #include "vislearning/cbaselib/FeaturePool.h"
  10. using namespace OBJREC;
  11. using namespace std;
  12. // refactor-nice.pl: check this substitution
  13. // old: using namespace ice;
  14. using namespace NICE;
  15. void HaarFeature::explode ( FeaturePool & featurePool, bool variableWindow ) const
  16. {
  17. HaarFeature *hf = new HaarFeature ( *this );
  18. hf->pos1 = 0;
  19. hf->pos2 = 0;
  20. hf->type = HAARTYPE_HORIZONTAL;
  21. for ( hf->pos1 = 0 ; hf->pos1 < hf->window_size_y - 1 ; hf->pos1 += hf->step_y )
  22. featurePool.addFeature ( hf->clone(), hf->step_y / (double)( HAARTYPE_NUMTYPES * hf->window_size_y ) );
  23. hf->pos1 = 0;
  24. hf->pos2 = 0;
  25. hf->type = HAARTYPE_VERTICAL;
  26. for ( hf->pos1 = 0 ; hf->pos1 < hf->window_size_x - 1; hf->pos1 += hf->step_x )
  27. featurePool.addFeature ( hf->clone(), hf->step_x / (double)( HAARTYPE_NUMTYPES * hf->window_size_x ) );
  28. hf->type = HAARTYPE_DIAGONAL;
  29. hf->pos1 = 0;
  30. hf->pos2 = 0;
  31. for ( hf->pos1 = 0 ; hf->pos1 < hf->window_size_x - 1; hf->pos1 += hf->step_x )
  32. for ( hf->pos2 = 0 ; hf->pos2 < hf->window_size_y - 1 ; hf->pos2 += hf->step_y )
  33. featurePool.addFeature ( hf->clone(),
  34. hf->step_x * hf->step_y / (double)( HAARTYPE_NUMTYPES * hf->window_size_x * hf->window_size_y ) );
  35. hf->pos1 = 0;
  36. hf->pos2 = 0;
  37. hf->type = HAARTYPE_3BLOCKS;
  38. for ( hf->pos1 = 0 ; hf->pos1 < hf->window_size_x - 2*hf->step_x ; hf->pos1 += hf->step_x )
  39. for ( hf->pos2 = hf->pos1 + hf->step_x; hf->pos2 < hf->window_size_x - hf->step_x ; hf->pos2 += hf->step_x )
  40. featurePool.addFeature ( hf->clone(),
  41. (2.0 * hf->step_x) / ( HAARTYPE_NUMTYPES * (hf->window_size_x - hf->step_x) ) );
  42. delete hf;
  43. }
  44. Feature *HaarFeature::clone() const
  45. {
  46. HaarFeature *fp = new HaarFeature(*this);
  47. return fp;
  48. }
  49. /************* HaarFeature **************/
  50. HaarFeature::HaarFeature( const Config *conf )
  51. {
  52. window_size_x = conf->gI("HaarFeature", "window_size_x", 24 );
  53. window_size_y = conf->gI("HaarFeature", "window_size_y", 24 );
  54. step_x = conf->gI("HaarFeature", "step_x", 1 );
  55. step_y = conf->gI("HaarFeature", "step_y", 1 );
  56. }
  57. HaarFeature::HaarFeature ( int _window_size_x,
  58. int _window_size_y,
  59. int _step_x,
  60. int _step_y )
  61. {
  62. window_size_x = _window_size_x;
  63. window_size_y = _window_size_y;
  64. pos1 = 1;
  65. pos2 = 0;
  66. step_x = _step_x;
  67. step_y = _step_y;
  68. type = HaarFeature::HAARTYPE_HORIZONTAL;
  69. }
  70. HaarFeature::~HaarFeature()
  71. {
  72. }
  73. double HaarFeature::val( const Example *example ) const
  74. {
  75. const NICE::MultiChannelImageT<long> & img = example->ce->getLChannel ( CachedExample::L_INTEGRALIMAGE );
  76. const long *integralImage = img.data[0];
  77. int xsize = img.xsize;
  78. int ysize = img.ysize;
  79. int x = example->x;
  80. int y = example->y;
  81. long tl, tr, bl, br;
  82. int exwidth = example->width;
  83. if ( exwidth != 0 )
  84. {
  85. int exheight = example->height;
  86. tl = (x - exwidth/2) + (y - exheight/2)*xsize;
  87. tr = tl + exwidth - 1;
  88. bl = tl + (exheight - 1)*xsize;
  89. br = bl + exwidth - 1;
  90. } else {
  91. tl = (x - window_size_x/2) + (y - window_size_y/2)*xsize;
  92. tr = tl + window_size_x - 1;
  93. bl = tl + (window_size_y - 1)*xsize;
  94. br = bl + (window_size_x - 1);
  95. }
  96. assert ( tl < xsize*ysize );
  97. assert ( tr < xsize*ysize );
  98. if ( br >= xsize*ysize )
  99. {
  100. fprintf (stderr, "xsize=%d, ysize=%d, x=%d, y=%d, wsy=%d, wsx=%d\n",
  101. xsize, ysize, x, y, window_size_x, window_size_y );
  102. fprintf (stderr, "example: %d x %d\n",
  103. example->width, example->height );
  104. }
  105. assert ( bl < xsize*ysize );
  106. assert ( br < xsize*ysize );
  107. assert ( pos1 >= 0 );
  108. assert ( pos2 >= 0 );
  109. double value;
  110. if ( type == HaarFeature::HAARTYPE_HORIZONTAL )
  111. {
  112. long ml = tl + xsize*pos1;
  113. long mr = tr + xsize*pos1;
  114. assert ( (ml >= 0) && (ml < xsize*ysize) );
  115. assert ( (mr >= 0) && (mr < xsize*ysize) );
  116. value = 2*integralImage[mr];
  117. value -= 2*integralImage[ml];
  118. value += integralImage[tl];
  119. value += integralImage[tr];
  120. value -= integralImage[bl];
  121. value -= integralImage[br];
  122. } else if ( type == HaarFeature::HAARTYPE_VERTICAL ) {
  123. long mt = tl + pos1;
  124. long mb = bl + pos1;
  125. assert ( (mt >= 0) && (mt < xsize*ysize) );
  126. assert ( (mb >= 0) && (mb < xsize*ysize) );
  127. value = 2*integralImage[mb];
  128. value -= 2*integralImage[mt];
  129. value += integralImage[tl];
  130. value += integralImage[tr];
  131. value -= integralImage[bl];
  132. value -= integralImage[br];
  133. } else if ( type == HaarFeature::HAARTYPE_DIAGONAL ) {
  134. int p2o = pos2*xsize;
  135. assert ( (p2o >= 0) && (p2o < xsize*ysize) );
  136. assert ( (tl+pos1 >= 0) && (tl+pos1 < xsize*ysize) );
  137. assert ( (tl+p2o >= 0) && (tl+p2o < xsize*ysize) );
  138. assert ( (bl+pos1 >= 0) && (bl+pos1 < xsize*ysize) );
  139. assert ( (tr+p2o >= 0) && (tr+p2o < xsize*ysize) );
  140. assert ( (tl+pos1+p2o >= 0) && (tl+pos1+p2o < xsize*ysize) );
  141. value = integralImage[tl];
  142. value += integralImage[bl];
  143. value += integralImage[br];
  144. value += integralImage[tr];
  145. value -= 2 * (
  146. integralImage[tl+pos1]
  147. + integralImage[tl+p2o]
  148. + integralImage[bl+pos1]
  149. + integralImage[tr+p2o]
  150. );
  151. value += 4 * integralImage[tl + pos1 + p2o];
  152. } else if ( type == HaarFeature::HAARTYPE_3BLOCKS ) {
  153. assert ( (tl+pos1 >= 0) && (tl+pos1 < xsize*ysize) );
  154. assert ( (bl+pos2 >= 0) && (bl+pos2 < xsize*ysize) );
  155. assert ( (tl+pos2 >= 0) && (tl+pos2 < xsize*ysize) );
  156. assert ( (bl+pos1 >= 0) && (bl+pos1 < xsize*ysize) );
  157. value = integralImage[tl];
  158. value += integralImage[br];
  159. value -= integralImage[bl];
  160. value -= integralImage[tr];
  161. value += 2 * (
  162. - integralImage[tl+pos1]
  163. - integralImage[bl+pos2]
  164. + integralImage[tl+pos2]
  165. + integralImage[bl+pos1]
  166. );
  167. } else {
  168. return -1;
  169. }
  170. assert ( finite(value) );
  171. return value;
  172. }
  173. void HaarFeature::restore (istream & is, int format)
  174. {
  175. is >> type;
  176. is >> window_size_x;
  177. is >> window_size_y;
  178. is >> pos1;
  179. is >> pos2;
  180. }
  181. void HaarFeature::store (ostream & os, int format) const
  182. {
  183. os << "HAARFEATURE" << " " << type << " "
  184. << window_size_x << " " << window_size_y
  185. << " " << pos1 << " " << pos2;
  186. }
  187. void HaarFeature::clear ()
  188. {
  189. // nothing to do in my opinion
  190. }