Visual Servoing Platform  version 3.5.1 under development (2023-03-14)
vpImageConvert.h
1 /****************************************************************************
2  *
3  * ViSP, open source Visual Servoing Platform software.
4  * Copyright (C) 2005 - 2021 by Inria. All rights reserved.
5  *
6  * This software is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  * See the file LICENSE.txt at the root directory of this source
11  * distribution for additional information about the GNU GPL.
12  *
13  * For using ViSP with software that can not be combined with the GNU
14  * GPL, please contact Inria about acquiring a ViSP Professional
15  * Edition License.
16  *
17  * See http://visp.inria.fr for more information.
18  *
19  * This software was developed at:
20  * Inria Rennes - Bretagne Atlantique
21  * Campus Universitaire de Beaulieu
22  * 35042 Rennes Cedex
23  * France
24  *
25  * If you have questions regarding the use of this file, please contact
26  * Inria at visp@inria.fr
27  *
28  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
29  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
30  *
31  * Description:
32  * Convert image types.
33  *
34  * Authors:
35  * Eric Marchand
36  * Fabien Spindler
37  * Anthony Saunier
38  *
39  *****************************************************************************/
40 
46 #ifndef vpIMAGECONVERT_H
47 #define vpIMAGECONVERT_H
48 
49 #include <stdint.h>
50 
51 // image
52 #include <visp3/core/vpConfig.h>
53 #include <visp3/core/vpDebug.h>
54 #include <visp3/core/vpImage.h>
55 // color
56 #include <visp3/core/vpRGBa.h>
57 
58 #ifdef VISP_HAVE_OPENCV
59 #if (VISP_HAVE_OPENCV_VERSION >= 0x040000) // Require opencv >= 4.0.0
60 #include <opencv2/highgui.hpp>
61 #include <opencv2/imgproc.hpp>
62 #include <opencv2/imgproc/types_c.h>
63 #elif (VISP_HAVE_OPENCV_VERSION >= 0x030000) // Require opencv >= 3.0.0
64 #include <opencv2/core/core.hpp>
65 #include <opencv2/highgui/highgui.hpp>
66 #include <opencv2/imgproc/imgproc.hpp>
67 #elif (VISP_HAVE_OPENCV_VERSION >= 0x020408) // Require opencv >= 2.4.8
68 #include <opencv2/core/core.hpp>
69 #include <opencv2/highgui/highgui.hpp>
70 #include <opencv2/imgproc/imgproc.hpp>
71 #elif (VISP_HAVE_OPENCV_VERSION >= 0x020101) // Require opencv >= 2.1.1
72 #include <opencv2/core/core.hpp>
73 #include <opencv2/highgui/highgui.hpp>
74 #include <opencv2/highgui/highgui_c.h>
75 #include <opencv2/legacy/legacy.hpp>
76 #else
77 #include <highgui.h>
78 #endif
79 #endif
80 
81 #ifdef VISP_HAVE_YARP
82 #include <yarp/sig/Image.h>
83 #endif
84 
85 #if defined(_WIN32)
86 // Include WinSock2.h before windows.h to ensure that winsock.h is not
87 // included by windows.h since winsock.h and winsock2.h are incompatible
88 #include <WinSock2.h>
89 #include <windows.h>
90 #endif
91 
105 class VISP_EXPORT vpImageConvert
106 {
107 
108 public:
109  static void createDepthHistogram(const vpImage<uint16_t> &src_depth, vpImage<vpRGBa> &dest_rgba);
110  static void createDepthHistogram(const vpImage<uint16_t> &src_depth, vpImage<unsigned char> &dest_depth);
111 
112  static void createDepthHistogram(const vpImage<float> &src_depth, vpImage<vpRGBa> &dest_depth);
113  static void createDepthHistogram(const vpImage<float> &src_depth, vpImage<unsigned char> &dest_depth);
114 
115  static void convert(const vpImage<unsigned char> &src, vpImage<vpRGBa> &dest);
116  static void convert(const vpImage<vpRGBa> &src, vpImage<unsigned char> &dest, unsigned int nThreads = 0);
117 
118  static void convert(const vpImage<float> &src, vpImage<unsigned char> &dest);
119  static void convert(const vpImage<unsigned char> &src, vpImage<float> &dest);
120 
121  static void convert(const vpImage<double> &src, vpImage<unsigned char> &dest);
122  static void convert(const vpImage<unsigned char> &src, vpImage<double> &dest);
123 
124  static void convert(const vpImage<uint16_t> &src, vpImage<unsigned char> &dest, unsigned char bitshift = 8);
125  static void convert(const vpImage<unsigned char> &src, vpImage<uint16_t> &dest, unsigned char bitshift = 8);
126 
132  template <typename Type> static void convert(const vpImage<Type> &src, vpImage<Type> &dest) { dest = src; }
133 
134 #ifdef VISP_HAVE_OPENCV
135  // Deprecated: will be removed with OpenCV transcient from C to C++ api
136  static void convert(const IplImage *src, vpImage<vpRGBa> &dest, bool flip = false);
137  static void convert(const IplImage *src, vpImage<unsigned char> &dest, bool flip = false);
138  static void convert(const vpImage<vpRGBa> &src, IplImage *&dest);
139  static void convert(const vpImage<unsigned char> &src, IplImage *&dest);
140 #if VISP_HAVE_OPENCV_VERSION >= 0x020100
141  static void convert(const cv::Mat &src, vpImage<vpRGBa> &dest, bool flip = false);
142  static void convert(const cv::Mat &src, vpImage<unsigned char> &dest, bool flip = false, unsigned int nThreads = 0);
143  static void convert(const vpImage<vpRGBa> &src, cv::Mat &dest);
144  static void convert(const vpImage<unsigned char> &src, cv::Mat &dest, bool copyData = true);
145 #endif
146 #endif
147 
148 #ifdef VISP_HAVE_YARP
149  static void convert(const vpImage<unsigned char> &src, yarp::sig::ImageOf<yarp::sig::PixelMono> *dest,
150  bool copyData = true);
151  static void convert(const yarp::sig::ImageOf<yarp::sig::PixelMono> *src, vpImage<unsigned char> &dest,
152  bool copyData = true);
153 
154  static void convert(const vpImage<vpRGBa> &src, yarp::sig::ImageOf<yarp::sig::PixelRgba> *dest, bool copyData = true);
155  static void convert(const yarp::sig::ImageOf<yarp::sig::PixelRgba> *src, vpImage<vpRGBa> &dest, bool copyData = true);
156 
157  static void convert(const vpImage<vpRGBa> &src, yarp::sig::ImageOf<yarp::sig::PixelRgb> *dest);
158  static void convert(const yarp::sig::ImageOf<yarp::sig::PixelRgb> *src, vpImage<vpRGBa> &dest);
159 #endif
160 
161  static void split(const vpImage<vpRGBa> &src, vpImage<unsigned char> *pR, vpImage<unsigned char> *pG,
163 
164  static void merge(const vpImage<unsigned char> *R, const vpImage<unsigned char> *G, const vpImage<unsigned char> *B,
165  const vpImage<unsigned char> *a, vpImage<vpRGBa> &RGBa);
166 
182  static inline void YUVToRGB(unsigned char y, unsigned char u, unsigned char v, unsigned char &r, unsigned char &g,
183  unsigned char &b)
184  {
185  double dr, dg, db;
186  dr = floor(0.9999695 * y - 0.0009508 * (u - 128) + 1.1359061 * (v - 128));
187  dg = floor(0.9999695 * y - 0.3959609 * (u - 128) - 0.5782955 * (v - 128));
188  db = floor(0.9999695 * y + 2.04112 * (u - 128) - 0.0016314 * (v - 128));
189 
190  dr = dr < 0. ? 0. : dr;
191  dg = dg < 0. ? 0. : dg;
192  db = db < 0. ? 0. : db;
193  dr = dr > 255. ? 255. : dr;
194  dg = dg > 255. ? 255. : dg;
195  db = db > 255. ? 255. : db;
196 
197  r = (unsigned char)dr;
198  g = (unsigned char)dg;
199  b = (unsigned char)db;
200  }
201  static void YUYVToRGBa(unsigned char *yuyv, unsigned char *rgba, unsigned int width, unsigned int height);
202  static void YUYVToRGB(unsigned char *yuyv, unsigned char *rgb, unsigned int width, unsigned int height);
203  static void YUYVToGrey(unsigned char *yuyv, unsigned char *grey, unsigned int size);
204  static void YUV411ToRGBa(unsigned char *yuv, unsigned char *rgba, unsigned int size);
205  static void YUV411ToRGB(unsigned char *yuv, unsigned char *rgb, unsigned int size);
206  static void YUV411ToGrey(unsigned char *yuv, unsigned char *grey, unsigned int size);
207  static void YUV422ToRGBa(unsigned char *yuv, unsigned char *rgba, unsigned int size);
208  static void YUV422ToRGB(unsigned char *yuv, unsigned char *rgb, unsigned int size);
209  static void YUV422ToGrey(unsigned char *yuv, unsigned char *grey, unsigned int size);
210  static void YUV420ToRGBa(unsigned char *yuv, unsigned char *rgba, unsigned int width, unsigned int height);
211  static void YUV420ToRGB(unsigned char *yuv, unsigned char *rgb, unsigned int width, unsigned int height);
212  static void YUV420ToGrey(unsigned char *yuv, unsigned char *grey, unsigned int size);
213 
214  static void YUV444ToRGBa(unsigned char *yuv, unsigned char *rgba, unsigned int size);
215  static void YUV444ToRGB(unsigned char *yuv, unsigned char *rgb, unsigned int size);
216  static void YUV444ToGrey(unsigned char *yuv, unsigned char *grey, unsigned int size);
217 
218  static void YV12ToRGBa(unsigned char *yuv, unsigned char *rgba, unsigned int width, unsigned int height);
219  static void YV12ToRGB(unsigned char *yuv, unsigned char *rgb, unsigned int width, unsigned int height);
220  static void YVU9ToRGBa(unsigned char *yuv, unsigned char *rgba, unsigned int width, unsigned int height);
221  static void YVU9ToRGB(unsigned char *yuv, unsigned char *rgb, unsigned int width, unsigned int height);
222  static void RGBToRGBa(unsigned char *rgb, unsigned char *rgba, unsigned int size);
223  static void RGBaToRGB(unsigned char *rgba, unsigned char *rgb, unsigned int size);
224 
225  static void RGBToGrey(unsigned char *rgb, unsigned char *grey, unsigned int width, unsigned int height,
226  bool flip = false);
227  static void RGBToGrey(unsigned char *rgb, unsigned char *grey, unsigned int size);
228  static void RGBaToGrey(unsigned char *rgba, unsigned char *grey, unsigned int width, unsigned int height,
229  unsigned int nThreads = 0);
230  static void RGBaToGrey(unsigned char *rgba, unsigned char *grey, unsigned int size);
231 
232  static void RGBToRGBa(unsigned char *rgb, unsigned char *rgba, unsigned int width, unsigned int height,
233  bool flip = false);
234 
235  static void GreyToRGBa(unsigned char *grey, unsigned char *rgba, unsigned int width, unsigned int height);
236  static void GreyToRGBa(unsigned char *grey, unsigned char *rgba, unsigned int size);
237  static void GreyToRGB(unsigned char *grey, unsigned char *rgb, unsigned int size);
238 
239  static void BGRToRGBa(unsigned char *bgr, unsigned char *rgba, unsigned int width, unsigned int height,
240  bool flip = false);
241 
242  static void BGRToGrey(unsigned char *bgr, unsigned char *grey, unsigned int width, unsigned int height,
243  bool flip = false, unsigned int nThreads = 0);
244 
245  static void BGRaToGrey(unsigned char *bgra, unsigned char *grey, unsigned int width, unsigned int height,
246  bool flip = false, unsigned int nThreads = 0);
247  static void BGRaToRGBa(unsigned char *bgra, unsigned char *rgba, unsigned int width, unsigned int height,
248  bool flip = false);
249 
250  static void YCbCrToRGB(unsigned char *ycbcr, unsigned char *rgb, unsigned int size);
251  static void YCbCrToRGBa(unsigned char *ycbcr, unsigned char *rgb, unsigned int size);
252  static void YCbCrToGrey(unsigned char *ycbcr, unsigned char *grey, unsigned int size);
253  static void YCrCbToRGB(unsigned char *ycrcb, unsigned char *rgb, unsigned int size);
254  static void YCrCbToRGBa(unsigned char *ycrcb, unsigned char *rgb, unsigned int size);
255  static void MONO16ToGrey(unsigned char *grey16, unsigned char *grey, unsigned int size);
256  static void MONO16ToRGBa(unsigned char *grey16, unsigned char *rgba, unsigned int size);
257 
258  static void HSVToRGBa(const double *hue, const double *saturation, const double *value, unsigned char *rgba,
259  unsigned int size);
260  static void HSVToRGBa(const unsigned char *hue, const unsigned char *saturation, const unsigned char *value,
261  unsigned char *rgba, unsigned int size);
262  static void RGBaToHSV(const unsigned char *rgba, double *hue, double *saturation, double *value, unsigned int size);
263  static void RGBaToHSV(const unsigned char *rgba, unsigned char *hue, unsigned char *saturation, unsigned char *value,
264  unsigned int size);
265 
266  static void HSVToRGB(const double *hue, const double *saturation, const double *value, unsigned char *rgb,
267  unsigned int size);
268  static void HSVToRGB(const unsigned char *hue, const unsigned char *saturation, const unsigned char *value,
269  unsigned char *rgb, unsigned int size);
270  static void RGBToHSV(const unsigned char *rgb, double *hue, double *saturation, double *value, unsigned int size);
271  static void RGBToHSV(const unsigned char *rgb, unsigned char *hue, unsigned char *saturation, unsigned char *value,
272  unsigned int size);
273 
274  static void demosaicBGGRToRGBaBilinear(const uint8_t *bggr, uint8_t *rgba, unsigned int width, unsigned int height,
275  unsigned int nThreads = 0);
276  static void demosaicBGGRToRGBaBilinear(const uint16_t *bggr, uint16_t *rgba, unsigned int width, unsigned int height,
277  unsigned int nThreads = 0);
278 
279  static void demosaicGBRGToRGBaBilinear(const uint8_t *gbrg, uint8_t *rgba, unsigned int width, unsigned int height,
280  unsigned int nThreads = 0);
281  static void demosaicGBRGToRGBaBilinear(const uint16_t *gbrg, uint16_t *rgba, unsigned int width, unsigned int height,
282  unsigned int nThreads = 0);
283 
284  static void demosaicGRBGToRGBaBilinear(const uint8_t *grbg, uint8_t *rgba, unsigned int width, unsigned int height,
285  unsigned int nThreads = 0);
286  static void demosaicGRBGToRGBaBilinear(const uint16_t *grbg, uint16_t *rgba, unsigned int width, unsigned int height,
287  unsigned int nThreads = 0);
288 
289  static void demosaicRGGBToRGBaBilinear(const uint8_t *rggb, uint8_t *rgba, unsigned int width, unsigned int height,
290  unsigned int nThreads = 0);
291  static void demosaicRGGBToRGBaBilinear(const uint16_t *rggb, uint16_t *rgba, unsigned int width, unsigned int height,
292  unsigned int nThreads = 0);
293 
294  static void demosaicBGGRToRGBaMalvar(const uint8_t *bggr, uint8_t *rgba, unsigned int width, unsigned int height,
295  unsigned int nThreads = 0);
296  static void demosaicBGGRToRGBaMalvar(const uint16_t *bggr, uint16_t *rgba, unsigned int width, unsigned int height,
297  unsigned int nThreads = 0);
298 
299  static void demosaicGBRGToRGBaMalvar(const uint8_t *gbrg, uint8_t *rgba, unsigned int width, unsigned int height,
300  unsigned int nThreads = 0);
301  static void demosaicGBRGToRGBaMalvar(const uint16_t *gbrg, uint16_t *rgba, unsigned int width, unsigned int height,
302  unsigned int nThreads = 0);
303 
304  static void demosaicGRBGToRGBaMalvar(const uint8_t *grbg, uint8_t *rgba, unsigned int width, unsigned int height,
305  unsigned int nThreads = 0);
306  static void demosaicGRBGToRGBaMalvar(const uint16_t *grbg, uint16_t *rgba, unsigned int width, unsigned int height,
307  unsigned int nThreads = 0);
308 
309  static void demosaicRGGBToRGBaMalvar(const uint8_t *rggb, uint8_t *rgba, unsigned int width, unsigned int height,
310  unsigned int nThreads = 0);
311  static void demosaicRGGBToRGBaMalvar(const uint16_t *rggb, uint16_t *rgba, unsigned int width, unsigned int height,
312  unsigned int nThreads = 0);
313 
314 private:
315  static void computeYCbCrLUT();
316 
317  static void HSV2RGB(const double *hue, const double *saturation, const double *value, unsigned char *rgba,
318  unsigned int size, unsigned int step);
319  static void RGB2HSV(const unsigned char *rgb, double *hue, double *saturation, double *value, unsigned int size,
320  unsigned int step);
321 
322 private:
323  static bool YCbCrLUTcomputed;
324  static int vpCrr[256];
325  static int vpCgb[256];
326  static int vpCgr[256];
327  static int vpCbb[256];
328 };
329 
330 #endif
static void convert(const vpImage< Type > &src, vpImage< Type > &dest)
static void YUVToRGB(unsigned char y, unsigned char u, unsigned char v, unsigned char &r, unsigned char &g, unsigned char &b)
Definition of the vpImage class member functions.
Definition: vpImage.h:74