Operations.cpp 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. #include "Operations.h"
  2. using namespace OBJREC;
  3. using namespace std;
  4. using namespace NICE;
  5. Operation::Operation()
  6. {
  7. values = NULL;
  8. maxtypes = 1000;
  9. }
  10. void Operation::set ( int _x1, int _y1, int _x2, int _y2, int _channel1, int _channel2, ValueAccess *_values )
  11. {
  12. x1 = _x1;
  13. y1 = _y1;
  14. x2 = _x2;
  15. y2 = _y2;
  16. channel1 = _channel1;
  17. channel2 = _channel2;
  18. values = _values;
  19. }
  20. void Operation::setContext ( bool _context )
  21. {
  22. context = _context;
  23. }
  24. bool Operation::getContext()
  25. {
  26. return context;
  27. }
  28. void Operation::setFeatType ( int _featType )
  29. {
  30. featType = _featType;
  31. }
  32. int Operation::getFeatType()
  33. {
  34. return featType;
  35. }
  36. void Operation::getXY ( const Features &feats, int &xsize, int &ysize )
  37. {
  38. xsize = feats.feats->width();
  39. ysize = feats.feats->height();
  40. }
  41. void Operation::store ( std::ostream & os )
  42. {
  43. os << x1 << " " << x2 << " " << y1 << " " << y2 << " " << channel1 << " " << channel2 << " " << featType << std::endl;
  44. if ( values == NULL )
  45. os << -1 << std::endl;
  46. else
  47. os << values->getType() << std::endl;
  48. }
  49. void Operation::restore ( std::istream &is )
  50. {
  51. is >> x1;
  52. is >> x2;
  53. is >> y1;
  54. is >> y2;
  55. is >> channel1;
  56. is >> channel2;
  57. is >> featType;
  58. int tmp;
  59. is >> tmp;
  60. if ( tmp >= 0 )
  61. {
  62. if ( tmp == RAWFEAT )
  63. {
  64. values = new MCImageAccess();
  65. }
  66. else if ( tmp == CONTEXT )
  67. {
  68. values = new ClassificationResultAccess();
  69. }
  70. else
  71. {
  72. throw ( "no valid ValueAccess" );
  73. }
  74. }
  75. else
  76. {
  77. values = NULL;
  78. }
  79. }
  80. std::string Operation::writeInfos()
  81. {
  82. std::stringstream ss;
  83. ss << " x1: " << x1 << " y1: " << y1 << " x2: " << x2 << " y2: " << y2 << " c1: " << channel1 << " c2: " << channel2;
  84. return ss.str();
  85. }
  86. double RegionFeat::getVal ( const Features &feats, const int &x, const int &y )
  87. {
  88. return (*feats.rProbs)[(*feats.feats)(x,y,channel1)][channel2];
  89. }
  90. double Minus::getVal ( const Features &feats, const int &x, const int &y )
  91. {
  92. int xsize, ysize;
  93. getXY ( feats, xsize, ysize );
  94. double v1 = values->getVal ( feats, BOUND ( x + x1, 0, xsize - 1 ), BOUND ( y + y1, 0, ysize - 1 ), channel1 );
  95. double v2 = values->getVal ( feats, BOUND ( x + x2, 0, xsize - 1 ), BOUND ( y + y2, 0, ysize - 1 ), channel2 );
  96. return v1 -v2;
  97. }
  98. double MinusAbs::getVal ( const Features &feats, const int &x, const int &y )
  99. {
  100. int xsize, ysize;
  101. getXY ( feats, xsize, ysize );
  102. double v1 = values->getVal ( feats, BOUND ( x + x1, 0, xsize - 1 ), BOUND ( y + y1, 0, ysize - 1 ), channel1 );
  103. double v2 = values->getVal ( feats, BOUND ( x + x2, 0, xsize - 1 ), BOUND ( y + y2, 0, ysize - 1 ), channel2 );
  104. return abs ( v1 -v2 );
  105. }
  106. double Addition::getVal ( const Features &feats, const int &x, const int &y )
  107. {
  108. int xsize, ysize;
  109. getXY ( feats, xsize, ysize );
  110. double v1 = values->getVal ( feats, BOUND ( x + x1, 0, xsize - 1 ), BOUND ( y + y1, 0, ysize - 1 ), channel1 );
  111. double v2 = values->getVal ( feats, BOUND ( x + x2, 0, xsize - 1 ), BOUND ( y + y2, 0, ysize -
  112. 1 ), channel2 );
  113. return v1 + v2;
  114. }
  115. double Only1::getVal ( const Features &feats, const int &x, const int &y )
  116. {
  117. int xsize, ysize;
  118. getXY ( feats, xsize, ysize );
  119. double v1 = values->getVal ( feats, BOUND ( x + x1, 0, xsize - 1 ), BOUND ( y + y1, 0, ysize - 1 ), channel1 );
  120. return v1;
  121. }
  122. double RelativeXPosition::getVal ( const Features &feats, const int &x, const int &y )
  123. {
  124. int xsize, ysize;
  125. getXY ( feats, xsize, ysize );
  126. return ( double ) x / ( double ) xsize;
  127. }
  128. double RelativeYPosition::getVal ( const Features &feats, const int &x, const int &y )
  129. {
  130. int xsize, ysize;
  131. getXY ( feats, xsize, ysize );
  132. return ( double ) y / ( double ) ysize;
  133. }
  134. double IntegralOps::getVal ( const Features &feats, const int &x, const int &y )
  135. {
  136. return feats.feats->getIntegralValue(x + x1, y + y1, x + x2, y + y2, channel1);
  137. }
  138. double GlobalFeats::getVal ( const Features &feats, const int &x, const int &y )
  139. {
  140. int xsize, ysize;
  141. getXY ( feats, xsize, ysize );
  142. return feats.feats->getIntegralValue( 0, 0, xsize - 1, ysize - 1, channel1 );
  143. }
  144. double IntegralCenteredOps::getVal ( const Features &feats, const int &x, const int &y )
  145. {
  146. return feats.feats->getIntegralValue(x - x1, y - y1, x + x1, y + y1, channel1);
  147. }
  148. double BiIntegralCenteredOps::getVal ( const Features &feats, const int &x, const int &y )
  149. {
  150. return feats.feats->getIntegralValue(x - x1, y - y1, x + x1, y + y1, channel1 ) - feats.feats->getIntegralValue(x - x2, y - y2, x + x2, y + y2, channel1);
  151. }
  152. double HaarHorizontal::getVal ( const Features &feats, const int &x, const int &y )
  153. {
  154. int tlx = x - x1;
  155. int tly = y - y1;
  156. int lrx = x + x1;
  157. int lry = y + y1;
  158. return feats.feats->getIntegralValue(tlx, tly, lrx, y, channel1 ) - feats.feats->getIntegralValue(tlx, y, lrx, lry, channel1);
  159. }
  160. double HaarVertical::getVal ( const Features &feats, const int &x, const int &y )
  161. {
  162. int tlx = x - x1;
  163. int tly = y - y1;
  164. int lrx = x + x1;
  165. int lry = y + y1;
  166. return feats.feats->getIntegralValue(tlx, tly, x, lry, channel1) - feats.feats->getIntegralValue(x, tly, lrx, lry, channel1);
  167. }
  168. double HaarDiag::getVal ( const Features &feats, const int &x, const int &y )
  169. {
  170. int tlx = x - x1;
  171. int tly = y - y1;
  172. int lrx = x + x1;
  173. int lry = y + y1;
  174. return feats.feats->getIntegralValue(tlx, tly, x, y, channel1) + feats.feats->getIntegralValue(x, y, lrx, lry, channel1) - feats.feats->getIntegralValue(tlx, y, x, lry, channel1) - feats.feats->getIntegralValue(x, tly, lrx, y, channel1);
  175. }
  176. double Haar3Horiz::getVal ( const Features &feats, const int &x, const int &y )
  177. {
  178. int tlx = x - x2;
  179. int tly = y - y2;
  180. int mtly = y - y1;
  181. int mlry = y + y1;
  182. int lrx = x + x2;
  183. int lry = y + y2;
  184. return feats.feats->getIntegralValue(tlx, tly, lrx, mtly, channel1) - feats.feats->getIntegralValue(tlx, mtly, lrx, mlry, channel1) + feats.feats->getIntegralValue(tlx, mlry, lrx, lry, channel1);
  185. }
  186. double Haar3Vert::getVal ( const Features &feats, const int &x, const int &y )
  187. {
  188. int tlx = x - x2;
  189. int tly = y - y2;
  190. int mtlx = x - x1;
  191. int mlrx = x + x1;
  192. int lrx = x + x2;
  193. int lry = y + y2;
  194. return feats.feats->getIntegralValue(tlx, tly, mtlx, lry, channel1) - feats.feats->getIntegralValue(mtlx, tly, mlrx, lry, channel1) + feats.feats->getIntegralValue(mlrx, tly, lrx, lry, channel1);
  195. }
  196. void IntegralOps::set ( int _x1, int _y1, int _x2, int _y2, int _channel1, int _channel2, ValueAccess *_values )
  197. {
  198. x1 = std::min ( _x1, _x2 );
  199. y1 = std::min ( _y1, _y2 );
  200. x2 = std::max ( _x1, _x2 );
  201. y2 = std::max ( _y1, _y2 );
  202. channel1 = _channel1;
  203. channel2 = _channel2;
  204. values = _values;
  205. }
  206. void IntegralCenteredOps::set ( int _x1, int _y1, int _x2, int _y2, int _channel1, int _channel2, ValueAccess *_values )
  207. {
  208. x1 = abs ( _x1 );
  209. y1 = abs ( _y1 );
  210. x2 = abs ( _x2 );
  211. y2 = abs ( _y2 );
  212. channel1 = _channel1;
  213. channel2 = _channel2;
  214. values = _values;
  215. }
  216. void BiIntegralCenteredOps::set ( int _x1, int _y1, int _x2, int _y2, int _channel1, int _channel2, ValueAccess *_values )
  217. {
  218. x1 = std::min ( abs ( _x1 ), abs ( _x2 ) );
  219. y1 = std::min ( abs ( _y1 ), abs ( _y2 ) );
  220. x2 = std::max ( abs ( _x1 ), abs ( _x2 ) );
  221. y2 = std::max ( abs ( _y1 ), abs ( _y2 ) );
  222. channel1 = _channel1;
  223. channel2 = _channel2;
  224. values = _values;
  225. }