HaarFeature.cpp 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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. using namespace NICE;
  13. void HaarFeature::explode ( FeaturePool & featurePool, bool variableWindow ) const
  14. {
  15. HaarFeature *hf = new HaarFeature ( *this );
  16. hf->pos1 = 0;
  17. hf->pos2 = 0;
  18. hf->type = HAARTYPE_HORIZONTAL;
  19. for ( hf->pos1 = 0 ; hf->pos1 < hf->window_size_y - 1 ; hf->pos1 += hf->step_y )
  20. featurePool.addFeature ( hf->clone(), hf->step_y / ( double ) ( HAARTYPE_NUMTYPES * hf->window_size_y ) );
  21. hf->pos1 = 0;
  22. hf->pos2 = 0;
  23. hf->type = HAARTYPE_VERTICAL;
  24. for ( hf->pos1 = 0 ; hf->pos1 < hf->window_size_x - 1; hf->pos1 += hf->step_x )
  25. featurePool.addFeature ( hf->clone(), hf->step_x / ( double ) ( HAARTYPE_NUMTYPES * hf->window_size_x ) );
  26. hf->type = HAARTYPE_DIAGONAL;
  27. hf->pos1 = 0;
  28. hf->pos2 = 0;
  29. for ( hf->pos1 = 0 ; hf->pos1 < hf->window_size_x - 1; hf->pos1 += hf->step_x )
  30. for ( hf->pos2 = 0 ; hf->pos2 < hf->window_size_y - 1 ; hf->pos2 += hf->step_y )
  31. featurePool.addFeature ( hf->clone(),
  32. hf->step_x * hf->step_y / ( double ) ( HAARTYPE_NUMTYPES * hf->window_size_x * hf->window_size_y ) );
  33. hf->pos1 = 0;
  34. hf->pos2 = 0;
  35. hf->type = HAARTYPE_3BLOCKS;
  36. for ( hf->pos1 = 0 ; hf->pos1 < hf->window_size_x - 2*hf->step_x ; hf->pos1 += hf->step_x )
  37. for ( hf->pos2 = hf->pos1 + hf->step_x; hf->pos2 < hf->window_size_x - hf->step_x ; hf->pos2 += hf->step_x )
  38. featurePool.addFeature ( hf->clone(),
  39. ( 2.0 * hf->step_x ) / ( HAARTYPE_NUMTYPES * ( hf->window_size_x - hf->step_x ) ) );
  40. delete hf;
  41. }
  42. Feature *HaarFeature::clone() const
  43. {
  44. HaarFeature *fp = new HaarFeature ( *this );
  45. return fp;
  46. }
  47. /************* HaarFeature **************/
  48. HaarFeature::HaarFeature ( const Config *conf )
  49. {
  50. window_size_x = conf->gI ( "HaarFeature", "window_size_x", 24 );
  51. window_size_y = conf->gI ( "HaarFeature", "window_size_y", 24 );
  52. step_x = conf->gI ( "HaarFeature", "step_x", 1 );
  53. step_y = conf->gI ( "HaarFeature", "step_y", 1 );
  54. }
  55. HaarFeature::HaarFeature ( int _window_size_x,
  56. int _window_size_y,
  57. int _step_x,
  58. int _step_y )
  59. {
  60. window_size_x = _window_size_x;
  61. window_size_y = _window_size_y;
  62. pos1 = 1;
  63. pos2 = 0;
  64. step_x = _step_x;
  65. step_y = _step_y;
  66. type = HaarFeature::HAARTYPE_HORIZONTAL;
  67. }
  68. HaarFeature::~HaarFeature()
  69. {
  70. }
  71. double HaarFeature::val ( const Example *example ) const
  72. {
  73. NICE::MultiChannelImage3DT<long> & img = example->ce->getLChannel ( CachedExample::L_INTEGRALIMAGE );
  74. std::vector<long*> data = img.getDataPointer();
  75. const long *integralImage = data[0];
  76. int xsize = img.width();
  77. int ysize = img.height();
  78. int x = example->x;
  79. int y = example->y;
  80. long tl, tr, bl, br;
  81. int exwidth = example->width;
  82. if ( exwidth != 0 )
  83. {
  84. int exheight = example->height;
  85. tl = ( x - exwidth / 2 ) + ( y - exheight / 2 ) * xsize;
  86. tr = tl + exwidth - 1;
  87. bl = tl + ( exheight - 1 ) * xsize;
  88. br = bl + exwidth - 1;
  89. } else {
  90. tl = ( x - window_size_x / 2 ) + ( y - window_size_y / 2 ) * xsize;
  91. tr = tl + window_size_x - 1;
  92. bl = tl + ( window_size_y - 1 ) * xsize;
  93. br = bl + ( window_size_x - 1 );
  94. }
  95. assert ( tl < xsize*ysize );
  96. assert ( tr < xsize*ysize );
  97. if ( br >= xsize*ysize )
  98. {
  99. fprintf ( stderr, "xsize=%d, ysize=%d, x=%d, y=%d, wsy=%d, wsx=%d\n",
  100. xsize, ysize, x, y, window_size_x, window_size_y );
  101. fprintf ( stderr, "example: %d x %d\n",
  102. example->width, example->height );
  103. }
  104. assert ( bl < xsize*ysize );
  105. assert ( br < xsize*ysize );
  106. assert ( pos1 >= 0 );
  107. assert ( pos2 >= 0 );
  108. //#if 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 ( NICE::isFinite( value ) );
  171. //#else
  172. // throw("not yet adapted for new MultiChannelImageT!");
  173. // double value = 0.0;
  174. //#endif
  175. return value;
  176. }
  177. void HaarFeature::restore ( istream & is, int format )
  178. {
  179. is >> type;
  180. is >> window_size_x;
  181. is >> window_size_y;
  182. is >> pos1;
  183. is >> pos2;
  184. }
  185. void HaarFeature::store ( ostream & os, int format ) const
  186. {
  187. os << "HAARFEATURE" << " " << type << " "
  188. << window_size_x << " " << window_size_y
  189. << " " << pos1 << " " << pos2;
  190. }
  191. void HaarFeature::clear()
  192. {
  193. // nothing to do in my opinion
  194. }