ColorHistogramFeature.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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) );
  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::MultiChannelImageT<double> & img =
  33. example->ce->getDChannel ( CachedExample::D_INTEGRALCOLOR );
  34. int tm_xsize = img.xsize;
  35. int tm_ysize = img.ysize;
  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.numChannels );
  64. assert ( img.data[bin] != NULL );
  65. long kA = xtl + ytl * tm_xsize;
  66. long kB = xrb + ytl * tm_xsize;
  67. long kC = xtl + yrb * tm_xsize;
  68. long kD = xrb + yrb * tm_xsize;
  69. double A,B,C,D;
  70. A = img.data[bin][ kA ];
  71. B = img.data[bin][ kB ];
  72. C = img.data[bin][ kC ];
  73. D = img.data[bin][ kD ];
  74. double val1 = (D - B - C + A);
  75. double sum = val1*val1;
  76. for ( int b = 0 ; b < (int)img.numChannels ; b++)
  77. {
  78. if ( b == bin ) continue;
  79. A = img.data[b][ kA ];
  80. B = img.data[b][ kB ];
  81. C = img.data[b][ kC ];
  82. D = img.data[b][ kD ];
  83. double val = ( D - B - C + A );
  84. sum += val*val;
  85. }
  86. sum = sqrt(sum);
  87. return ( val1 + epsilon ) / ( sum + epsilon );
  88. }
  89. void ColorHistogramFeature::explode ( FeaturePool & featurePool, bool variableWindow ) const
  90. {
  91. int nScales = (variableWindow ? numScales : 1 );
  92. for ( int i = 0 ; i < nScales ; i++ )
  93. {
  94. int wsy = window_size_y;
  95. int wsx = window_size_x;
  96. for ( int _bin = 0 ; _bin < numBins ; _bin++ )
  97. {
  98. ColorHistogramFeature *f = new ColorHistogramFeature();
  99. f->window_size_x = wsx;
  100. f->window_size_y = wsy;
  101. f->bin = _bin;
  102. featurePool.addFeature ( f, 1.0 / ( numBins * nScales ) );
  103. }
  104. wsx = (int) (scaleStep * wsx);
  105. wsy = (int) (scaleStep * wsy);
  106. }
  107. }
  108. Feature *ColorHistogramFeature::clone() const
  109. {
  110. ColorHistogramFeature *f = new ColorHistogramFeature();
  111. f->window_size_x = window_size_x;
  112. f->window_size_y = window_size_y;
  113. f->bin = bin;
  114. return f;
  115. }
  116. Feature *ColorHistogramFeature::generateFirstParameter () const
  117. {
  118. return clone();
  119. }
  120. void ColorHistogramFeature::restore (istream & is, int format)
  121. {
  122. is >> window_size_x;
  123. is >> window_size_y;
  124. is >> bin;
  125. }
  126. void ColorHistogramFeature::store (ostream & os, int format) const
  127. {
  128. os << "ColorHistogramFeature "
  129. << window_size_x << " "
  130. << window_size_y << " "
  131. << bin;
  132. }
  133. void ColorHistogramFeature::clear ()
  134. {
  135. }