ColorHistogramFeature.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. /**
  2. * @file ColorHistogramFeature.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 "ColorHistogramFeature.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. ColorHistogramFeature::ColorHistogramFeature ( const Config *conf )
  16. {
  17. window_size_x = conf->gI ( "ColorHistogramFeature", "window_size_x", 21 );
  18. window_size_y = conf->gI ( "ColorHistogramFeature", "window_size_y", 21 );
  19. scaleStep = conf->gD ( "ColorHistogramFeature", "scale_step", sqrt ( 2.0f ) );
  20. numScales = conf->gI ( "ColorHistogramFeature", "num_scales", 5 );
  21. int numBinsH = conf->gI ( "ColorHistogramFeature", "num_bins_h", 4 );
  22. int numBinsS = conf->gI ( "ColorHistogramFeature", "num_bins_s", 2 );
  23. int numBinsV = conf->gI ( "ColorHistogramFeature", "num_bins_v", 2 );
  24. numBins = numBinsH * numBinsS * numBinsV;
  25. }
  26. /** simple destructor */
  27. ColorHistogramFeature::~ColorHistogramFeature()
  28. {
  29. }
  30. double ColorHistogramFeature::val ( const Example *example ) const
  31. {
  32. const NICE::MultiChannelImage3DT<double> & img =
  33. example->ce->getDChannel ( CachedExample::D_INTEGRALCOLOR );
  34. int tm_xsize = img.width();
  35. int tm_ysize = img.height();
  36. int xsize;
  37. int ysize;
  38. example->ce->getImageSize ( xsize, ysize );
  39. int wsx2, wsy2;
  40. int exwidth = example->width;
  41. if ( exwidth == 0 ) {
  42. wsx2 = window_size_x * tm_xsize / ( 2 * xsize );
  43. wsy2 = window_size_y * tm_ysize / ( 2 * ysize );
  44. } else {
  45. int exheight = example->height;
  46. wsx2 = exwidth * tm_xsize / ( 2 * xsize );
  47. wsy2 = exheight * tm_ysize / ( 2 * ysize );
  48. }
  49. int xx, yy;
  50. xx = ( example->x ) * tm_xsize / xsize;
  51. yy = ( example->y ) * tm_ysize / ysize;
  52. assert ( ( wsx2 > 0 ) && ( wsy2 > 0 ) );
  53. int xtl = xx - wsx2;
  54. int ytl = yy - wsy2;
  55. int xrb = xx + wsx2;
  56. int yrb = yy + wsy2;
  57. #define BOUND(x,min,max) (((x)<(min))?(min):((x)>(max)?(max):(x)))
  58. xtl = BOUND ( xtl, 0, tm_xsize - 1 );
  59. ytl = BOUND ( ytl, 0, tm_ysize - 1 );
  60. xrb = BOUND ( xrb, 0, tm_xsize - 1 );
  61. yrb = BOUND ( yrb, 0, tm_ysize - 1 );
  62. #undef BOUND
  63. assert ( bin < ( int ) img.channels() );
  64. double A, B, C, D;
  65. A = img.get ( xtl, ytl, bin );
  66. B = img.get ( xrb, ytl, bin );
  67. C = img.get ( xtl, yrb, bin );
  68. D = img.get ( xrb, yrb, bin );
  69. double val1 = ( D - B - C + A );
  70. double sum = val1 * val1;
  71. for ( int b = 0 ; b < ( int ) img.channels() ; b++ )
  72. {
  73. if ( b == bin )
  74. continue;
  75. A = img.get ( xtl, ytl, b );
  76. B = img.get ( xrb, ytl, b );
  77. C = img.get ( xtl, yrb, b );
  78. D = img.get ( xrb, yrb, b );
  79. double val = ( D - B - C + A );
  80. sum += val * val;
  81. }
  82. sum = sqrt ( sum );
  83. return ( val1 + epsilon ) / ( sum + epsilon );
  84. }
  85. void ColorHistogramFeature::explode ( FeaturePool & featurePool, bool variableWindow ) const
  86. {
  87. int nScales = ( variableWindow ? numScales : 1 );
  88. for ( int i = 0 ; i < nScales ; i++ )
  89. {
  90. int wsy = window_size_y;
  91. int wsx = window_size_x;
  92. for ( int _bin = 0 ; _bin < numBins ; _bin++ )
  93. {
  94. ColorHistogramFeature *f = new ColorHistogramFeature();
  95. f->window_size_x = wsx;
  96. f->window_size_y = wsy;
  97. f->bin = _bin;
  98. featurePool.addFeature ( f, 1.0 / ( numBins * nScales ) );
  99. }
  100. wsx = ( int ) ( scaleStep * wsx );
  101. wsy = ( int ) ( scaleStep * wsy );
  102. }
  103. }
  104. Feature *ColorHistogramFeature::clone() const
  105. {
  106. ColorHistogramFeature *f = new ColorHistogramFeature();
  107. f->window_size_x = window_size_x;
  108. f->window_size_y = window_size_y;
  109. f->bin = bin;
  110. return f;
  111. }
  112. Feature *ColorHistogramFeature::generateFirstParameter () const
  113. {
  114. return clone();
  115. }
  116. void ColorHistogramFeature::restore ( istream & is, int format )
  117. {
  118. is >> window_size_x;
  119. is >> window_size_y;
  120. is >> bin;
  121. }
  122. void ColorHistogramFeature::store ( ostream & os, int format ) const
  123. {
  124. os << "ColorHistogramFeature "
  125. << window_size_x << " "
  126. << window_size_y << " "
  127. << bin;
  128. }
  129. void ColorHistogramFeature::clear ()
  130. {
  131. }