PixelPairFeature.cpp 6.4 KB

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