Convert.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  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 Image \c src into a RGB-ColorImage \c dst using pseudo colors.
  107. * @param src source gray image
  108. * @param dst result image
  109. * @param _min desired lower bound (smaller initial values will be set to zero after conversion)
  110. * @param _max desired upper bound (larger initial values will be set to 255 after conversion)
  111. * @author Alexander Freytag
  112. * @date 17-04-2013 (dd-mm-yyyy)
  113. */
  114. template<class P>
  115. void imageToPseudoColorWithRangeSpecification ( const NICE::ImageT<P> & src, NICE::ColorImage & img, const double & _min, const double & _max );
  116. /**
  117. * Convert the matrix \c src into a RGB-ColorImage \c dst using pseudo colors.
  118. * @param src source gray image
  119. * @param dst result image
  120. */
  121. template<class P>
  122. void matrixToPseudoColor ( const NICE::MatrixT<P> & m, NICE::ColorImage & img );
  123. /**
  124. * Convert a signed image into a colored image
  125. * (white positiv values, red negativ values).
  126. * @note usefull to display float or signed images
  127. * @param src source gray image
  128. * @param dst optional buffer to be used as target.<br>
  129. * Create a new ColorImage if \c dst == NULL.
  130. * @return Pointer to ColorImage
  131. */
  132. template<class P>
  133. ColorImage* signedImageToRGB(const ImageT<P>& src, ColorImageT<Ipp8u>* dst=NULL);
  134. /**
  135. * \}
  136. * @name Gray-Float Conversion
  137. * \{
  138. */
  139. /**
  140. * Convert the gray image \c src into a float image \c dst .
  141. * @param src source gray image.
  142. * @param dst optional buffer to be used as target.<br>
  143. * Create a new FloatImage if \c dst == NULL.
  144. * @return Pointer to FloatImage
  145. * @throw ImageException will be thrown if \c dst != NULL and the size of \c src and \c dst is not equal.
  146. */
  147. template<class P>
  148. FloatImage* grayToFloat(const ImageT<P>& src, FloatImage* dst=NULL);
  149. /**
  150. * Convert the float image \c src into a gray image \c dst just rounded (not scaled).
  151. * @param src source float image
  152. * @param dst optional buffer to be used as target.<br>
  153. * Create a new ColorImage if \c dst == NULL.
  154. * @param roundMode rounding mode for floating point conversion
  155. * - ippRndZero : floating-point value will be truncated toward zero
  156. * - ippRndNear : floating-point value will be rounded to the nearest integer
  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. template<class P>
  161. ImageT<P>* floatToGray(const FloatImage& src, ImageT<P>* dst=NULL,
  162. IppRoundMode roundMode=ippRndNear);
  163. /**
  164. * Convert the float image \c src into a gray image \c dst by scaling with p' = 255/(fmax-fmin)*(p-fmin).
  165. * @param src source float image
  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, Image* dst=NULL);
  172. /**
  173. * Convert the float image \c src into a gray image \c dst by scaling with p' = 255/(\c fmax-\c fmin)*(p-\c fmin) .
  174. * @param src source float image
  175. * @param fmin minimum float value
  176. * @param fmax maximum float value
  177. * @param dst optional buffer to be used as target.<br>
  178. * Create a new ColorImage if \c dst == NULL.
  179. * @return Pointer to Image
  180. * @throw ImageException will be thrown if \c dst != NULL and the size of \c src and \c dst is not equal.
  181. */
  182. Image* floatToGrayScaled(const FloatImage& src, Ipp32f fmin, Ipp32f fmax, Image* dst=NULL);
  183. /**
  184. * \}
  185. * @name RGB-Float Conversion
  186. * \{
  187. */
  188. /**
  189. * Convert the RGB-ColorImage \c src into a FloatImage \c dst containing all rgb values.<br>
  190. * Result image is 3 times wider.
  191. * @param src source RGB image
  192. * @param dst optional buffer to be used as target.<br>
  193. * Create a new FloatImage if \c dst == NULL.
  194. * @return Pointer to FloatImage
  195. * @throw ImageException will be thrown if \c dst != NULL and the size of \c src and \c dst is not equal.
  196. */
  197. FloatImage* rgbToFloat(const ColorImage& src, FloatImage* dst = NULL);
  198. /**
  199. * \}
  200. * @name BitDepth Conversion
  201. * \{
  202. */
  203. /**
  204. * @example image_convert.cpp
  205. */
  206. /**
  207. * Convert the gray image \c src to a gray image \c dst with a different bitdepth.
  208. * @note Ipp only supports the following conversions:
  209. * - 8u16u, 8u16s, 8u32s, 8s32s, 16u32s
  210. * - 8u32f, 8s32f, 16u32f, 16s32f
  211. * - 16u8u, 16s8u, 32s8u, 32s8s
  212. * - 32f8u, 32f8s, 32f16u, 32f16s
  213. * @param src source gray image
  214. * @param dst optional buffer to be used as target.<br>
  215. * Create a new Image if \c dst == NULL.
  216. * @return Pointer to Image
  217. * @throw ImageException will be thrown if \c dst != NULL and the size of \c src and \c dst is not equal.
  218. */
  219. template<class P1, class P2>
  220. ImageT<P2>* convertBitDepth(const ImageT<P1>& src, ImageT<P2>* dst);
  221. /**
  222. * Convert the color image \c src to a color image \c dst with a different bitdepth.
  223. * @note Ipp only supports the following conversions:
  224. * - 8u16u, 8u16s, 8u32s, 8s32s, 16u32s
  225. * - 8u32f, 8s32f, 16u32f, 16s32f
  226. * - 16u8u, 16s8u, 32s8u, 32s8s
  227. * - 32f8u, 32f8s, 32f16u, 32f16s
  228. * @param src source color image
  229. * @param dst optional buffer to be used as target.<br>
  230. * Create a new ColorImage if \c dst == NULL.
  231. * @return Pointer to ColorImage
  232. * @throw ImageException will be thrown if \c dst != NULL and the size of \c src and \c dst is not equal.
  233. */
  234. template<class P1, class P2>
  235. ColorImageT<P2>* convertBitDepth(const ColorImageT<P1>& src, ColorImageT<P2>* dst);
  236. /**
  237. * \}
  238. * @name Interpolation
  239. * @note This functions are only available with ipp support.
  240. * \{
  241. */
  242. /**
  243. * Remap Image src into the Image dst using lookup tables.
  244. * @param src source gray image
  245. * @param dst destination gray image
  246. * @param px 2D float image containing new x coordinate
  247. * @param py 2D float image containing new y coordinate
  248. * @param interpolation interpolation type (IPPI_INTER_NN|IPPI_INTER_LINEAR|IPPI_INTER_CUBIC)
  249. * nearest neighbor interpolation, linear interpolation or cubic interpolation
  250. * @return Pointer to Image
  251. */
  252. Image* remap(const Image& src, const FloatImage &px, const FloatImage &py,
  253. Image* dst = NULL, int interpolation=IPPI_INTER_LINEAR);
  254. /**
  255. * Remap ColorImage src into the ColorImage dst using lookup tables.
  256. * @param src source RGB image
  257. * @param dst destination RGB image
  258. * @param px 2D float image containing new x coordinate
  259. * @param py 2D float image containing new y coordinate
  260. * @param interpolation interpolation type (IPPI_INTER_NN|IPPI_INTER_LINEAR|IPPI_INTER_CUBIC)
  261. * nearest neighbor interpolation, linear interpolation or cubic interpolation
  262. * @return Pointer to ColorImage
  263. */
  264. ColorImage* remap(const ColorImage& src, const FloatImage &px, const FloatImage &py,
  265. ColorImage* dst = NULL, int interpolation=IPPI_INTER_LINEAR);
  266. /**
  267. * Scale ImageT src into the ImageT dst using scale factors xFactor and yFactor.
  268. * @param src source ImageT
  269. * @param dst destination ImageT
  270. * @param xFactor factor for scaling the x-axis if 0 it is caculated by src vs. dst
  271. * @param yFactor factor for scaling the y-axis if 0 it is caculated by src vs. dst
  272. * @param interpolation interpolation type
  273. * (IPPI_INTER_NN|IPPI_INTER_LINEAR|IPPI_INTER_CUBIC)
  274. * nearest neighbor interpolation, linear interpolation
  275. * or cubic interpolation
  276. * @return Pointer to ImageT<P>
  277. */
  278. template<class P>
  279. ImageT<P>* scale(const ImageT<P>& src, ImageT<P>* dst,
  280. double xFactor=0.0, double yFactor=0.0,
  281. int interpolation=IPPI_INTER_LINEAR);
  282. //! see scale()
  283. template<class P>
  284. inline ImageT<P>* resize(const ImageT<P>& src, ImageT<P>* dst,
  285. double xFactor=0.0, double yFactor=0.0,
  286. int interpolation=IPPI_INTER_LINEAR) {
  287. return scale(src, dst, xFactor, yFactor, interpolation);
  288. }
  289. /**
  290. * Scale ColorImageT src into the ColorImageT dst using scale factors xFactor and yFactor.
  291. * @param src source ColorImageT
  292. * @param dst destination ColorImageT
  293. * @param xFactor factor for scaling the x-axis if 0 it is caculated by src vs. dst
  294. * @param yFactor factor for scaling the y-axis if 0 it is caculated by src vs. dst
  295. * @param interpolation interpolation type (IPPI_INTER_NN|IPPI_INTER_LINEAR|IPPI_INTER_CUBIC)
  296. * nearest neighbor interpolation, linear interpolation or cubic interpolation
  297. * @return Pointer to ColorImageT<P>
  298. */
  299. template<class P>
  300. ColorImageT<P>* scale(const ColorImageT<P>& src, ColorImageT<P>* dst,
  301. double xFactor=0.0, double yFactor=0.0,
  302. int interpolation=IPPI_INTER_LINEAR);
  303. //! see scale()
  304. template<class P>
  305. inline ColorImageT<P>* resize(const ColorImageT<P>& src, ColorImageT<P>* dst,
  306. double xFactor=0.0, double yFactor=0.0,
  307. int interpolation=IPPI_INTER_LINEAR) {
  308. return scale(src, dst, xFactor, yFactor, interpolation);
  309. }
  310. /**
  311. * @name Some Helper Functions
  312. * \{
  313. */
  314. /**
  315. * Clip a \c rect within \c image.
  316. * @param image The image
  317. * @param rect The rectangle
  318. * @return \c Rect
  319. */
  320. Rect clipRect(const MultiChannelImageAccess& image, const Rect& rect);
  321. /**
  322. * Make an \c IppiRect for the full image.
  323. * @param image The image
  324. * @return \c IppiRect
  325. */
  326. IppiRect makeRectFullImage(const MultiChannelImageAccess& image);
  327. /**
  328. * Make an \c IppiSize for the full image.
  329. * @param image The image
  330. * @return \c IppiSize
  331. */
  332. IppiSize makeROIFullImage(const MultiChannelImageAccess& image);
  333. /**
  334. * Get an \c IppiSize for \c rect clipped within \c image.
  335. * @param image The image
  336. * @param rect The rectangle
  337. * @return \c IppiSize
  338. */
  339. IppiSize makeROIRect(const MultiChannelImageAccess& image, const Rect& rect);
  340. /**
  341. * Work around a certain IPP bug (?).
  342. * @param ippiSize input and output
  343. * @param clippedROI input and output
  344. */
  345. void preventIppBug(IppiSize& ippiSize, Rect& clippedROI);
  346. } // namespace
  347. //#ifdef __GNUC__
  348. #include <core/image/Convert.tcc>
  349. //#endif
  350. #endif // LIMUN_CONVERT_H