SemanticFeature.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. /**
  2. * @file SemanticFeature.cpp
  3. * @brief texton feature similar to jamie shottons idea
  4. * @author Erik Rodner
  5. * @date 05/07/2008
  6. */
  7. #include <iostream>
  8. #include "SemanticFeature.h"
  9. #include "vislearning/cbaselib/FeaturePool.h"
  10. using namespace OBJREC;
  11. using namespace std;
  12. using namespace NICE;
  13. /** simple constructor */
  14. SemanticFeature::SemanticFeature ( const Config *conf,
  15. const set<int> *_possibleClassNos )
  16. : possibleClassNos ( _possibleClassNos )
  17. {
  18. window_size_x = conf->gI ( "SemanticFeature", "window_size_x", 21 );
  19. window_size_y = conf->gI ( "SemanticFeature", "window_size_y", 21 );
  20. scaleStep = conf->gD ( "SemanticFeature", "scale_step", sqrt ( 2.0f ) );
  21. numScales = conf->gI ( "SemanticFeature", "num_scales", 5 );
  22. end_shiftx = conf->gI ( "SemanticFeature", "end_shift_x", 40 );
  23. end_shifty = conf->gI ( "SemanticFeature", "end_shift_y", 40 );
  24. step_shiftx = conf->gI ( "SemanticFeature", "step_shift_x", 5 );
  25. step_shifty = conf->gI ( "SemanticFeature", "step_shift_y", 5 );
  26. shiftx = 0;
  27. shifty = 0;
  28. }
  29. SemanticFeature::SemanticFeature ( const Config *conf )
  30. {
  31. SemanticFeature ( conf, NULL );
  32. }
  33. /** simple destructor */
  34. SemanticFeature::~SemanticFeature()
  35. {
  36. }
  37. double SemanticFeature::val ( const Example *example ) const
  38. {
  39. const NICE::MultiChannelImageT<double> & img = example->ce->getDChannel (
  40. CachedExample::D_INTEGRALPRIOR );
  41. int xsize;
  42. int ysize;
  43. example->ce->getImageSize ( xsize, ysize );
  44. int tm_xsize = img.width();
  45. int tm_ysize = img.height();
  46. #if 0
  47. int xtl = example->x - window_size_x / 2;
  48. int ytl = example->y - window_size_y / 2;
  49. int xrb = example->x + window_size_x / 2;
  50. int yrb = example->y + window_size_y / 2;
  51. xtl = xtl * tm_xsize / xsize;
  52. ytl = ytl * tm_ysize / ysize;
  53. xrb = xrb * tm_xsize / xsize;
  54. yrb = yrb * tm_ysize / ysize;
  55. #endif
  56. int wsx2 = window_size_x * tm_xsize / ( 2 * xsize );
  57. int wsy2 = window_size_y * tm_ysize / ( 2 * ysize );
  58. int xx = ( example->x + shiftx ) * tm_xsize / xsize;
  59. int yy = ( example->y + shifty ) * tm_ysize / ysize;
  60. int xtl = xx - wsx2;
  61. int ytl = yy - wsy2;
  62. int xrb = xx + wsx2;
  63. int yrb = yy + wsy2;
  64. #define BOUND(x,min,max) (((x)<(min))?(min):((x)>(max)?(max):(x)))
  65. xtl = BOUND ( xtl, 0, tm_xsize - 1 );
  66. ytl = BOUND ( ytl, 0, tm_ysize - 1 );
  67. xrb = BOUND ( xrb, 0, tm_xsize - 1 );
  68. yrb = BOUND ( yrb, 0, tm_ysize - 1 );
  69. #undef BOUND
  70. double A, B, C, D;
  71. A = img.get(xtl,ytl,classno);
  72. B = img.get(xrb,ytl,classno);
  73. C = img.get(xtl,yrb,classno);
  74. D = img.get(xrb,yrb,classno);
  75. int area = ( xrb - xtl ) * ( yrb - ytl );
  76. /*******************************
  77. BE CAREFUL
  78. THIS INCORPORATES POSTION
  79. INFORMATION INDIRECTLY
  80. ********************************/
  81. if ( area == 0 )
  82. {
  83. return 0.0;
  84. }
  85. else
  86. {
  87. /* A B
  88. C D */
  89. return ( D - B - C + A ) / area;
  90. }
  91. }
  92. void SemanticFeature::explode ( FeaturePool & featurePool, bool variableWindow ) const
  93. {
  94. if ( possibleClassNos == NULL )
  95. {
  96. fprintf ( stderr, "SemanticFeature::explode: no classno set given !\n" );
  97. exit ( -1 );
  98. }
  99. // use leaf nodes only !!
  100. for ( set<int>::const_iterator k = possibleClassNos->begin();
  101. k != possibleClassNos->end();
  102. k++ )
  103. {
  104. for ( int sy = 0 ; sy <= end_shifty ; sy += step_shifty )
  105. {
  106. for ( int sx = 0 ; sx <= end_shiftx ; sx += step_shiftx )
  107. {
  108. int wsy = window_size_y;
  109. int wsx = window_size_x;
  110. for ( int i = 0 ; i < numScales ; i++ )
  111. {
  112. SemanticFeature *f = new SemanticFeature();
  113. f->classno = *k;
  114. f->window_size_x = wsx;
  115. f->window_size_y = wsy;
  116. f->shiftx = sx;
  117. f->shifty = sy;
  118. featurePool.addFeature ( f, step_shiftx * step_shifty / ( double ) ( end_shiftx * end_shifty * possibleClassNos->size() ) );
  119. wsx = ( int ) ( scaleStep * wsx );
  120. wsy = ( int ) ( scaleStep * wsy );
  121. }
  122. }
  123. }
  124. }
  125. }
  126. Feature *SemanticFeature::clone() const
  127. {
  128. SemanticFeature *f = new SemanticFeature();
  129. f->window_size_x = window_size_x;
  130. f->window_size_y = window_size_y;
  131. f->classno = classno;
  132. f->shiftx = shiftx;
  133. f->shifty = shifty;
  134. return f;
  135. }
  136. Feature *SemanticFeature::generateFirstParameter () const
  137. {
  138. return clone();
  139. }
  140. void SemanticFeature::restore ( istream & is, int format )
  141. {
  142. is >> window_size_x;
  143. is >> window_size_y;
  144. is >> shiftx;
  145. is >> shifty;
  146. is >> classno;
  147. }
  148. void SemanticFeature::store ( ostream & os, int format ) const
  149. {
  150. os << "SemanticFeature "
  151. << window_size_x << " "
  152. << window_size_y << " "
  153. << shiftx << " "
  154. << shifty << " "
  155. << classno;
  156. }
  157. void SemanticFeature::clear ()
  158. {
  159. }