Operations.cpp 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  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 Minus::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 v1 -v2;
  84. }
  85. double MinusAbs::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 abs ( v1 -v2 );
  92. }
  93. double Addition::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 -
  99. 1 ), channel2 );
  100. return v1 + v2;
  101. }
  102. double Only1::getVal ( const Features &feats, const int &x, const int &y )
  103. {
  104. int xsize, ysize;
  105. getXY ( feats, xsize, ysize );
  106. double v1 = values->getVal ( feats, BOUND ( x + x1, 0, xsize - 1 ), BOUND ( y + y1, 0, ysize - 1 ), channel1 );
  107. return v1;
  108. }
  109. double RelativeXPosition::getVal ( const Features &feats, const int &x, const int &y )
  110. {
  111. int xsize, ysize;
  112. getXY ( feats, xsize, ysize );
  113. return ( double ) x / ( double ) xsize;
  114. }
  115. double RelativeYPosition::getVal ( const Features &feats, const int &x, const int &y )
  116. {
  117. int xsize, ysize;
  118. getXY ( feats, xsize, ysize );
  119. return ( double ) x / ( double ) xsize;
  120. }
  121. double IntegralOps::getVal ( const Features &feats, const int &x, const int &y )
  122. {
  123. int xsize, ysize;
  124. getXY ( feats, xsize, ysize );
  125. return computeMean ( *feats.integralImg, 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 );
  126. }
  127. double GlobalFeats::getVal ( const Features &feats, const int &x, const int &y )
  128. {
  129. int xsize, ysize;
  130. getXY ( feats, xsize, ysize );
  131. return computeMean ( *feats.integralImg, 0, 0, xsize - 1, ysize - 1, channel1 );
  132. }
  133. double IntegralCenteredOps::getVal ( const Features &feats, const int &x, const int &y )
  134. {
  135. int xsize, ysize;
  136. getXY ( feats, xsize, ysize );
  137. return computeMean ( *feats.integralImg, 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 );
  138. }
  139. double BiIntegralCenteredOps::getVal ( const Features &feats, const int &x, const int &y )
  140. {
  141. int xsize, ysize;
  142. getXY ( feats, xsize, ysize );
  143. return computeMean ( *feats.integralImg, 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.integralImg, 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 );
  144. }
  145. double HaarHorizontal::getVal ( const Features &feats, const int &x, const int &y )
  146. {
  147. int xsize, ysize;
  148. getXY ( feats, xsize, ysize );
  149. int tlx = BOUND ( x - x1, 0, xsize - 1 );
  150. int tly = BOUND ( y - y1, 0, ysize - 1 );
  151. int lrx = BOUND ( x + x1, 0, xsize - 1 );
  152. int lry = BOUND ( y + y1, 0, ysize - 1 );
  153. return computeMean ( *feats.integralImg, tlx, tly, lrx, y, channel1 ) - computeMean ( *feats.integralImg, tlx, y, lrx, lry, channel1 );
  154. }
  155. double HaarVertical::getVal ( const Features &feats, const int &x, const int &y )
  156. {
  157. int xsize, ysize;
  158. getXY ( feats, xsize, ysize );
  159. int tlx = BOUND ( x - x1, 0, xsize - 1 );
  160. int tly = BOUND ( y - y1, 0, ysize - 1 );
  161. int lrx = BOUND ( x + x1, 0, xsize - 1 );
  162. int lry = BOUND ( y + y1, 0, ysize - 1 );
  163. return computeMean ( *feats.integralImg, tlx, tly, x, lry, channel1 ) - computeMean ( *feats.integralImg, x, tly, lrx, lry, channel1 );
  164. }
  165. double HaarDiag::getVal ( const Features &feats, const int &x, const int &y )
  166. {
  167. int xsize, ysize;
  168. getXY ( feats, xsize, ysize );
  169. int tlx = BOUND ( x - x1, 0, xsize - 1 );
  170. int tly = BOUND ( y - y1, 0, ysize - 1 );
  171. int lrx = BOUND ( x + x1, 0, xsize - 1 );
  172. int lry = BOUND ( y + y1, 0, ysize - 1 );
  173. return computeMean ( *feats.integralImg, tlx, tly, x, y, channel1 ) + computeMean ( *feats.integralImg, x, y, lrx, lry, channel1 ) - computeMean ( *feats.integralImg, tlx, y, x, lry, channel1 ) - computeMean ( *feats.integralImg, x, tly, lrx, y, channel1 );
  174. }
  175. double Haar3Horiz::getVal ( const Features &feats, const int &x, const int &y )
  176. {
  177. int xsize, ysize;
  178. getXY ( feats, xsize, ysize );
  179. int tlx = BOUND ( x - x2, 0, xsize - 1 );
  180. int tly = BOUND ( y - y2, 0, ysize - 1 );
  181. int mtly = BOUND ( y - y1, 0, ysize - 1 );
  182. int mlry = BOUND ( y + y1, 0, ysize - 1 );
  183. int lrx = BOUND ( x + x2, 0, xsize - 1 );
  184. int lry = BOUND ( y + y2, 0, ysize - 1 );
  185. return computeMean ( *feats.integralImg, tlx, tly, lrx, mtly, channel1 ) - computeMean ( *feats.integralImg, tlx, mtly, lrx, mlry, channel1 ) + computeMean ( *feats.integralImg, tlx, mlry, lrx, lry, channel1 );
  186. }
  187. double Haar3Vert::getVal ( const Features &feats, const int &x, const int &y )
  188. {
  189. int xsize, ysize;
  190. getXY ( feats, xsize, ysize );
  191. int tlx = BOUND ( x - x2, 0, xsize - 1 );
  192. int tly = BOUND ( y - y2, 0, ysize - 1 );
  193. int mtlx = BOUND ( x - x1, 0, xsize - 1 );
  194. int mlrx = BOUND ( x + x1, 0, xsize - 1 );
  195. int lrx = BOUND ( x + x2, 0, xsize - 1 );
  196. int lry = BOUND ( y + y2, 0, ysize - 1 );
  197. return computeMean ( *feats.integralImg, tlx, tly, mtlx, lry, channel1 ) - computeMean ( *feats.integralImg, mtlx, tly, mlrx, lry, channel1 ) + computeMean ( *feats.integralImg, mlrx, tly, lrx, lry, channel1 );
  198. }
  199. void IntegralOps::set ( int _x1, int _y1, int _x2, int _y2, int _channel1, int _channel2, ValueAccess *_values )
  200. {
  201. x1 = std::min ( _x1, _x2 );
  202. y1 = std::min ( _y1, _y2 );
  203. x2 = std::max ( _x1, _x2 );
  204. y2 = std::max ( _y1, _y2 );
  205. channel1 = _channel1;
  206. channel2 = _channel2;
  207. values = _values;
  208. }
  209. double IntegralOps::computeMean ( const NICE::MultiChannelImageT<double> &intImg, const int &uLx, const int &uLy, const int &lRx, const int &lRy, const int &chan )
  210. {
  211. double val1 = intImg.get ( uLx, uLy, chan );
  212. double val2 = intImg.get ( lRx, uLy, chan );
  213. double val3 = intImg.get ( uLx, lRy, chan );
  214. double val4 = intImg.get ( lRx, lRy, chan );
  215. double area = ( lRx - uLx ) * ( lRy - uLy );
  216. if ( area == 0 )
  217. return 0.0;
  218. return ( val1 + val4 - val2 - val3 ) / area;
  219. }
  220. void IntegralCenteredOps::set ( int _x1, int _y1, int _x2, int _y2, int _channel1, int _channel2, ValueAccess *_values )
  221. {
  222. x1 = abs ( _x1 );
  223. y1 = abs ( _y1 );
  224. x2 = abs ( _x2 );
  225. y2 = abs ( _y2 );
  226. channel1 = _channel1;
  227. channel2 = _channel2;
  228. values = _values;
  229. }
  230. void BiIntegralCenteredOps::set ( int _x1, int _y1, int _x2, int _y2, int _channel1, int _channel2, ValueAccess *_values )
  231. {
  232. x1 = std::min ( abs ( _x1 ), abs ( _x2 ) );
  233. y1 = std::min ( abs ( _y1 ), abs ( _y2 ) );
  234. x2 = std::max ( abs ( _x1 ), abs ( _x2 ) );
  235. y2 = std::max ( abs ( _y1 ), abs ( _y2 ) );
  236. channel1 = _channel1;
  237. channel2 = _channel2;
  238. values = _values;
  239. }