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