PixelPairFeature.cpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. #include <iostream>
  2. #include "vislearning/features/fpfeatures/PixelPairFeature.h"
  3. #include "vislearning/cbaselib/FeaturePool.h"
  4. using namespace OBJREC;
  5. using namespace std;
  6. using namespace NICE;
  7. #define BOUND(x,min,max) (((x)<(min))?(min):((x)>(max)?(max):(x)))
  8. const int firstColorchannel = 0;
  9. const int lastColorchannel = 2;
  10. void PixelPairFeature::explode ( FeaturePool & featurePool, bool variableWindow ) const
  11. {
  12. PixelPairFeature *f = new PixelPairFeature ( *this );
  13. int firstchannel = ( imagetype == CachedExample::I_COLOR ) ? firstColorchannel : 0;
  14. int lastchannel = ( imagetype == CachedExample::I_COLOR ) ? lastColorchannel : 0;
  15. int wsx = window_size_x / 2;
  16. int wsy = window_size_y / 2;
  17. int numberOfPairFeatures = (lastchannel - firstchannel + 1) *
  18. (2*wsx / step_x) * (2*wsy / step_y);
  19. numberOfPairFeatures *= numberOfPairFeatures;
  20. for ( int _type = PPTYPE_DIFF ; _type < PPTYPE_VALUE ; _type++ )
  21. for ( int _x1 = -wsx ; _x1 < wsx ; _x1 += step_x )
  22. for ( int _y1 = -wsy ; _y1 < wsy ; _y1 += step_y )
  23. for ( int _b1 = firstchannel ; _b1 <= lastchannel ; _b1++ )
  24. for ( int _x2 = -wsx ; _x2 < wsx ; _x2 += step_x )
  25. for ( int _y2 = -wsy ; _y2 < wsy ; _y2 += step_y )
  26. for ( int _b2 = firstchannel ; _b2 <= lastchannel ; _b2++ )
  27. {
  28. if ( (_b1 == _b2) && (_x1 == _x2) && (_y1 == _y2) ) continue;
  29. f->x1 = _x1; f->y1 = _y1; f->b1 = _b1;
  30. f->x2 = _x2; f->y2 = _y2; f->b2 = _b2;
  31. f->type = _type;
  32. featurePool.addFeature ( f->clone(), 1.0 / ((PPTYPE_VALUE - PPTYPE_DIFF) * numberOfPairFeatures) );
  33. }
  34. f->type = PPTYPE_VALUE;
  35. for ( int _x1 = -wsx ; _x1 < wsx ; _x1 += step_x )
  36. for ( int _y1 = -wsy ; _y1 < wsy ; _y1 += step_y )
  37. for ( int _b1 = firstchannel ; _b1 <= lastchannel ; _b1++ )
  38. {
  39. f->x1 = _x1; f->y1 = _y1; f->b1 = _b1;
  40. featurePool.addFeature ( f->clone(), 1.0 / numberOfPairFeatures );
  41. }
  42. delete f;
  43. }
  44. Feature *PixelPairFeature::clone() const
  45. {
  46. PixelPairFeature *fp = new PixelPairFeature(*this);
  47. return fp;
  48. }
  49. /************* PixelPairFeature **************/
  50. PixelPairFeature::PixelPairFeature( const Config *conf )
  51. {
  52. window_size_x = conf->gI("PixelPairFeatures", "window_size_x", 24 );
  53. window_size_y = conf->gI("PixelPairFeatures", "window_size_y", 24 );
  54. step_x = conf->gI("PixelPairFeatures", "step_x", 1 );
  55. step_y = conf->gI("PixelPairFeatures", "step_y", 1 );
  56. bool use_color = conf->gB("PixelPairFeatures", "use_color", true );
  57. if ( use_color ) {
  58. imagetype = CachedExample::I_COLOR;
  59. } else {
  60. imagetype = CachedExample::I_GRAYVALUES;
  61. }
  62. }
  63. PixelPairFeature::PixelPairFeature ( int _window_size_x,
  64. int _window_size_y,
  65. int _step_x,
  66. int _step_y,
  67. int _imagetype )
  68. {
  69. window_size_x = _window_size_x;
  70. window_size_y = _window_size_y;
  71. x1 = 0; y1 = 0; b1 = firstColorchannel;
  72. x2 = 1; y1 = 0; b2 = firstColorchannel;
  73. step_x = _step_x;
  74. step_y = _step_y;
  75. type = PixelPairFeature::PPTYPE_DIFF;
  76. imagetype = _imagetype;
  77. }
  78. PixelPairFeature::~PixelPairFeature()
  79. {
  80. }
  81. double PixelPairFeature::val( const Example *example ) const
  82. {
  83. int xl = example->x;
  84. int yl = example->y;
  85. NICE::MultiChannelImageT<int> & img = example->ce->getIChannel ( imagetype );
  86. int xx1 = x1;
  87. int yy1 = y1;
  88. int xx2 = x1;
  89. int yy2 = x2;
  90. int exwidth = example->width;
  91. if ( exwidth != 0 )
  92. {
  93. int exheight = example->height;
  94. xx1 = xx1 * exwidth / window_size_x;
  95. yy1 = yy1 * exheight / window_size_y;
  96. xx2 = xx2 * exwidth / window_size_x;
  97. yy2 = yy2 * exheight / window_size_y;
  98. }
  99. int xsize = img.xsize;
  100. int ysize = img.ysize;
  101. const int *channel1 = img.data[b1];
  102. int p1x = BOUND ( xl + xx1, 0, xsize-1 );
  103. int p1y = BOUND ( yl + yy1, 0, ysize-1 );
  104. long off1 = p1x + p1y*xsize;
  105. int v1 = channel1[off1];
  106. if ( type != PPTYPE_VALUE )
  107. {
  108. const int *channel2 = img.data[b2];
  109. int p2x = BOUND ( xl + xx2, 0, xsize-1 );
  110. int p2y = BOUND ( yl + yy2, 0, ysize-1 );
  111. long off2 = p2x + p2y*xsize;
  112. int v2 = channel2[off2];
  113. if ( type == PPTYPE_DIFF )
  114. return v1 - v2;
  115. else if ( type == PPTYPE_ABSDIFF )
  116. return fabs(v1-v2);
  117. else if ( type == PPTYPE_SUM )
  118. return v1 + v2;
  119. else
  120. exit(-1);
  121. } else {
  122. return v1;
  123. }
  124. }
  125. void PixelPairFeature::restore (istream & is, int format)
  126. {
  127. is >> type;
  128. is >> imagetype;
  129. is >> window_size_x;
  130. is >> window_size_y;
  131. is >> x1;
  132. is >> y1;
  133. is >> b1;
  134. is >> x2;
  135. is >> y2;
  136. is >> b2;
  137. }
  138. void PixelPairFeature::store (ostream & os, int format) const
  139. {
  140. os << "PIXELPAIRFEATURE" << " " << type << " "
  141. << imagetype << " "
  142. << window_size_x << " " << window_size_y << " "
  143. << " " << x1 << " " << y1 << " " << b1
  144. << " " << x2 << " " << y2 << " " << b2;
  145. }
  146. void PixelPairFeature::clear ()
  147. {
  148. // nothing to do in my opinion
  149. }
  150. #if 0
  151. void PixelPairFeature::calcFeatureValues ( const Examples & examples,
  152. vector<int> & examples_selection,
  153. FeatureValuesUnsorted & values ) const
  154. {
  155. for ( vector<int>::const_iterator si = examples_selection.begin();
  156. si != examples_selection.end();
  157. si++ )
  158. {
  159. int index = *si;
  160. const pair<int, Example> & p = examples[index];
  161. int classno = p.first;
  162. const Example & example = p.second;
  163. double value = 0.0;
  164. int xsize, ysize;
  165. int xl = example.x - window_size_x/2;
  166. int yl = example.y - window_size_y/2;
  167. const double *channel1 = example.ce->getChannel ( b1, xsize, ysize );
  168. int p1x = BOUND ( xl + x1, 0, xsize-1 );
  169. int p1y = BOUND ( yl + y1, 0, ysize-1 );
  170. long off1 = p1x + p1y*xsize;
  171. double v1 = channel1[off1];
  172. if ( type != PPTYPE_VALUE )
  173. {
  174. const double *channel2 = example.ce->getChannel ( b2, xsize, ysize );
  175. int p2x = BOUND ( xl + x2, 0, xsize-1 );
  176. int p2y = BOUND ( yl + y2, 0, ysize-1 );
  177. long off2 = p2x + p2y*xsize;
  178. double v2 = channel2[off2];
  179. if ( type == PPTYPE_DIFF )
  180. value = v1 - v2;
  181. else if ( type == PPTYPE_ABSDIFF )
  182. value = fabs(v1-v2);
  183. else if ( type == PPTYPE_SUM )
  184. value = v1 + v2;
  185. } else {
  186. value = v1;
  187. }
  188. values.push_back ( quadruplet<double, int, int, double> (
  189. value, classno, index, example.weight ) );
  190. }
  191. }
  192. #endif
  193. #undef BOUND