ImageTools.tcc 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  1. #include <core/image/ImageTools.h>
  2. #include <core/image/ippwrapper.h>
  3. namespace NICE {
  4. template<class P>
  5. ImageT<P>* addConstBorder(const ImageT<P> &src, uint width, uint height, P value, ImageT<P>* dst)
  6. {
  7. ImageT<P>* result = createResultBuffer(src.width()+2*width, src.height()+2*height, dst);
  8. #ifdef NICE_USELIB_IPP
  9. IppStatus ret = ippiCopyConstBorder_C1R(src.getPixelPointer(), src.getStepsize(),
  10. makeROIFullImage(src),
  11. result->getPixelPointer(), result->getStepsize(),
  12. makeROIFullImage(*result),
  13. height, width, value);
  14. if(ret!=ippStsNoErr)
  15. fthrow(ImageException, ippGetStatusString(ret));
  16. #else // NICE_USELIB_IPP
  17. fthrow(ImageException,"Not yet supported without IPP.");
  18. #endif // NICE_USELIB_IPP
  19. return(result);
  20. }
  21. template<class P>
  22. ImageT<P>* copyBorder(const ImageT<P> &src, uint width, uint height, ImageT<P>* dst)
  23. {
  24. ImageT<P>* result = createResultBuffer(src.width(), src.height(), dst);
  25. if ( (2*(int)width > src.width()) || (2*(int)height > src.height()) )
  26. fthrow(ImageException,"Border exceeds image size.");
  27. // inefficient version
  28. for ( int y = 0 ; y < (int)height ; y++ )
  29. for ( int x = 0 ; x < src.width() ; x++ )
  30. result->setPixel ( x, y, src.getPixel ( x, y ) );
  31. for ( int y = src.height() - height ; y < src.height() ; y++ )
  32. for ( int x = 0 ; x < src.width() ; x++ )
  33. result->setPixel ( x, y, src.getPixel ( x, y ) );
  34. for ( int x = 0 ; x < (int)width ; x++ )
  35. for ( int y = (int)height ; y < src.height() - (int)height ; y++ )
  36. result->setPixel ( x, y, src.getPixel ( x, y ) );
  37. for ( int x = src.width()-width ; x < src.width() ; x++ )
  38. for ( int y = (int)height ; y < src.height() - (int)height ; y++ )
  39. result->setPixel ( x, y, src.getPixel ( x, y ) );
  40. return result;
  41. }
  42. template<class P>
  43. void autoCropRect(const ImageT<P> &src, P color, Rect &rect)
  44. {
  45. bool quit=false;
  46. for(int y=0;y<src.height();y++) {
  47. for(int x=0;x<src.width();x++) {
  48. if(src(x,y)>color) {
  49. rect.top=y;
  50. quit=true;
  51. break;
  52. }
  53. }
  54. if(quit)
  55. break;
  56. }
  57. quit=false;
  58. for(int y=src.height()-1;y>=0;y--) {
  59. for(int x=0;x<src.width();x++) {
  60. if(src(x,y)>color) {
  61. rect.height=y-rect.top+1;
  62. quit=true;
  63. break;
  64. }
  65. }
  66. if(quit)
  67. break;
  68. }
  69. quit=false;
  70. for(int x=0;x<src.width();x++) {
  71. for(int y=0;y<src.height();y++) {
  72. if(src(x,y)>color) {
  73. rect.left=x;
  74. quit=true;
  75. break;
  76. }
  77. }
  78. if(quit)
  79. break;
  80. }
  81. quit=false;
  82. for(int x=src.width()-1;x>=0;x--) {
  83. for(int y=0;y<src.height();y++) {
  84. if(src(x,y)>color) {
  85. rect.width=x-rect.left+1;
  86. quit=true;
  87. break;
  88. }
  89. }
  90. if(quit)
  91. break;
  92. }
  93. }
  94. template<class P>
  95. void normalizeToRange(const ImageT<P> &src, ImageT<P> &dst , P min, P max)
  96. {
  97. ImageT<P> cpy=src;
  98. normalizeToRange(cpy,min,max);
  99. dst=cpy;
  100. }
  101. template<class P>
  102. void normalizeToRange(ImageT<P> &src, P min, P max)
  103. {
  104. //compute scale
  105. P src_max,src_min;
  106. src.minmax(src_min,src_max);
  107. if(src_max-src_min > static_cast<P>(1e-8))
  108. {
  109. P scale=(max-min)/(src_max-src_min);
  110. for (int y=0;y<src.height();y++)
  111. {
  112. for (int x=0;x<src.width();x++)
  113. {
  114. P value=src.getPixelQuick(x,y);
  115. //shift to zero
  116. value-=src_min;
  117. //scale to destined range
  118. value*=scale;
  119. //shift to new minimum;
  120. value+=min;
  121. src.setPixelQuick(x,y,value);
  122. }
  123. }
  124. }
  125. else
  126. {//maybe exception, but clever handling seems the better choice
  127. for (int y=0;y<src.height();y++)
  128. {
  129. for (int x=0;x<src.width();x++)
  130. {
  131. P value=src.getPixelQuick(x,y);
  132. //shift to zero
  133. value-=src_min;
  134. //shift to new minimum;
  135. value+=min;
  136. src.setPixelQuick(x,y,value);
  137. }
  138. }
  139. fprintf(stderr,"normalizeToRange: Input-Image max value = min value");
  140. }
  141. }
  142. template<class P>
  143. void findLocalMinima(const ImageT<P> &src, P thresh, int dist, std::vector<Coord> &minima)
  144. {
  145. ImageT<P> cpy=src;
  146. Coord minidx = cpy.minIndex();
  147. P min = cpy(minidx);
  148. while(min<thresh) {
  149. minima.push_back(minidx);
  150. int top = (minidx.y>dist)? minidx.y-dist : 0;
  151. int left = (minidx.x>dist)? minidx.x-dist : 0;
  152. int height = (minidx.y+dist<src.height())? minidx.y-top+dist : src.height()-top;
  153. int width = (minidx.x+dist<src.width())? minidx.x-left+dist : src.width()-left;
  154. Rect area(left, top, width, height);
  155. ImageT<P> simage = cpy.subImage(area);
  156. simage=std::numeric_limits<P>::max();
  157. minidx = cpy.minIndex();
  158. min = cpy(minidx);
  159. }
  160. }
  161. template<class P>
  162. void findLocalMaxima(const ImageT<P> &src, P thresh, int dist, std::vector<Coord> &maxima)
  163. {
  164. ImageT<P> cpy=src;
  165. Coord maxidx = cpy.maxIndex();
  166. P max = cpy(maxidx);
  167. while(max>thresh) {
  168. maxima.push_back(maxidx);
  169. int top = (maxidx.y>dist)? maxidx.y-dist : 0;
  170. int left = (maxidx.x>dist)? maxidx.x-dist : 0;
  171. int height = (maxidx.y+dist<src.height())? maxidx.y-top+dist : src.height()-top;
  172. int width = (maxidx.x+dist<src.width())? maxidx.x-left+dist : src.width()-left;
  173. Rect area(left, top, width, height);
  174. ImageT<P> simage = cpy.subImage(area);
  175. simage=std::numeric_limits<P>::min();
  176. maxidx = cpy.maxIndex();
  177. max = cpy(maxidx);
  178. }
  179. }
  180. template<class P>
  181. IppiConnectedComp* floodFill4Connected(ImageT<P>& src,
  182. const IppiPoint& seed,
  183. const P& newVal,
  184. IppiConnectedComp* compBuffer) {
  185. if(compBuffer==NULL)
  186. compBuffer = new IppiConnectedComp;
  187. #ifdef NICE_USELIB_IPP
  188. IppiSize roiSize = {src.width(), src.height()};
  189. Ipp32s buffSize;
  190. ippiFloodFillGetSize(roiSize, &buffSize);
  191. Ipp8u* floodBuffer = new Ipp8u[buffSize];
  192. IppStatus ret = ippiFloodFill_4Con_C1IR(src.getPixelPointer(),
  193. src.getStepsize(),
  194. roiSize, seed, newVal,
  195. compBuffer, floodBuffer);
  196. if(ret!=ippStsNoErr)
  197. fthrow(ImageException, ippGetStatusString(ret));
  198. // clean up
  199. delete floodBuffer;
  200. return compBuffer;
  201. #else
  202. fthrow(ImageException,"Not yet supportet without IPP.");
  203. #endif // NICE_USELIB_IPP
  204. return NULL;
  205. }
  206. template<class P>
  207. IppiConnectedComp* floodFill8Connected(ImageT<P>& src,
  208. const IppiPoint& seed,
  209. const P& newVal,
  210. IppiConnectedComp* compBuffer) {
  211. if(compBuffer==NULL)
  212. compBuffer = new IppiConnectedComp;
  213. #ifdef NICE_USELIB_IPP
  214. IppiSize roiSize = {src.width(), src.height()};
  215. Ipp32s buffSize;
  216. ippiFloodFillGetSize(roiSize, &buffSize);
  217. Ipp8u* floodBuffer = new Ipp8u[buffSize];
  218. IppStatus ret = ippiFloodFill_8Con_C1IR(src.getPixelPointer(),
  219. src.getStepsize(),
  220. roiSize, seed, newVal,
  221. compBuffer, floodBuffer);
  222. if(ret!=ippStsNoErr)
  223. fthrow(ImageException, ippGetStatusString(ret));
  224. // clean up
  225. delete floodBuffer;
  226. return compBuffer;
  227. #else
  228. fthrow(ImageException,"Not yet supportet without IPP.");
  229. #endif // NICE_USELIB_IPP
  230. return NULL;
  231. }
  232. template<class P>
  233. void Floodfill(ImageT<P>& src, int startx, int starty, uint _newVal, uint _minDelta, uint _maxDelta)
  234. {
  235. #ifdef NICE_USELIB_IPP
  236. int pBufferSize;
  237. IppStatus ret = ippiFloodFillGetSize_Grad(makeROIFullImage(src), &pBufferSize);
  238. if(ret!=ippStsNoErr)
  239. fthrow(ImageException, ippGetStatusString(ret));
  240. Ipp8u pBuffer[pBufferSize];
  241. IppiPoint seed = {startx, starty};
  242. IppiConnectedComp pRegion;
  243. ret = ippiFloodFill_Grad8Con_C1IR(src.getPixelPointer(), src.getStepsize(),
  244. makeROIFullImage(src), seed,
  245. static_cast<Ipp8u>(_newVal),
  246. static_cast<Ipp8u>(_minDelta),
  247. static_cast<Ipp8u>(_maxDelta),
  248. &pRegion, pBuffer);
  249. if(ret!=ippStsNoErr)
  250. fthrow(ImageException, ippGetStatusString(ret));
  251. #else // NICE_USELIB_IPP
  252. fthrow(ImageException,"Not yet supportet without IPP.");
  253. #endif // NICE_USELIB_IPP
  254. }
  255. template<class P>
  256. void Floodfill(ColorImageT<P>& src, int startx, int starty, uint _newVal, uint _minDelta, uint _maxDelta)
  257. {
  258. FloodfillRGB(src,startx,starty,_newVal,_newVal,_newVal,
  259. _minDelta,_minDelta,_minDelta,_maxDelta,_maxDelta,_maxDelta);
  260. }
  261. template<class P>
  262. void FloodfillRGB(ColorImageT<P>& src, int startx, int starty,
  263. uint _newValR, uint _newValG, uint _newValB,
  264. uint _minDeltaR, uint _minDeltaG, uint _minDeltaB,
  265. uint _maxDeltaR, uint _maxDeltaG, uint _maxDeltaB)
  266. {
  267. #ifdef NICE_USELIB_IPP
  268. int pBufferSize;
  269. IppStatus ret = ippiFloodFillGetSize_Grad(makeROIFullImage(src), &pBufferSize);
  270. if(ret!=ippStsNoErr)
  271. fthrow(ImageException, ippGetStatusString(ret));
  272. Ipp8u pBuffer[pBufferSize];
  273. IppiPoint seed = {startx, starty};
  274. IppiConnectedComp pRegion;
  275. Ipp8u newVal[3] = {_newValR,_newValG,_newValB};
  276. Ipp8u minDelta[3] = {_minDeltaR,_minDeltaG,_minDeltaB};
  277. Ipp8u maxDelta[3] = {_maxDeltaR,_maxDeltaG,_maxDeltaB};
  278. ret = ippiFloodFill_Grad8Con_C3IR(src.getPixelPointer(), src.getStepsize(),
  279. makeROIFullImage(src),
  280. seed, newVal, minDelta, maxDelta,
  281. &pRegion, pBuffer);
  282. if(ret!=ippStsNoErr)
  283. fthrow(ImageException, ippGetStatusString(ret));
  284. #else // NICE_USELIB_IPP
  285. fthrow(ImageException,"Not yet supportet without IPP.");
  286. #endif // NICE_USELIB_IPP
  287. }
  288. }