EOHFeature.cpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. /**
  2. * @file EOHFeature.cpp
  3. * @brief edge orientation histograms (Levi and Weiss 2004)
  4. * @author Erik Rodner
  5. * @date 05/07/2008
  6. */
  7. #include <iostream>
  8. #include "EOHFeature.h"
  9. #include "vislearning/cbaselib/FeaturePool.h"
  10. using namespace OBJREC;
  11. const double epsilon = 10e-8;
  12. using namespace std;
  13. using namespace NICE;
  14. /** simple constructor */
  15. EOHFeature::EOHFeature ( const Config *conf )
  16. {
  17. window_size_x = conf->gI ( "EOHFeature", "window_size_x", 21 );
  18. window_size_y = conf->gI ( "EOHFeature", "window_size_y", 21 );
  19. scaleStep = conf->gD ( "EOHFeature", "scale_step", sqrt ( 2.0f ) );
  20. numScales = conf->gI ( "EOHFeature", "num_scales", 5 );
  21. numBins = conf->gI ( "EOHFeature", "num_bins", 9 );
  22. bin = 0;
  23. bin2 = 0;
  24. type = EOH_VALUE;
  25. }
  26. /** simple destructor */
  27. EOHFeature::~EOHFeature()
  28. {
  29. }
  30. double EOHFeature::val ( const Example *example ) const
  31. {
  32. const NICE::MultiChannelImage3DT<double> & img = example->ce->getDChannel (
  33. CachedExample::D_INTEGRALEOH );
  34. int xsize;
  35. int ysize;
  36. example->ce->getImageSize ( xsize, ysize );
  37. int tm_xsize = img.width();
  38. int tm_ysize = img.height();
  39. int wsx2, wsy2;
  40. int xx, yy;
  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. xx = ( example->x ) * tm_xsize / xsize;
  51. yy = ( example->y ) * tm_ysize / ysize;
  52. int xtl = xx - wsx2;
  53. int ytl = yy - wsy2;
  54. int xrb = xx + wsx2;
  55. int yrb = yy + wsy2;
  56. #define BOUND(x,min,max) (((x)<(min))?(min):((x)>(max)?(max):(x)))
  57. xtl = BOUND ( xtl, 0, tm_xsize - 1 );
  58. ytl = BOUND ( ytl, 0, tm_ysize - 1 );
  59. xrb = BOUND ( xrb, 0, tm_xsize - 1 );
  60. yrb = BOUND ( yrb, 0, tm_ysize - 1 );
  61. #undef BOUND
  62. double A, B, C, D;
  63. assert ( bin < ( int ) img.channels() );
  64. A = img.get ( xtl, ytl, bin );
  65. B = img.get ( xrb, ytl, bin );
  66. C = img.get ( xtl, yrb, bin );
  67. D = img.get ( xrb, yrb, bin );
  68. if ( type == EOH_VALUE )
  69. {
  70. int area = ( xrb - xtl ) * ( yrb - ytl );
  71. if ( area == 0 )
  72. return 0.0;
  73. else {
  74. /* A B
  75. C D */
  76. double value = ( D - B - C + A ) / area;
  77. return value;
  78. }
  79. }
  80. else if ( type == EOH_RATIO )
  81. {
  82. assert ( bin2 < ( int ) img.channels() );
  83. double val1 = ( D - B - C + A );
  84. A = img.get ( xtl, ytl, bin2 );
  85. B = img.get ( xrb, ytl, bin2 );
  86. C = img.get ( xtl, yrb, bin2 );
  87. D = img.get ( xrb, yrb, bin2 );
  88. double val2 = ( D - B - C + A );
  89. return ( val1 + epsilon ) / ( val2 + epsilon );
  90. }
  91. else if ( type == EOH_DOMINANT_ORIENTATION )
  92. {
  93. double val1 = ( D - B - C + A );
  94. double sum = val1;
  95. for ( int b = 0 ; b < ( int ) img.channels() ; b++ )
  96. {
  97. if ( b == bin )
  98. continue;
  99. A = img.get ( xtl, ytl, b );
  100. B = img.get ( xrb, ytl, b );
  101. C = img.get ( xtl, yrb, b );
  102. D = img.get ( xrb, yrb, b );
  103. sum += ( D - B - C + A );
  104. }
  105. return ( val1 + epsilon ) / ( sum + epsilon );
  106. }
  107. assert ( 1 == 0 );
  108. }
  109. void EOHFeature::explode ( FeaturePool & featurePool, bool variableWindow ) const
  110. {
  111. int nScales = ( variableWindow ? numScales : 1 );
  112. for ( int i = 0 ; i < nScales ; i++ )
  113. {
  114. int wsy = window_size_y;
  115. int wsx = window_size_x;
  116. for ( int _type = 0 ; _type < EOH_NUMTYPES; _type++ )
  117. {
  118. if ( ( _type == EOH_VALUE ) || ( _type == EOH_DOMINANT_ORIENTATION ) )
  119. {
  120. for ( int _bin = 0 ; _bin < numBins ; _bin++ )
  121. {
  122. EOHFeature *f = new EOHFeature();
  123. f->window_size_x = wsx;
  124. f->window_size_y = wsy;
  125. f->bin = _bin;
  126. f->type = _type;
  127. featurePool.addFeature ( f, 1.0 / ( EOH_NUMTYPES * numBins * nScales ) );
  128. }
  129. }
  130. if ( ( _type == EOH_RATIO ) )
  131. {
  132. for ( int _bin = 0 ; _bin < numBins ; _bin++ )
  133. {
  134. for ( int _bin2 = 0 ; _bin2 < numBins ; _bin2++ )
  135. {
  136. if ( bin == bin2 ) continue;
  137. EOHFeature *f = new EOHFeature();
  138. f->window_size_x = wsx;
  139. f->window_size_y = wsy;
  140. f->bin = _bin;
  141. f->bin2 = _bin2;
  142. f->type = _type;
  143. featurePool.addFeature ( f, 1.0 / ( EOH_NUMTYPES * ( numBins - 1 ) * numBins * nScales ) );
  144. }
  145. }
  146. }
  147. }
  148. wsx = ( int ) ( scaleStep * wsx );
  149. wsy = ( int ) ( scaleStep * wsy );
  150. }
  151. }
  152. Feature *EOHFeature::clone() const
  153. {
  154. EOHFeature *f = new EOHFeature();
  155. f->window_size_x = window_size_x;
  156. f->window_size_y = window_size_y;
  157. f->bin = bin;
  158. f->bin2 = bin2;
  159. f->type = type;
  160. return f;
  161. }
  162. Feature *EOHFeature::generateFirstParameter () const
  163. {
  164. return clone();
  165. }
  166. void EOHFeature::restore ( istream & is, int format )
  167. {
  168. is >> window_size_x;
  169. is >> window_size_y;
  170. is >> type;
  171. is >> bin;
  172. is >> bin2;
  173. }
  174. void EOHFeature::store ( ostream & os, int format ) const
  175. {
  176. os << "EOHFEATURE "
  177. << window_size_x << " "
  178. << window_size_y << " "
  179. << type << " "
  180. << bin << " "
  181. << bin2;
  182. }
  183. void EOHFeature::clear ()
  184. {
  185. }