SemanticFeature.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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) );
  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.xsize;
  45. int tm_ysize = img.ysize;
  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.data[classno][ xtl + ytl * tm_xsize ];
  72. B = img.data[classno][ xrb + ytl * tm_xsize ];
  73. C = img.data[classno][ xtl + yrb * tm_xsize ];
  74. D = img.data[classno][ xrb + yrb * tm_xsize ];
  75. int area = (xrb - xtl)*(yrb - ytl);
  76. /*******************************
  77. BE CAREFUL
  78. THIS INCORPORATES POSTION
  79. INFORMATION INDIRECTLY
  80. ********************************/
  81. if ( area == 0 )
  82. return 0.0;
  83. else
  84. /* A B
  85. C D */
  86. return (D - B - C + A) / area;
  87. }
  88. void SemanticFeature::explode ( FeaturePool & featurePool, bool variableWindow ) const
  89. {
  90. if ( possibleClassNos == NULL )
  91. {
  92. fprintf (stderr, "SemanticFeature::explode: no classno set given !\n");
  93. exit(-1);
  94. }
  95. // use leaf nodes only !!
  96. for ( set<int>::const_iterator k = possibleClassNos->begin();
  97. k != possibleClassNos->end();
  98. k++ )
  99. {
  100. for ( int sy = 0 ; sy <= end_shifty ; sy += step_shifty )
  101. {
  102. for ( int sx = 0 ; sx <= end_shiftx ; sx += step_shiftx )
  103. {
  104. int wsy = window_size_y;
  105. int wsx = window_size_x;
  106. for ( int i = 0 ; i < numScales ; i++ )
  107. {
  108. SemanticFeature *f = new SemanticFeature();
  109. f->classno = *k;
  110. f->window_size_x = wsx;
  111. f->window_size_y = wsy;
  112. f->shiftx = sx;
  113. f->shifty = sy;
  114. featurePool.addFeature ( f, step_shiftx * step_shifty / (double)( end_shiftx * end_shifty * possibleClassNos->size() ) );
  115. wsx = (int) (scaleStep * wsx);
  116. wsy = (int) (scaleStep * wsy);
  117. }
  118. }
  119. }
  120. }
  121. }
  122. Feature *SemanticFeature::clone() const
  123. {
  124. SemanticFeature *f = new SemanticFeature();
  125. f->window_size_x = window_size_x;
  126. f->window_size_y = window_size_y;
  127. f->classno = classno;
  128. f->shiftx = shiftx;
  129. f->shifty = shifty;
  130. return f;
  131. }
  132. Feature *SemanticFeature::generateFirstParameter () const
  133. {
  134. return clone();
  135. }
  136. void SemanticFeature::restore (istream & is, int format)
  137. {
  138. is >> window_size_x;
  139. is >> window_size_y;
  140. is >> shiftx;
  141. is >> shifty;
  142. is >> classno;
  143. }
  144. void SemanticFeature::store (ostream & os, int format) const
  145. {
  146. os << "SemanticFeature "
  147. << window_size_x << " "
  148. << window_size_y << " "
  149. << shiftx << " "
  150. << shifty << " "
  151. << classno;
  152. }
  153. void SemanticFeature::clear ()
  154. {
  155. }