Operations.cpp 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  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::getXY ( const Features &feats, int &xsize, int &ysize )
  29. {
  30. xsize = feats.feats->width();
  31. ysize = feats.feats->height();
  32. }
  33. void Operation::store ( std::ostream & os )
  34. {
  35. os << x1 << " " << x2 << " " << y1 << " " << y2 << " " << channel1 << " " << channel2 << std::endl;
  36. if ( values == NULL )
  37. os << -1 << std::endl;
  38. else
  39. os << values->getType() << std::endl;
  40. }
  41. void Operation::restore ( std::istream &is )
  42. {
  43. is >> x1;
  44. is >> x2;
  45. is >> y1;
  46. is >> y2;
  47. is >> channel1;
  48. is >> channel2;
  49. int tmp;
  50. is >> tmp;
  51. if ( tmp >= 0 )
  52. {
  53. if ( tmp == RAWFEAT )
  54. {
  55. values = new MCImageAccess();
  56. }
  57. else if ( tmp == CONTEXT )
  58. {
  59. values = new ClassificationResultAccess();
  60. }
  61. else
  62. {
  63. throw ( "no valid ValueAccess" );
  64. }
  65. }
  66. else
  67. {
  68. values = NULL;
  69. }
  70. }
  71. std::string Operation::writeInfos()
  72. {
  73. std::stringstream ss;
  74. ss << " x1: " << x1 << " y1: " << y1 << " x2: " << x2 << " y2: " << y2 << " c1: " << channel1 << " c2: " << channel2;
  75. return ss.str();
  76. }
  77. double Equality::getVal ( const Features &feats, const int &x, const int &y )
  78. {
  79. int xsize, ysize;
  80. getXY ( feats, xsize, ysize );
  81. double v1 = values->getVal ( feats, BOUND ( x + x1, 0, xsize - 1 ), BOUND ( y + y1, 0, ysize - 1 ), channel1 );
  82. double v2 = values->getVal ( feats, BOUND ( x + x2, 0, xsize - 1 ), BOUND ( y + y2, 0, ysize - 1 ), channel2 );
  83. return (double)(v1 == v2);
  84. }
  85. double Minus::getVal ( const Features &feats, const int &x, const int &y )
  86. {
  87. int xsize, ysize;
  88. getXY ( feats, xsize, ysize );
  89. double v1 = values->getVal ( feats, BOUND ( x + x1, 0, xsize - 1 ), BOUND ( y + y1, 0, ysize - 1 ), channel1 );
  90. double v2 = values->getVal ( feats, BOUND ( x + x2, 0, xsize - 1 ), BOUND ( y + y2, 0, ysize - 1 ), channel2 );
  91. return v1 -v2;
  92. }
  93. double MinusAbs::getVal ( const Features &feats, const int &x, const int &y )
  94. {
  95. int xsize, ysize;
  96. getXY ( feats, xsize, ysize );
  97. double v1 = values->getVal ( feats, BOUND ( x + x1, 0, xsize - 1 ), BOUND ( y + y1, 0, ysize - 1 ), channel1 );
  98. double v2 = values->getVal ( feats, BOUND ( x + x2, 0, xsize - 1 ), BOUND ( y + y2, 0, ysize - 1 ), channel2 );
  99. return abs ( v1 -v2 );
  100. }
  101. double Addition::getVal ( const Features &feats, const int &x, const int &y )
  102. {
  103. int xsize, ysize;
  104. getXY ( feats, xsize, ysize );
  105. double v1 = values->getVal ( feats, BOUND ( x + x1, 0, xsize - 1 ), BOUND ( y + y1, 0, ysize - 1 ), channel1 );
  106. double v2 = values->getVal ( feats, BOUND ( x + x2, 0, xsize - 1 ), BOUND ( y + y2, 0, ysize -
  107. 1 ), channel2 );
  108. return v1 + v2;
  109. }
  110. double Only1::getVal ( const Features &feats, const int &x, const int &y )
  111. {
  112. int xsize, ysize;
  113. getXY ( feats, xsize, ysize );
  114. double v1 = values->getVal ( feats, BOUND ( x + x1, 0, xsize - 1 ), BOUND ( y + y1, 0, ysize - 1 ), channel1 );
  115. return v1;
  116. }
  117. double RelativeXPosition::getVal ( const Features &feats, const int &x, const int &y )
  118. {
  119. int xsize, ysize;
  120. getXY ( feats, xsize, ysize );
  121. return ( double ) x / ( double ) xsize;
  122. }
  123. double RelativeYPosition::getVal ( const Features &feats, const int &x, const int &y )
  124. {
  125. int xsize, ysize;
  126. getXY ( feats, xsize, ysize );
  127. return ( double ) x / ( double ) xsize;
  128. }
  129. double IntegralOps::getVal ( const Features &feats, const int &x, const int &y )
  130. {
  131. int xsize, ysize;
  132. getXY ( feats, xsize, ysize );
  133. return computeMean ( *feats.feats, BOUND ( x + x1, 0, xsize - 1 ), BOUND ( y + y1, 0, ysize - 1 ), BOUND ( x + x2, 0, xsize - 1 ), BOUND ( y + y2, 0, ysize - 1 ), channel1 );
  134. }
  135. double GlobalFeats::getVal ( const Features &feats, const int &x, const int &y )
  136. {
  137. int xsize, ysize;
  138. getXY ( feats, xsize, ysize );
  139. return computeMean ( *feats.feats, 0, 0, xsize - 1, ysize - 1, channel1 );
  140. }
  141. double IntegralCenteredOps::getVal ( const Features &feats, const int &x, const int &y )
  142. {
  143. int xsize, ysize;
  144. getXY ( feats, xsize, ysize );
  145. return computeMean ( *feats.feats, BOUND ( x - x1, 0, xsize - 1 ), BOUND ( y - y1, 0, ysize - 1 ), BOUND ( x + x1, 0, xsize - 1 ), BOUND ( y + y1, 0, ysize - 1 ), channel1 );
  146. }
  147. double BiIntegralCenteredOps::getVal ( const Features &feats, const int &x, const int &y )
  148. {
  149. int xsize, ysize;
  150. getXY ( feats, xsize, ysize );
  151. return computeMean ( *feats.feats, BOUND ( x - x1, 0, xsize - 1 ), BOUND ( y - y1, 0, ysize - 1 ), BOUND ( x + x1, 0, xsize - 1 ), BOUND ( y + y1, 0, ysize - 1 ), channel1 ) - computeMean ( *feats.feats, BOUND ( x - x2, 0, xsize - 1 ), BOUND ( y - y2, 0, ysize - 1 ), BOUND ( x + x2, 0, xsize - 1 ), BOUND ( y + y2, 0, ysize - 1 ), channel1 );
  152. }
  153. double HaarHorizontal::getVal ( const Features &feats, const int &x, const int &y )
  154. {
  155. int xsize, ysize;
  156. getXY ( feats, xsize, ysize );
  157. int tlx = BOUND ( x - x1, 0, xsize - 1 );
  158. int tly = BOUND ( y - y1, 0, ysize - 1 );
  159. int lrx = BOUND ( x + x1, 0, xsize - 1 );
  160. int lry = BOUND ( y + y1, 0, ysize - 1 );
  161. return computeMean ( *feats.feats, tlx, tly, lrx, y, channel1 ) - computeMean ( *feats.feats, tlx, y, lrx, lry, channel1 );
  162. }
  163. double HaarVertical::getVal ( const Features &feats, const int &x, const int &y )
  164. {
  165. int xsize, ysize;
  166. getXY ( feats, xsize, ysize );
  167. int tlx = BOUND ( x - x1, 0, xsize - 1 );
  168. int tly = BOUND ( y - y1, 0, ysize - 1 );
  169. int lrx = BOUND ( x + x1, 0, xsize - 1 );
  170. int lry = BOUND ( y + y1, 0, ysize - 1 );
  171. return computeMean ( *feats.feats, tlx, tly, x, lry, channel1 ) - computeMean ( *feats.feats, x, tly, lrx, lry, channel1 );
  172. }
  173. double HaarDiag::getVal ( const Features &feats, const int &x, const int &y )
  174. {
  175. int xsize, ysize;
  176. getXY ( feats, xsize, ysize );
  177. int tlx = BOUND ( x - x1, 0, xsize - 1 );
  178. int tly = BOUND ( y - y1, 0, ysize - 1 );
  179. int lrx = BOUND ( x + x1, 0, xsize - 1 );
  180. int lry = BOUND ( y + y1, 0, ysize - 1 );
  181. return computeMean ( *feats.feats, tlx, tly, x, y, channel1 ) + computeMean ( *feats.feats, x, y, lrx, lry, channel1 ) - computeMean ( *feats.feats, tlx, y, x, lry, channel1 ) - computeMean ( *feats.feats, x, tly, lrx, y, channel1 );
  182. }
  183. double Haar3Horiz::getVal ( const Features &feats, const int &x, const int &y )
  184. {
  185. int xsize, ysize;
  186. getXY ( feats, xsize, ysize );
  187. int tlx = BOUND ( x - x2, 0, xsize - 1 );
  188. int tly = BOUND ( y - y2, 0, ysize - 1 );
  189. int mtly = BOUND ( y - y1, 0, ysize - 1 );
  190. int mlry = BOUND ( y + y1, 0, ysize - 1 );
  191. int lrx = BOUND ( x + x2, 0, xsize - 1 );
  192. int lry = BOUND ( y + y2, 0, ysize - 1 );
  193. return computeMean ( *feats.feats, tlx, tly, lrx, mtly, channel1 ) - computeMean ( *feats.feats, tlx, mtly, lrx, mlry, channel1 ) + computeMean ( *feats.feats, tlx, mlry, lrx, lry, channel1 );
  194. }
  195. double Haar3Vert::getVal ( const Features &feats, const int &x, const int &y )
  196. {
  197. int xsize, ysize;
  198. getXY ( feats, xsize, ysize );
  199. int tlx = BOUND ( x - x2, 0, xsize - 1 );
  200. int tly = BOUND ( y - y2, 0, ysize - 1 );
  201. int mtlx = BOUND ( x - x1, 0, xsize - 1 );
  202. int mlrx = BOUND ( x + x1, 0, xsize - 1 );
  203. int lrx = BOUND ( x + x2, 0, xsize - 1 );
  204. int lry = BOUND ( y + y2, 0, ysize - 1 );
  205. return computeMean ( *feats.feats, tlx, tly, mtlx, lry, channel1 ) - computeMean ( *feats.feats, mtlx, tly, mlrx, lry, channel1 ) + computeMean ( *feats.feats, mlrx, tly, lrx, lry, channel1 );
  206. }
  207. void IntegralOps::set ( int _x1, int _y1, int _x2, int _y2, int _channel1, int _channel2, ValueAccess *_values )
  208. {
  209. x1 = std::min ( _x1, _x2 );
  210. y1 = std::min ( _y1, _y2 );
  211. x2 = std::max ( _x1, _x2 );
  212. y2 = std::max ( _y1, _y2 );
  213. channel1 = _channel1;
  214. channel2 = _channel2;
  215. values = _values;
  216. }
  217. double IntegralOps::computeMean ( const NICE::MultiChannelImageT<double> &intImg, const int &uLx, const int &uLy, const int &lRx, const int &lRy, const int &chan )
  218. {
  219. double val1 = intImg.get ( uLx, uLy, chan );
  220. double val2 = intImg.get ( lRx, uLy, chan );
  221. double val3 = intImg.get ( uLx, lRy, chan );
  222. double val4 = intImg.get ( lRx, lRy, chan );
  223. double area = ( lRx - uLx ) * ( lRy - uLy );
  224. if ( area == 0 )
  225. return 0.0;
  226. return ( val1 + val4 - val2 - val3 ) / area;
  227. }
  228. void IntegralCenteredOps::set ( int _x1, int _y1, int _x2, int _y2, int _channel1, int _channel2, ValueAccess *_values )
  229. {
  230. x1 = abs ( _x1 );
  231. y1 = abs ( _y1 );
  232. x2 = abs ( _x2 );
  233. y2 = abs ( _y2 );
  234. channel1 = _channel1;
  235. channel2 = _channel2;
  236. values = _values;
  237. }
  238. void BiIntegralCenteredOps::set ( int _x1, int _y1, int _x2, int _y2, int _channel1, int _channel2, ValueAccess *_values )
  239. {
  240. x1 = std::min ( abs ( _x1 ), abs ( _x2 ) );
  241. y1 = std::min ( abs ( _y1 ), abs ( _y2 ) );
  242. x2 = std::max ( abs ( _x1 ), abs ( _x2 ) );
  243. y2 = std::max ( abs ( _y1 ), abs ( _y2 ) );
  244. channel1 = _channel1;
  245. channel2 = _channel2;
  246. values = _values;
  247. }