ImageTools.h 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425
  1. /*
  2. * NICE-Core - efficient algebra and computer vision methods
  3. * - libimage - An image library
  4. * See file License for license information.
  5. */
  6. #ifndef LIMUN_IMAGETOOLS_H
  7. #define LIMUN_IMAGETOOLS_H
  8. #include "core/image/ippwrapper.h"
  9. #include "core/vector/VectorT.h"
  10. #include "core/vector/MatrixT.h"
  11. #include "core/image/ImageT.h"
  12. #include "core/image/ColorImageT.h"
  13. #include "core/image/Convert.h"
  14. #include "core/image/FilterT.h"
  15. namespace NICE {
  16. /**
  17. * @name Arithmetic and Logical Operations
  18. * \{
  19. */
  20. /**
  21. * Calculates the absolute difference between Image \c src0 and Image \c src1 into the Image \c dst .
  22. * @param src0 source gray image
  23. * @param src1 source gray image
  24. * @param dst optional buffer to be used as target.<br>
  25. * Create a new Image if \c dst == NULL.<br>
  26. * If \c dst != NULL then size must be equal to \c src 's size!
  27. * @throw ImageException will be thrown if \c dst != NULL and the size of \c src0 / \c scr1 and \c dst or
  28. * the size of \c src0 and \c src1 is not equal.
  29. * @return Image
  30. */
  31. Image* absDiff(const Image& src0, const Image& src1, Image* dst=NULL);
  32. /**
  33. * Calculates the absolute difference between ColorImage \c src0 and ColorImage \c src1 into the ColorImage \c dst .
  34. * @param src0 source color image
  35. * @param src1 source color image
  36. * @param dst optional buffer to be used as target.<br>
  37. * Create a new ColorImage if \c dst == NULL.<br>
  38. * If \c dst != NULL then size must be equal to \c src' s size!
  39. * @throw ImageException will be thrown if \c dst != NULL and the size of \c src0 / \c scr1 and \c dst or
  40. * the size of \c src0 and \c src1 is not equal.
  41. * @return ColorImage
  42. */
  43. ColorImage* absDiff(const ColorImage& src0, const ColorImage& src1, ColorImage* dst=NULL);
  44. /**
  45. * Calculates bitwise and between the corresponding pixels of the Image \c src0 and Image
  46. * \c src1 into the Image \c dst.
  47. * @param src0 source gray image
  48. * @param src1 source gray image
  49. * @param dst optional buffer to be used as target.<br>
  50. * Create a new Image if \c dst == NULL.<br>
  51. * If \c dst != NULL then size must be equal to \c src 's size!
  52. * @throw ImageException will be thrown if \c dst != NULL and the size of \c src0 / \c scr1 and \c dst or
  53. * the size of \c src0 and \c src1 is not equal.
  54. * @return Image
  55. */
  56. Image* And(const Image& src0, const Image& src1, Image* dst=NULL);
  57. /**
  58. * Calculates bitwise and between the corresponding pixels of the ColorImage \c src0 and
  59. * ColorImage \c src1 into the ColorImage \c dst.
  60. * @param src0 source color image
  61. * @param src1 source color image
  62. * @param dst optional buffer to be used as target.<br>
  63. * Create a new Image if \c dst == NULL.<br>
  64. * If \c dst != NULL then size must be equal to \c src size!
  65. * @throw ImageException will be thrown if \c dst != NULL and the size of \c src0 / \c scr1 and \c dst or
  66. * the size of \c src0 and \c src1 is not equal.
  67. * @return ColorImage
  68. */
  69. ColorImage* And(const ColorImage& src0, const ColorImage& src1, ColorImage* dst=NULL);
  70. /**
  71. * \}
  72. * @name Border Functions
  73. * \{
  74. */
  75. /** Add Border with a constant value.
  76. * @param src source gray image
  77. * @param width width of the left and right border
  78. * @param height height of the top and bottom border
  79. * @param value border color
  80. * @param dst destination image
  81. */
  82. template<class P>
  83. ImageT<P>* addConstBorder(const ImageT<P> &src, uint width, uint height,
  84. P value=0, ImageT<P>* dst = NULL);
  85. /** Copy the border of an image.
  86. * @param src source gray image
  87. * @param width width of the left and right border
  88. * @param height height of the top and bottom border
  89. * @param dst destination image
  90. */
  91. template<class P>
  92. ImageT<P>* copyBorder(const ImageT<P> &src, uint width, uint height, ImageT<P>* dst);
  93. /**
  94. * \}
  95. * @name Threshold Functions
  96. * \{
  97. */
  98. /**
  99. * Generate a binarized image from Image \c src .
  100. * @param src source gray image
  101. * @param threshold threshold
  102. * @param dst optional buffer to be used as target.<br>
  103. * Create a new Image if \c dst == NULL.<br>
  104. * If \c dst != NULL then size must be equal to \c src 's size!
  105. * @return Image
  106. * @throw ImageException will be thrown if \c dst != NULL and the size of \c src and \c dst is not equal.
  107. */
  108. Image* threshold(const Image& src, const int threshold, Image* dst=NULL);
  109. /**
  110. * Generate a threshold image from Image \c src .
  111. * @param src source Image
  112. * @param threshold threshold
  113. * @param dst optional buffer to be used as target.<br>
  114. * Create a new Image if \c dst == NULL.<br>
  115. * If \c dst != NULL then size must be equal to \c src 's size!
  116. * @param value set pixel less or equal to \c threshold to this value
  117. * @return Image
  118. * @throw ImageException will be thrown if \c dst != NULL and the size of \c src and \c dst is not equal.
  119. */
  120. Image* lowerThreshold(const Image& src, const int threshold,
  121. Image* dst=NULL, const int value=0);
  122. /**
  123. * Generate a threshold image from Image \c src .
  124. * @param src source Image
  125. * @param threshold threshold
  126. * @param dst optional buffer to be used as target.<br>
  127. * Create a new Image if \c dst == NULL.<br>
  128. * If \c dst != NULL then size must be equal to \c src 's size!
  129. * @param value set pixel greater than threshold to this value
  130. * @return Image
  131. * @throw ImageException will be thrown if \c dst != NULL and the size of \c src and \c dst is not equal.
  132. */
  133. Image* upperThreshold(Image& src, const int threshold,
  134. Image* dst=NULL, const int value=255);
  135. /**
  136. * Generate a binarized image from Image \c src .
  137. * @param src source gray image
  138. * @param threshold threshold
  139. */
  140. void thresholdIP(Image& src, const int threshold);
  141. /**
  142. * Generate a threshold image from Image \c src .
  143. * @param src source gray image
  144. * @param threshold threshold
  145. * @param value set pixel less or equal to \c threshold to this value
  146. */
  147. void lowerThresholdIP(Image& src, const int threshold, const int value=0);
  148. /**
  149. * Generate a threshold image from Image \c src .
  150. * @param src source gray image
  151. * @param threshold threshold
  152. * @param value set pixel greater than \c threshold to this value
  153. */
  154. void upperThresholdIP(Image& src, const int threshold, const int value=255);
  155. /**
  156. * \}
  157. * @name Minimum / Maximum Functions
  158. * \{
  159. */
  160. /**
  161. * autoCropRect determine the rectangular area \c Rect within the pixels have larger values than a given value.
  162. * @param src source image
  163. * @param color color value
  164. * @param rect returned \c Rect of the area which has larger values than the color value
  165. **/
  166. template<class P>
  167. void autoCropRect(const ImageT<P> &src, P color, Rect &rect);
  168. /**
  169. * scale and shift image values as a new image with a destined range [min,max]
  170. * @param src input image
  171. * @param dst output image
  172. * @param min target range minimum
  173. * @param max target range maximum
  174. */
  175. template<class P>
  176. void normalizeToRange(const ImageT<P> &src, ImageT<P> &dst , P min, P max);
  177. /**
  178. * scale and shift image values to destined range [min,max] (inplace)
  179. * @param src image to modify
  180. * @param min target range minimum
  181. * @param max target range maximum
  182. */
  183. template<class P>
  184. void normalizeToRange(ImageT<P> &src, P min, P max);
  185. /**
  186. * findLocalMinima find minima in images.
  187. * @param src source image
  188. * @param thresh minima must be below this threshold
  189. * @param dist local minima have a displacement of rectangular distance to other minima
  190. * @param minima vector of coordiantes of minima
  191. **/
  192. template<class P>
  193. void findLocalMinima(const ImageT<P> &src, P thresh, int dist, std::vector<Coord> &minima);
  194. /**
  195. * findLocalMaxima find maxima in images.
  196. * @param src source image
  197. * @param thresh maxima must be below this threshold
  198. * @param dist local maxima have a displacement of rectangular distance to other minima
  199. * @param maxima vector of coordiantes of maxima
  200. **/
  201. template<class P>
  202. void findLocalMaxima(const ImageT<P> &src, P thresh, int dist, std::vector<Coord> &maxima);
  203. /**
  204. * \}
  205. * @name Line Segmentation
  206. * \{
  207. */
  208. /**
  209. * Create a lookup table to speed up the Hough Transformation
  210. * @return Pointer to gray image
  211. */
  212. FloatImage* houghTransformLUT();
  213. /**
  214. * Hough Transformation for a given gradient strength and degree image
  215. * @param gradStr gradient strength image
  216. * @param gradDir gradient direction image
  217. * @param noLines return the \c noLines strongest Lines as result
  218. * @param gradThresh pixels with gradient strenth lower \c gradThresh will be ignored
  219. * @param soAreaDeg degree area which will be cleared while getting best \c nolines lines
  220. * @param soAreaDist distance area which will be cleared while getting best \c nolines lines
  221. * @param soBorder ignore a border of size \c soBorder
  222. * @param lut optional lookup table, create with \b houghTransformLUT() to speed up the ht
  223. * @param diffAngle only angles from \c -diffAngle to \c +diffAngle will be accumulated
  224. * @throw ImageException will be thrown if the size of \c gradStr and \c gradDir is not equal.
  225. * @return IntMatrix of size (\c noLines x 3) or
  226. * (lines found x 3) if lesser lines than \c noLines can be found.
  227. * - 1.col: distance
  228. * - 2.col: angle in degree
  229. * - 3.col: accumulator value
  230. */
  231. IntMatrix* houghTransform(const Image& gradStr,
  232. const Image& gradDir,
  233. const uint& noLines,
  234. const uint& gradThresh,
  235. const uint& soAreaDeg,
  236. const uint& soAreaDist,
  237. const uint& soBorder = 0,
  238. const FloatImage* lut=NULL,
  239. const int diffAngle = 22);
  240. /**
  241. * Hough Transformation for a given gradient strength image
  242. * @param gradStr gradient strength image
  243. * @param noLines return the \c noLines strongest Lines as result
  244. * @param gradThresh pixels with gradient strenth lower \c gradThresh will be ignored
  245. * @param soAreaDeg degree area which will be cleared while getting the stronges \c nolines lines
  246. * @param soAreaDist distance area which will be cleared while getting the strongest \c nolines lines
  247. * @param soBorder ignore a border of size \c soBorder
  248. * @param lut optional lookup table, create with \b houghTransformLUT() to speed up the ht
  249. * @throw ImageException will be thrown if the size of \c gradStr and \c gradDir is not equal.
  250. * @return IntMatrix of size (\c noLines x 3) or
  251. * (lines found x 3) if lesser lines than \c noLines can be found.
  252. * - 1.col: distance
  253. * - 2.col: angle in degree
  254. * - 3.col: accumulator value
  255. */
  256. IntMatrix* houghTransform(const Image& gradStr,
  257. const uint& noLines,
  258. const uint& gradThresh,
  259. const uint& soAreaDeg,
  260. const uint& soAreaDist,
  261. const uint& soBorder = 0,
  262. const FloatImage* lut=NULL);
  263. /**
  264. * \}
  265. * @name Corner Detection
  266. * \{
  267. */
  268. /**
  269. * Kanade-Lucas-Tomasi corner detection
  270. * @param src source gray image
  271. * @param noCorner return the \c noCorner strongest corners as result
  272. * @param EVThresh only corners where the smaller eigenvalue is bigger than \c EVThresh will be processed
  273. * @param soNeighborhood only neighbors of a pixel with distance lower or equal to
  274. \c soNeighborhood are taken into account during the calculation of the structure matrix
  275. * @param soArea all pixels around a detected corner with distance smaller or equal to
  276. * \c soArea will be deleted
  277. * @param gradX optional horizontal gradient image
  278. * @param gradY optional vertical gradient image
  279. * @throw ImageException will be thrown, if \c gradX != NULL or \c GradY != NULL and the
  280. * size of \c gradX and \c gradY or \c gradX / \c gradY and \c src is not equal.
  281. * @return Matrix
  282. */
  283. Matrix* KLTCornerDetector(const Image& src,
  284. const uint& noCorner,
  285. const double& EVThresh,
  286. const uint& soNeighborhood,
  287. const uint& soArea,
  288. const GrayImage16s* gradX=NULL,
  289. const GrayImage16s* gradY=NULL);
  290. /**
  291. * \}
  292. * @name Floodfill Functions
  293. * \{
  294. */
  295. /**
  296. * Floodfill algorithm for GrayImages
  297. * fill area with the color \c newVal beginning with the starting point \c seed
  298. * using a 4-neighborhood
  299. * @param src source gray image
  300. * @param seed start point
  301. * @param newVal value to fill component with
  302. * @param compBuffer optional buffer to be used as target.<br>
  303. * create a new IppiConnectedComp if \c compBuffer == NULL.<br>
  304. * @return pointer to IppiConnectedComp
  305. */
  306. template<class P>
  307. IppiConnectedComp* floodFill4Connected(ImageT<P>& src,
  308. const IppiPoint& seed,
  309. const P& newVal,
  310. IppiConnectedComp* compBuffer = NULL);
  311. /**
  312. * Floodfill algorithm for GrayImages
  313. * fill area with the color \c newVal beginning with the starting point \c seed
  314. * using a 4-neighborhood
  315. * @param src source gray image
  316. * @param seed start point
  317. * @param newVal value to fill component with
  318. * @param compBuffer optional buffer to be used as target.<br>
  319. * create a new IppiConnectedComp if \c compBuffer == NULL.<br>
  320. * @return pointer to IppiConnectedComp
  321. */
  322. template<class P>
  323. IppiConnectedComp* floodFill8Connected(ImageT<P>& src,
  324. const IppiPoint& seed,
  325. const P& newVal,
  326. IppiConnectedComp* compBuffer = NULL);
  327. /**
  328. * Floodfill algorithm for GrayImages
  329. * @param src source image
  330. * @param startx start x coordinate for floodfill
  331. * @param starty start y coordinate for floodfill
  332. * @param newVal new value in filled area
  333. * @param minDelta minimum Delta for neighbourpixel from actual pixelvalue
  334. * @param maxDelta maximum Delta for neighbourpixel from actual pixelvalue
  335. */
  336. template<class P>
  337. void Floodfill(ImageT<P>& src, int startx=0, int starty=0,
  338. uint newVal=0, uint minDelta=50, uint maxDelta=50);
  339. /**
  340. * Floodfill algorithm for ColorImages treating all channels the same
  341. * @param src source image
  342. * @param startx start x coordinate for floodfill
  343. * @param starty start y coordinate for floodfill
  344. * @param newVal new value in filled area
  345. * @param minDelta minimum Delta for neighbourpixel from actual pixelvalue
  346. * @param maxDelta maximum Delta for neighbourpixel from actual pixelvalue
  347. */
  348. template<class P>
  349. void Floodfill(ColorImageT<P>& src, int startx=0, int starty=0,
  350. uint newVal=0, uint minDelta=50, uint maxDelta=50);
  351. /**
  352. * Floodfill algorithm for ColorImages
  353. * @param src source image
  354. * @param startx start x coordinate for floodfill
  355. * @param starty start y coordinate for floodfill
  356. * @param newValR new value in filled area for R channel
  357. * @param newValG new value in filled area for G channel
  358. * @param newValB new value in filled area for B channel
  359. * @param minDeltaR minimum Delta for neighbourpixel from actual pixelvalue for R channel
  360. * @param minDeltaG minimum Delta for neighbourpixel from actual pixelvalue for G channel
  361. * @param minDeltaB minimum Delta for neighbourpixel from actual pixelvalue for B channel
  362. * @param maxDeltaR maximum Delta for neighbourpixel from actual pixelvalue for R channel
  363. * @param maxDeltaG maximum Delta for neighbourpixel from actual pixelvalue for G channel
  364. * @param maxDeltaB maximum Delta for neighbourpixel from actual pixelvalue for B channel
  365. */
  366. template<class P>
  367. void FloodfillRGB(ColorImageT<P>& src, int startx=0, int starty=0,
  368. uint newValR=0, uint newValG=0, uint newValB=0,
  369. uint minDeltaR=50, uint minDeltaG=50, uint minDeltaB=50,
  370. uint maxDeltaR=50, uint maxDeltaG=50, uint maxDeltaB=50);
  371. } // namespace
  372. #include "core/image/ImageT.h"
  373. #include "core/image/ImageT.h"
  374. #include "core/image/ColorImageT.h"
  375. #include "core/image/Buffer.h"
  376. //#ifdef __GNUC__
  377. #include <core/image/ImageTools.tcc>
  378. //#endif
  379. #endif //LIMUN_IMAGETOOLS_H