Visual Servoing Platform  version 3.0.1
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
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
7  * modify it under the terms of the GNU General Public License
8  * ("GPL") version 2 as published by the Free Software Foundation.
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 http://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  * Authors:
34  * Eric Marchand
35  * Fabien Spindler
36  * Anthony Saunier
37  *
38  *****************************************************************************/
39 
45 #ifndef vpIMAGECONVERT_H
46 #define vpIMAGECONVERT_H
47 
48 #include <stdint.h>
49 
50 // image
51 #include <visp3/core/vpConfig.h>
52 #include <visp3/core/vpImage.h>
53 #include <visp3/core/vpDebug.h>
54 // color
55 #include <visp3/core/vpRGBa.h>
56 
57 #ifdef VISP_HAVE_OPENCV
58 # if (VISP_HAVE_OPENCV_VERSION >= 0x030000) // Require opencv >= 3.0.0
59 # include <opencv2/core/core.hpp>
60 # include <opencv2/highgui/highgui.hpp>
61 # include <opencv2/imgproc/imgproc.hpp>
62 # elif (VISP_HAVE_OPENCV_VERSION >= 0x020408) // Require opencv >= 2.4.8
63 # include <opencv2/core/core.hpp>
64 # include <opencv2/highgui/highgui.hpp>
65 # include <opencv2/imgproc/imgproc.hpp>
66 # elif (VISP_HAVE_OPENCV_VERSION >= 0x020101) // Require opencv >= 2.1.1
67 # include <opencv2/core/core.hpp>
68 # include <opencv2/legacy/legacy.hpp>
69 # include <opencv2/highgui/highgui.hpp>
70 # include <opencv2/highgui/highgui_c.h>
71 # else
72 # include <highgui.h>
73 # endif
74 #endif
75 
76 #ifdef VISP_HAVE_YARP
77 # include <yarp/sig/Image.h>
78 #endif
79 
80 #if defined(_WIN32)
81 # include <windows.h>
82 #endif
83 
97 class VISP_EXPORT vpImageConvert
98 {
99 
100 public:
101  static void createDepthHistogram(const vpImage<uint16_t> &src_depth, vpImage<vpRGBa> &dest_rgba);
102  static void convert(const vpImage<unsigned char> &src, vpImage<vpRGBa> & dest) ;
103  static void convert(const vpImage<vpRGBa> &src, vpImage<unsigned char> & dest) ;
104 
105  static void convert(const vpImage<float> &src, vpImage<unsigned char> &dest);
106  static void convert(const vpImage<unsigned char> &src, vpImage<float> &dest);
107 
108  static void convert(const vpImage<double> &src, vpImage<unsigned char> &dest);
109  static void convert(const vpImage<unsigned char> &src, vpImage<double> &dest);
110 
111  static void convert(const vpImage<uint16_t> &src, vpImage<unsigned char> &dest);
112  static void convert(const vpImage<unsigned char> &src, vpImage<uint16_t> &dest);
113 
119  template< typename Type>
120  static void convert(const vpImage<Type> &src, vpImage<Type> & dest)
121  {
122  dest = src;
123  }
124 
125 #ifdef VISP_HAVE_OPENCV
126  // Deprecated: will be removed with OpenCV transcient from C to C++ api
127  static void convert(const IplImage* src, vpImage<vpRGBa> & dest, bool flip = false) ;
128  static void convert(const IplImage* src, vpImage<unsigned char> & dest, bool flip = false) ;
129  static void convert(const vpImage<vpRGBa> & src, IplImage *&dest) ;
130  static void convert(const vpImage<unsigned char> & src, IplImage* &dest) ;
131 # if VISP_HAVE_OPENCV_VERSION >= 0x020100
132  static void convert(const cv::Mat& src, vpImage<vpRGBa>& dest, const bool flip = false);
133  static void convert(const cv::Mat& src, vpImage<unsigned char>& dest, const bool flip = false);
134  static void convert(const vpImage<vpRGBa> & src, cv::Mat& dest) ;
135  static void convert(const vpImage<unsigned char> & src, cv::Mat& dest, const bool copyData = true) ;
136 # endif
137 #endif
138 
139 #ifdef VISP_HAVE_YARP
140  static void convert(const vpImage<unsigned char> & src,
141  yarp::sig::ImageOf< yarp::sig::PixelMono > *dest, const bool copyData = true) ;
142  static void convert(const yarp::sig::ImageOf< yarp::sig::PixelMono > *src,
143  vpImage<unsigned char> & dest,const bool copyData = true ) ;
144 
145 
146  static void convert(const vpImage<vpRGBa> & src,
147  yarp::sig::ImageOf< yarp::sig::PixelRgba > *dest, const bool copyData = true) ;
148  static void convert(const yarp::sig::ImageOf< yarp::sig::PixelRgba > *src,
149  vpImage<vpRGBa> & dest,const bool copyData = true) ;
150 
151  static void convert(const vpImage<vpRGBa> & src,
152  yarp::sig::ImageOf< yarp::sig::PixelRgb > *dest) ;
153  static void convert(const yarp::sig::ImageOf< yarp::sig::PixelRgb > *src,
154  vpImage<vpRGBa> & dest) ;
155 #endif
156 
157  static void split(const vpImage<vpRGBa> &src,
161  vpImage<unsigned char>* pa = NULL);
162 
163  static void merge(const vpImage<unsigned char> *R,
164  const vpImage<unsigned char> *G,
165  const vpImage<unsigned char> *B,
166  const vpImage<unsigned char> *a,
167  vpImage<vpRGBa> &RGBa);
168 
186  static inline void YUVToRGB(unsigned char y,
187  unsigned char u,
188  unsigned char v,
189  unsigned char &r,
190  unsigned char &g,
191  unsigned char &b)
192  {
193  double dr, dg, db;
194  dr = floor(0.9999695*y - 0.0009508*(u-128) + 1.1359061*(v-128));
195  dg = floor(0.9999695*y - 0.3959609*(u-128) - 0.5782955*(v-128));
196  db = floor(0.9999695*y + 2.04112*(u-128) - 0.0016314*(v-128));
197 
198  dr = dr < 0. ? 0. : dr;
199  dg = dg < 0. ? 0. : dg;
200  db = db < 0. ? 0. : db;
201  dr = dr > 255. ? 255. : dr;
202  dg = dg > 255. ? 255. : dg;
203  db = db > 255. ? 255. : db;
204 
205  r = (unsigned char) dr;
206  g = (unsigned char) dg;
207  b = (unsigned char) db;
208  }
209  static void YUYVToRGBa(unsigned char* yuyv, unsigned char* rgba,
210  unsigned int width, unsigned int height);
211  static void YUYVToRGB(unsigned char* yuyv, unsigned char* rgb,
212  unsigned int width, unsigned int height);
213  static void YUYVToGrey(unsigned char* yuyv, unsigned char* grey,
214  unsigned int size);
215  static void YUV411ToRGBa(unsigned char* yuv,
216  unsigned char* rgba, unsigned int size);
217  static void YUV411ToRGB(unsigned char* yuv,
218  unsigned char* rgb, unsigned int size);
219  static void YUV411ToGrey(unsigned char* yuv,
220  unsigned char* grey, unsigned int size);
221  static void YUV422ToRGBa(unsigned char* yuv,
222  unsigned char* rgba, unsigned int size);
223  static void YUV422ToRGB(unsigned char* yuv,
224  unsigned char* rgb, unsigned int size);
225  static void YUV422ToGrey(unsigned char* yuv,
226  unsigned char* grey, unsigned int size);
227  static void YUV420ToRGBa(unsigned char* yuv,
228  unsigned char* rgba, unsigned int width, unsigned int height);
229  static void YUV420ToRGB(unsigned char* yuv,
230  unsigned char* rgb, unsigned int width, unsigned int height);
231  static void YUV420ToGrey(unsigned char* yuv,
232  unsigned char* grey, unsigned int size);
233 
234  static void YUV444ToRGBa(unsigned char* yuv,
235  unsigned char* rgba, unsigned int size);
236  static void YUV444ToRGB(unsigned char* yuv,
237  unsigned char* rgb, unsigned int size);
238  static void YUV444ToGrey(unsigned char* yuv,
239  unsigned char* grey, unsigned int size);
240 
241  static void YV12ToRGBa(unsigned char* yuv,
242  unsigned char* rgba, unsigned int width, unsigned int height);
243  static void YV12ToRGB(unsigned char* yuv,
244  unsigned char* rgb, unsigned int width, unsigned int height);
245  static void YVU9ToRGBa(unsigned char* yuv,
246  unsigned char* rgba, unsigned int width, unsigned int height);
247  static void YVU9ToRGB(unsigned char* yuv,
248  unsigned char* rgb, unsigned int width, unsigned int height);
249  static void RGBToRGBa(unsigned char* rgb,
250  unsigned char* rgba, unsigned int size);
251  static void RGBaToRGB(unsigned char* rgba,
252  unsigned char* rgb, unsigned int size);
253 
254  static void RGBToGrey(unsigned char* rgb, unsigned char* grey, unsigned int size);
255  static void RGBaToGrey(unsigned char* rgba, unsigned char* grey, unsigned int size);
256 
257  static void RGBToRGBa(unsigned char * rgb, unsigned char * rgba,
258  unsigned int width, unsigned int height, bool flip = false);
259  static void RGBToGrey(unsigned char * rgb, unsigned char * grey,
260  unsigned int width, unsigned int height, bool flip = false);
261 
262  static void GreyToRGBa(unsigned char* grey,
263  unsigned char* rgba, unsigned int size);
264  static void GreyToRGB(unsigned char* grey,
265  unsigned char* rgb, unsigned int size);
266 
267  static void BGRToRGBa(unsigned char * bgr, unsigned char * rgba,
268  unsigned int width, unsigned int height, bool flip=false);
269 
270  static void BGRToGrey(unsigned char * bgr, unsigned char * grey,
271  unsigned int width, unsigned int height, bool flip=false);
272 
273  static void YCbCrToRGB(unsigned char *ycbcr, unsigned char *rgb,
274  unsigned int size);
275  static void YCbCrToRGBa (unsigned char *ycbcr, unsigned char *rgb,
276  unsigned int size);
277  static void YCrCbToRGB(unsigned char *ycbcr, unsigned char *rgb,
278  unsigned int size);
279  static void YCrCbToRGBa(unsigned char *ycbcr, unsigned char *rgb,
280  unsigned int size);
281  static void YCbCrToGrey(unsigned char *ycbcr, unsigned char *grey,
282  unsigned int size);
283  static void MONO16ToGrey(unsigned char *grey16, unsigned char *grey,
284  unsigned int size);
285  static void MONO16ToRGBa(unsigned char *grey16, unsigned char *rgba,
286  unsigned int size);
287 
288  static void HSVToRGBa(const double *hue, const double *saturation, const double *value, unsigned char *rgba,
289  const unsigned int size);
290  static void HSVToRGBa(const unsigned char *hue, const unsigned char *saturation, const unsigned char *value,
291  unsigned char *rgba, const unsigned int size);
292  static void RGBaToHSV(const unsigned char *rgba, double *hue, double *saturation, double *value,
293  const unsigned int size);
294  static void RGBaToHSV(const unsigned char *rgba, unsigned char *hue, unsigned char *saturation, unsigned char *value,
295  const unsigned int size);
296 
297  static void HSVToRGB(const double *hue, const double *saturation, const double *value, unsigned char *rgb,
298  const unsigned int size);
299  static void HSVToRGB(const unsigned char *hue, const unsigned char *saturation, const unsigned char *value,
300  unsigned char *rgb, const unsigned int size);
301  static void RGBToHSV(const unsigned char *rgb, double *hue, double *saturation, double *value,
302  const unsigned int size);
303  static void RGBToHSV(const unsigned char *rgb, unsigned char *hue, unsigned char *saturation, unsigned char *value,
304  const unsigned int size);
305 
306 private:
307  static void computeYCbCrLUT();
308 
309  static void HSV2RGB(const double *hue, const double *saturation, const double *value, unsigned char *rgba,
310  const unsigned int size, const unsigned int step);
311  static void RGB2HSV(const unsigned char *rgb, double *hue, double *saturation, double *value,
312  const unsigned int size, const unsigned int step);
313 
314 private:
315  static bool YCbCrLUTcomputed;
316  static int vpCrr[256];
317  static int vpCgb[256];
318  static int vpCgr[256];
319  static int vpCbb[256];
320 
321 } ;
322 
323 #endif
324 
325 
326 /*
327 * Local variables:
328 * c-basic-offset: 2
329 * End:
330 */
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:117