Convert.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  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_CONVERT_H
  7. #define LIMUN_CONVERT_H
  8. #include "core/image/ippwrapper.h"
  9. #include "core/vector/VectorT.h"
  10. #include "core/image/Buffer.h"
  11. #include "core/image/GrayColorImageCommonImplementation.h"
  12. #include "core/image/RectT.h"
  13. #include "core/image/ImageT.h"
  14. #include "core/image/ColorImageT.h"
  15. #include <math.h>
  16. #include <limits>
  17. #include <iostream>
  18. namespace NICE {
  19. template<class P> class ColorImageT;
  20. template<class P> class ImageT;
  21. /**
  22. * @name Colorspace Conversion
  23. * \{
  24. */
  25. /**
  26. * Convert the HSV-ColorImage \c src into a RGB-ColorImage \c dst .
  27. * @param src source HSV image
  28. * @param dst optional buffer to be used as target.<br>
  29. * Create a new ColorImage if \c dst == NULL.
  30. * @return Pointer to ColorImage
  31. * @throw ImageException will be thrown if \c dst != NULL and the size of \c src and \c dst is not equal.
  32. */
  33. ColorImage* hsvToRGB(const ColorImage& src, ColorImage* dst=NULL);
  34. /**
  35. * Convert the RGB-ColorImage \c src into a HSV-ColorImage \c dst .
  36. * @param src source RGB image
  37. * @param dst optional buffer to be used as target.<br>
  38. * Create a new ColorImage if \c dst == NULL.
  39. * @return Pointer to ColorImage
  40. * @throw ImageException will be thrown if \c dst != NULL and the size of \c src and \c dst is not equal.
  41. */
  42. ColorImage* rgbToHSV(const ColorImage& src, ColorImage* dst=NULL);
  43. /**
  44. * Convert the YUV-ColorImage \c src into a RGB-ColorImage \c dst .
  45. * @param src source YUV image
  46. * @param dst optional buffer to be used as target.<br>
  47. * Create a new ColorImage if \c dst == NULL.
  48. * @return Pointer to ColorImage
  49. * @throw ImageException will be thrown if \c dst != NULL and the size of \c dst and \c src is not equal.
  50. */
  51. ColorImage* yuvToRGB(const ColorImage& src, ColorImage* dst=NULL);
  52. /**
  53. * Convert the RGB-ColorImage \c src into a YUV-ColorImage \c dst .
  54. * @param src source RGB image
  55. * @param dst optional buffer to be used as target.<br>
  56. * Create a new ColorImage if \c dst == NULL.
  57. * @return Pointer to ColorImage
  58. * @throw ImageException will be thrown if \c dst != NULL and the size of \c dst and \c src is not equal.
  59. */
  60. ColorImage* rgbToYUV(const ColorImage& src, ColorImage* dst=NULL);
  61. /**
  62. * \}
  63. * @name Gray-RGB Conversion
  64. * \{
  65. */
  66. /**
  67. * Create a lookup table of size 256x3 for faster rgbToGray conversion.
  68. * @return Pointer to ImageT<int>
  69. *
  70. */
  71. ImageT<int>* rgbToGrayLUT();
  72. /**
  73. * Convert the RGB-ColorImage \c src into a Image \c dst .
  74. * @param src source RGB image
  75. * @param dst optional buffer to be used as target.<br>
  76. * Create a new ColorImage if \c dst == NULL.
  77. * @param rgbToGrayLUT optional lookup table (only if no Ipp is used and P = Ipp8u)
  78. * @return Pointer to Image
  79. * @throw ImageException will be thrown if \c dst != NULL and the size of \c src and \c dst is not equal.
  80. */
  81. template<class P>
  82. ImageT<P>* rgbToGray(const ColorImageT<P>& src, ImageT<P>* dst=NULL,
  83. const ImageT<int>* rgbToGrayLUT=NULL);
  84. /**
  85. * Convert the Image \c src into a RGB-ColorImage \c dst .
  86. * @param src source gray image
  87. * @param dst optional buffer to be used as target.<br>
  88. * Create a new gray image if \c dst == NULL.
  89. * @return Pointer to gray image
  90. * @throw ImageException will be thrown if \c dst != NULL and the size of \c src and \c dst is not equal.
  91. */
  92. template<class P>
  93. ColorImageT<P>* grayToRGB(const ImageT<P>& src, ColorImageT<P>* dst=NULL);
  94. /**
  95. * convert a gray value to RGB pseudo colors (gray value must be between 0 and 1)
  96. */
  97. void convertToPseudoColor ( double x, double & r, double & g, double & b );
  98. /**
  99. * Convert the Image \c src into a RGB-ColorImage \c dst using pseudo colors.
  100. * @param src source gray image
  101. * @param dst result image
  102. */
  103. template<class P>
  104. void imageToPseudoColor ( const NICE::ImageT<P> & src, NICE::ColorImage & dst );
  105. /**
  106. * Convert the matrix \c src into a RGB-ColorImage \c dst using pseudo colors.
  107. * @param src source gray image
  108. * @param dst result image
  109. */
  110. template<class P>
  111. void matrixToPseudoColor ( const NICE::MatrixT<P> & m, NICE::ColorImage & img );
  112. /**
  113. * Convert a signed image into a colored image
  114. * (white positiv values, red negativ values).
  115. * @note usefull to display float or signed images
  116. * @param src source gray image
  117. * @param dst optional buffer to be used as target.<br>
  118. * Create a new ColorImage if \c dst == NULL.
  119. * @return Pointer to ColorImage
  120. */
  121. template<class P>
  122. ColorImage* signedImageToRGB(const ImageT<P>& src, ColorImageT<Ipp8u>* dst=NULL);
  123. /**
  124. * \}
  125. * @name Gray-Float Conversion
  126. * \{
  127. */
  128. /**
  129. * Convert the gray image \c src into a float image \c dst .
  130. * @param src source gray image.
  131. * @param dst optional buffer to be used as target.<br>
  132. * Create a new FloatImage if \c dst == NULL.
  133. * @return Pointer to FloatImage
  134. * @throw ImageException will be thrown if \c dst != NULL and the size of \c src and \c dst is not equal.
  135. */
  136. template<class P>
  137. FloatImage* grayToFloat(const ImageT<P>& src, FloatImage* dst=NULL);
  138. /**
  139. * Convert the float image \c src into a gray image \c dst just rounded (not scaled).
  140. * @param src source float image
  141. * @param dst optional buffer to be used as target.<br>
  142. * Create a new ColorImage if \c dst == NULL.
  143. * @param roundMode rounding mode for floating point conversion
  144. * - ippRndZero : floating-point value will be truncated toward zero
  145. * - ippRndNear : floating-point value will be rounded to the nearest integer
  146. * @return Pointer to Image
  147. * @throw ImageException will be thrown if \c dst != NULL and the size of \c src and \c dst is not equal.
  148. */
  149. template<class P>
  150. ImageT<P>* floatToGray(const FloatImage& src, ImageT<P>* dst=NULL,
  151. IppRoundMode roundMode=ippRndNear);
  152. /**
  153. * Convert the float image \c src into a gray image \c dst by scaling with p' = 255/(fmax-fmin)*(p-fmin).
  154. * @param src source float image
  155. * @param dst optional buffer to be used as target.<br>
  156. * Create a new ColorImage if \c dst == NULL.
  157. * @return Pointer to Image
  158. * @throw ImageException will be thrown if \c dst != NULL and the size of \c src and \c dst is not equal.
  159. */
  160. Image* floatToGrayScaled(const FloatImage& src, Image* dst=NULL);
  161. /**
  162. * Convert the float image \c src into a gray image \c dst by scaling with p' = 255/(\c fmax-\c fmin)*(p-\c fmin) .
  163. * @param src source float image
  164. * @param fmin minimum float value
  165. * @param fmax maximum float value
  166. * @param dst optional buffer to be used as target.<br>
  167. * Create a new ColorImage if \c dst == NULL.
  168. * @return Pointer to Image
  169. * @throw ImageException will be thrown if \c dst != NULL and the size of \c src and \c dst is not equal.
  170. */
  171. Image* floatToGrayScaled(const FloatImage& src, Ipp32f fmin, Ipp32f fmax, Image* dst=NULL);
  172. /**
  173. * \}
  174. * @name RGB-Float Conversion
  175. * \{
  176. */
  177. /**
  178. * Convert the RGB-ColorImage \c src into a FloatImage \c dst containing all rgb values.<br>
  179. * Result image is 3 times wider.
  180. * @param src source RGB image
  181. * @param dst optional buffer to be used as target.<br>
  182. * Create a new FloatImage if \c dst == NULL.
  183. * @return Pointer to FloatImage
  184. * @throw ImageException will be thrown if \c dst != NULL and the size of \c src and \c dst is not equal.
  185. */
  186. FloatImage* rgbToFloat(const ColorImage& src, FloatImage* dst = NULL);
  187. /**
  188. * \}
  189. * @name BitDepth Conversion
  190. * \{
  191. */
  192. /**
  193. * @example image_convert.cpp
  194. */
  195. /**
  196. * Convert the gray image \c src to a gray image \c dst with a different bitdepth.
  197. * @note Ipp only supports the following conversions:
  198. * - 8u16u, 8u16s, 8u32s, 8s32s, 16u32s
  199. * - 8u32f, 8s32f, 16u32f, 16s32f
  200. * - 16u8u, 16s8u, 32s8u, 32s8s
  201. * - 32f8u, 32f8s, 32f16u, 32f16s
  202. * @param src source gray image
  203. * @param dst optional buffer to be used as target.<br>
  204. * Create a new Image if \c dst == NULL.
  205. * @return Pointer to Image
  206. * @throw ImageException will be thrown if \c dst != NULL and the size of \c src and \c dst is not equal.
  207. */
  208. template<class P1, class P2>
  209. ImageT<P2>* convertBitDepth(const ImageT<P1>& src, ImageT<P2>* dst);
  210. /**
  211. * Convert the color image \c src to a color image \c dst with a different bitdepth.
  212. * @note Ipp only supports the following conversions:
  213. * - 8u16u, 8u16s, 8u32s, 8s32s, 16u32s
  214. * - 8u32f, 8s32f, 16u32f, 16s32f
  215. * - 16u8u, 16s8u, 32s8u, 32s8s
  216. * - 32f8u, 32f8s, 32f16u, 32f16s
  217. * @param src source color image
  218. * @param dst optional buffer to be used as target.<br>
  219. * Create a new ColorImage if \c dst == NULL.
  220. * @return Pointer to ColorImage
  221. * @throw ImageException will be thrown if \c dst != NULL and the size of \c src and \c dst is not equal.
  222. */
  223. template<class P1, class P2>
  224. ColorImageT<P2>* convertBitDepth(const ColorImageT<P1>& src, ColorImageT<P2>* dst);
  225. /**
  226. * \}
  227. * @name Interpolation
  228. * @note This functions are only available with ipp support.
  229. * \{
  230. */
  231. /**
  232. * Remap Image src into the Image dst using lookup tables.
  233. * @param src source gray image
  234. * @param dst destination gray image
  235. * @param px 2D float image containing new x coordinate
  236. * @param py 2D float image containing new y coordinate
  237. * @param interpolation interpolation type (IPPI_INTER_NN|IPPI_INTER_LINEAR|IPPI_INTER_CUBIC)
  238. * nearest neighbor interpolation, linear interpolation or cubic interpolation
  239. * @return Pointer to Image
  240. */
  241. Image* remap(const Image& src, const FloatImage &px, const FloatImage &py,
  242. Image* dst = NULL, int interpolation=IPPI_INTER_LINEAR);
  243. /**
  244. * Remap ColorImage src into the ColorImage dst using lookup tables.
  245. * @param src source RGB image
  246. * @param dst destination RGB image
  247. * @param px 2D float image containing new x coordinate
  248. * @param py 2D float image containing new y coordinate
  249. * @param interpolation interpolation type (IPPI_INTER_NN|IPPI_INTER_LINEAR|IPPI_INTER_CUBIC)
  250. * nearest neighbor interpolation, linear interpolation or cubic interpolation
  251. * @return Pointer to ColorImage
  252. */
  253. ColorImage* remap(const ColorImage& src, const FloatImage &px, const FloatImage &py,
  254. ColorImage* dst = NULL, int interpolation=IPPI_INTER_LINEAR);
  255. /**
  256. * Scale ImageT src into the ImageT dst using scale factors xFactor and yFactor.
  257. * @param src source ImageT
  258. * @param dst destination ImageT
  259. * @param xFactor factor for scaling the x-axis if 0 it is caculated by src vs. dst
  260. * @param yFactor factor for scaling the y-axis if 0 it is caculated by src vs. dst
  261. * @param interpolation interpolation type
  262. * (IPPI_INTER_NN|IPPI_INTER_LINEAR|IPPI_INTER_CUBIC)
  263. * nearest neighbor interpolation, linear interpolation
  264. * or cubic interpolation
  265. * @return Pointer to ImageT<P>
  266. */
  267. template<class P>
  268. ImageT<P>* scale(const ImageT<P>& src, ImageT<P>* dst,
  269. double xFactor=0.0, double yFactor=0.0,
  270. int interpolation=IPPI_INTER_LINEAR);
  271. //! see scale()
  272. template<class P>
  273. inline ImageT<P>* resize(const ImageT<P>& src, ImageT<P>* dst,
  274. double xFactor=0.0, double yFactor=0.0,
  275. int interpolation=IPPI_INTER_LINEAR) {
  276. return scale(src, dst, xFactor, yFactor, interpolation);
  277. }
  278. /**
  279. * Scale ColorImageT src into the ColorImageT dst using scale factors xFactor and yFactor.
  280. * @param src source ColorImageT
  281. * @param dst destination ColorImageT
  282. * @param xFactor factor for scaling the x-axis if 0 it is caculated by src vs. dst
  283. * @param yFactor factor for scaling the y-axis if 0 it is caculated by src vs. dst
  284. * @param interpolation interpolation type (IPPI_INTER_NN|IPPI_INTER_LINEAR|IPPI_INTER_CUBIC)
  285. * nearest neighbor interpolation, linear interpolation or cubic interpolation
  286. * @return Pointer to ColorImageT<P>
  287. */
  288. template<class P>
  289. ColorImageT<P>* scale(const ColorImageT<P>& src, ColorImageT<P>* dst,
  290. double xFactor=0.0, double yFactor=0.0,
  291. int interpolation=IPPI_INTER_LINEAR);
  292. //! see scale()
  293. template<class P>
  294. inline ColorImageT<P>* resize(const ColorImageT<P>& src, ColorImageT<P>* dst,
  295. double xFactor=0.0, double yFactor=0.0,
  296. int interpolation=IPPI_INTER_LINEAR) {
  297. return scale(src, dst, xFactor, yFactor, interpolation);
  298. }
  299. /**
  300. * @name Some Helper Functions
  301. * \{
  302. */
  303. /**
  304. * Clip a \c rect within \c image.
  305. * @param image The image
  306. * @param rect The rectangle
  307. * @return \c Rect
  308. */
  309. Rect clipRect(const MultiChannelImageAccess& image, const Rect& rect);
  310. /**
  311. * Make an \c IppiRect for the full image.
  312. * @param image The image
  313. * @return \c IppiRect
  314. */
  315. IppiRect makeRectFullImage(const MultiChannelImageAccess& image);
  316. /**
  317. * Make an \c IppiSize for the full image.
  318. * @param image The image
  319. * @return \c IppiSize
  320. */
  321. IppiSize makeROIFullImage(const MultiChannelImageAccess& image);
  322. /**
  323. * Get an \c IppiSize for \c rect clipped within \c image.
  324. * @param image The image
  325. * @param rect The rectangle
  326. * @return \c IppiSize
  327. */
  328. IppiSize makeROIRect(const MultiChannelImageAccess& image, const Rect& rect);
  329. /**
  330. * Work around a certain IPP bug (?).
  331. * @param ippiSize input and output
  332. * @param clippedROI input and output
  333. */
  334. void preventIppBug(IppiSize& ippiSize, Rect& clippedROI);
  335. } // namespace
  336. //#ifdef __GNUC__
  337. #include <core/image/Convert.tcc>
  338. //#endif
  339. #endif // LIMUN_CONVERT_H