Visual Servoing Platform  version 3.1.0
vpImageConvert.h
1 /****************************************************************************
2  *
3  * This file is part of the ViSP software.
4  * Copyright (C) 2005 - 2017 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 >= 0x030000) // Require opencv >= 3.0.0
60 #include <opencv2/core/core.hpp>
61 #include <opencv2/highgui/highgui.hpp>
62 #include <opencv2/imgproc/imgproc.hpp>
63 #elif (VISP_HAVE_OPENCV_VERSION >= 0x020408) // Require opencv >= 2.4.8
64 #include <opencv2/core/core.hpp>
65 #include <opencv2/highgui/highgui.hpp>
66 #include <opencv2/imgproc/imgproc.hpp>
67 #elif (VISP_HAVE_OPENCV_VERSION >= 0x020101) // Require opencv >= 2.1.1
68 #include <opencv2/core/core.hpp>
69 #include <opencv2/highgui/highgui.hpp>
70 #include <opencv2/highgui/highgui_c.h>
71 #include <opencv2/legacy/legacy.hpp>
72 #else
73 #include <highgui.h>
74 #endif
75 #endif
76 
77 #ifdef VISP_HAVE_YARP
78 #include <yarp/sig/Image.h>
79 #endif
80 
81 #if defined(_WIN32)
82 // Include WinSock2.h before windows.h to ensure that winsock.h is not
83 // included by windows.h since winsock.h and winsock2.h are incompatible
84 #include <WinSock2.h>
85 #include <windows.h>
86 #endif
87 
101 class VISP_EXPORT vpImageConvert
102 {
103 
104 public:
105  static void createDepthHistogram(const vpImage<uint16_t> &src_depth, vpImage<vpRGBa> &dest_rgba);
106  static void createDepthHistogram(const vpImage<uint16_t> &src_depth, vpImage<unsigned char> &dest_depth);
107  static void convert(const vpImage<unsigned char> &src, vpImage<vpRGBa> &dest);
108  static void convert(const vpImage<vpRGBa> &src, vpImage<unsigned char> &dest);
109 
110  static void convert(const vpImage<float> &src, vpImage<unsigned char> &dest);
111  static void convert(const vpImage<unsigned char> &src, vpImage<float> &dest);
112 
113  static void convert(const vpImage<double> &src, vpImage<unsigned char> &dest);
114  static void convert(const vpImage<unsigned char> &src, vpImage<double> &dest);
115 
116  static void convert(const vpImage<uint16_t> &src, vpImage<unsigned char> &dest);
117  static void convert(const vpImage<unsigned char> &src, vpImage<uint16_t> &dest);
118 
124  template <typename Type> static void convert(const vpImage<Type> &src, vpImage<Type> &dest) { dest = src; }
125 
126 #ifdef VISP_HAVE_OPENCV
127  // Deprecated: will be removed with OpenCV transcient from C to C++ api
128  static void convert(const IplImage *src, vpImage<vpRGBa> &dest, bool flip = false);
129  static void convert(const IplImage *src, vpImage<unsigned char> &dest, bool flip = false);
130  static void convert(const vpImage<vpRGBa> &src, IplImage *&dest);
131  static void convert(const vpImage<unsigned char> &src, IplImage *&dest);
132 #if VISP_HAVE_OPENCV_VERSION >= 0x020100
133  static void convert(const cv::Mat &src, vpImage<vpRGBa> &dest, const bool flip = false);
134  static void convert(const cv::Mat &src, vpImage<unsigned char> &dest, const bool flip = false);
135  static void convert(const vpImage<vpRGBa> &src, cv::Mat &dest);
136  static void convert(const vpImage<unsigned char> &src, cv::Mat &dest, const bool copyData = true);
137 #endif
138 #endif
139 
140 #ifdef VISP_HAVE_YARP
141  static void convert(const vpImage<unsigned char> &src, yarp::sig::ImageOf<yarp::sig::PixelMono> *dest,
142  const bool copyData = true);
143  static void convert(const yarp::sig::ImageOf<yarp::sig::PixelMono> *src, vpImage<unsigned char> &dest,
144  const bool copyData = true);
145 
146  static void convert(const vpImage<vpRGBa> &src, yarp::sig::ImageOf<yarp::sig::PixelRgba> *dest,
147  const bool copyData = true);
148  static void convert(const yarp::sig::ImageOf<yarp::sig::PixelRgba> *src, vpImage<vpRGBa> &dest,
149  const bool copyData = true);
150 
151  static void convert(const vpImage<vpRGBa> &src, yarp::sig::ImageOf<yarp::sig::PixelRgb> *dest);
152  static void convert(const yarp::sig::ImageOf<yarp::sig::PixelRgb> *src, vpImage<vpRGBa> &dest);
153 #endif
154 
155  static void split(const vpImage<vpRGBa> &src, vpImage<unsigned char> *pR, vpImage<unsigned char> *pG,
157 
158  static void merge(const vpImage<unsigned char> *R, const vpImage<unsigned char> *G, const vpImage<unsigned char> *B,
159  const vpImage<unsigned char> *a, vpImage<vpRGBa> &RGBa);
160 
177  static inline void YUVToRGB(unsigned char y, unsigned char u, unsigned char v, unsigned char &r, unsigned char &g,
178  unsigned char &b)
179  {
180  double dr, dg, db;
181  dr = floor(0.9999695 * y - 0.0009508 * (u - 128) + 1.1359061 * (v - 128));
182  dg = floor(0.9999695 * y - 0.3959609 * (u - 128) - 0.5782955 * (v - 128));
183  db = floor(0.9999695 * y + 2.04112 * (u - 128) - 0.0016314 * (v - 128));
184 
185  dr = dr < 0. ? 0. : dr;
186  dg = dg < 0. ? 0. : dg;
187  db = db < 0. ? 0. : db;
188  dr = dr > 255. ? 255. : dr;
189  dg = dg > 255. ? 255. : dg;
190  db = db > 255. ? 255. : db;
191 
192  r = (unsigned char)dr;
193  g = (unsigned char)dg;
194  b = (unsigned char)db;
195  }
196  static void YUYVToRGBa(unsigned char *yuyv, unsigned char *rgba, unsigned int width, unsigned int height);
197  static void YUYVToRGB(unsigned char *yuyv, unsigned char *rgb, unsigned int width, unsigned int height);
198  static void YUYVToGrey(unsigned char *yuyv, unsigned char *grey, unsigned int size);
199  static void YUV411ToRGBa(unsigned char *yuv, unsigned char *rgba, unsigned int size);
200  static void YUV411ToRGB(unsigned char *yuv, unsigned char *rgb, unsigned int size);
201  static void YUV411ToGrey(unsigned char *yuv, unsigned char *grey, unsigned int size);
202  static void YUV422ToRGBa(unsigned char *yuv, unsigned char *rgba, unsigned int size);
203  static void YUV422ToRGB(unsigned char *yuv, unsigned char *rgb, unsigned int size);
204  static void YUV422ToGrey(unsigned char *yuv, unsigned char *grey, unsigned int size);
205  static void YUV420ToRGBa(unsigned char *yuv, unsigned char *rgba, unsigned int width, unsigned int height);
206  static void YUV420ToRGB(unsigned char *yuv, unsigned char *rgb, unsigned int width, unsigned int height);
207  static void YUV420ToGrey(unsigned char *yuv, unsigned char *grey, unsigned int size);
208 
209  static void YUV444ToRGBa(unsigned char *yuv, unsigned char *rgba, unsigned int size);
210  static void YUV444ToRGB(unsigned char *yuv, unsigned char *rgb, unsigned int size);
211  static void YUV444ToGrey(unsigned char *yuv, unsigned char *grey, unsigned int size);
212 
213  static void YV12ToRGBa(unsigned char *yuv, unsigned char *rgba, unsigned int width, unsigned int height);
214  static void YV12ToRGB(unsigned char *yuv, unsigned char *rgb, unsigned int width, unsigned int height);
215  static void YVU9ToRGBa(unsigned char *yuv, unsigned char *rgba, unsigned int width, unsigned int height);
216  static void YVU9ToRGB(unsigned char *yuv, unsigned char *rgb, unsigned int width, unsigned int height);
217  static void RGBToRGBa(unsigned char *rgb, unsigned char *rgba, unsigned int size);
218  static void RGBaToRGB(unsigned char *rgba, unsigned char *rgb, unsigned int size);
219 
220  static void RGBToGrey(unsigned char *rgb, unsigned char *grey, unsigned int size);
221  static void RGBaToGrey(unsigned char *rgba, unsigned char *grey, unsigned int size);
222 
223  static void RGBToRGBa(unsigned char *rgb, unsigned char *rgba, unsigned int width, unsigned int height,
224  bool flip = false);
225  static void RGBToGrey(unsigned char *rgb, unsigned char *grey, unsigned int width, unsigned int height,
226  bool flip = false);
227 
228  static void GreyToRGBa(unsigned char *grey, unsigned char *rgba, unsigned int size);
229  static void GreyToRGB(unsigned char *grey, unsigned char *rgb, unsigned int size);
230 
231  static void BGRToRGBa(unsigned char *bgr, unsigned char *rgba, unsigned int width, unsigned int height,
232  bool flip = false);
233 
234  static void BGRToGrey(unsigned char *bgr, unsigned char *grey, unsigned int width, unsigned int height,
235  bool flip = false);
236 
237  static void YCbCrToRGB(unsigned char *ycbcr, unsigned char *rgb, unsigned int size);
238  static void YCbCrToRGBa(unsigned char *ycbcr, unsigned char *rgb, unsigned int size);
239  static void YCrCbToRGB(unsigned char *ycbcr, unsigned char *rgb, unsigned int size);
240  static void YCrCbToRGBa(unsigned char *ycbcr, unsigned char *rgb, unsigned int size);
241  static void YCbCrToGrey(unsigned char *ycbcr, unsigned char *grey, unsigned int size);
242  static void MONO16ToGrey(unsigned char *grey16, unsigned char *grey, unsigned int size);
243  static void MONO16ToRGBa(unsigned char *grey16, unsigned char *rgba, unsigned int size);
244 
245  static void HSVToRGBa(const double *hue, const double *saturation, const double *value, unsigned char *rgba,
246  const unsigned int size);
247  static void HSVToRGBa(const unsigned char *hue, const unsigned char *saturation, const unsigned char *value,
248  unsigned char *rgba, const unsigned int size);
249  static void RGBaToHSV(const unsigned char *rgba, double *hue, double *saturation, double *value,
250  const unsigned int size);
251  static void RGBaToHSV(const unsigned char *rgba, unsigned char *hue, unsigned char *saturation, unsigned char *value,
252  const unsigned int size);
253 
254  static void HSVToRGB(const double *hue, const double *saturation, const double *value, unsigned char *rgb,
255  const unsigned int size);
256  static void HSVToRGB(const unsigned char *hue, const unsigned char *saturation, const unsigned char *value,
257  unsigned char *rgb, const unsigned int size);
258  static void RGBToHSV(const unsigned char *rgb, double *hue, double *saturation, double *value,
259  const unsigned int size);
260  static void RGBToHSV(const unsigned char *rgb, unsigned char *hue, unsigned char *saturation, unsigned char *value,
261  const unsigned int size);
262 
263 private:
264  static void computeYCbCrLUT();
265 
266  static void HSV2RGB(const double *hue, const double *saturation, const double *value, unsigned char *rgba,
267  const unsigned int size, const unsigned int step);
268  static void RGB2HSV(const unsigned char *rgb, double *hue, double *saturation, double *value, const unsigned int size,
269  const unsigned int step);
270 
271 private:
272  static bool YCbCrLUTcomputed;
273  static int vpCrr[256];
274  static int vpCgb[256];
275  static int vpCgr[256];
276  static int vpCbb[256];
277 };
278 
279 #endif
280 
281 /*
282  * Local variables:
283  * c-basic-offset: 2
284  * End:
285  */
static void YUVToRGB(unsigned char y, unsigned char u, unsigned char v, unsigned char &r, unsigned char &g, unsigned char &b)
static void convert(const vpImage< Type > &src, vpImage< Type > &dest)
Definition of the vpImage class member functions.
Definition: vpImage.h:116