mexopencv  0.1
mex interface for opencv library
cvtColor.cpp
Go to the documentation of this file.
1 
8 #include "mexopencv.hpp"
9 using namespace std;
10 using namespace cv;
11 
12 namespace {
15  // add alpha channel to RGB or BGR image
16  ("BGR2BGRA", cv::COLOR_BGR2BGRA)
17  ("RGB2RGBA", cv::COLOR_RGB2RGBA)
18  // remove alpha channel from RGB or BGR image
19  ("BGRA2BGR", cv::COLOR_BGRA2BGR)
20  ("RGBA2RGB", cv::COLOR_RGBA2RGB)
21  // convert between RGB and BGR color spaces (with or without alpha channel)
22  ("BGR2RGBA", cv::COLOR_BGR2RGBA)
23  ("RGB2BGRA", cv::COLOR_RGB2BGRA)
24  ("RGBA2BGR", cv::COLOR_RGBA2BGR)
25  ("BGRA2RGB", cv::COLOR_BGRA2RGB)
26  ("BGR2RGB", cv::COLOR_BGR2RGB)
27  ("RGB2BGR", cv::COLOR_RGB2BGR)
28  ("BGRA2RGBA", cv::COLOR_BGRA2RGBA)
29  ("RGBA2BGRA", cv::COLOR_RGBA2BGRA)
30  // convert between RGB/BGR and grayscale
31  ("BGR2GRAY", cv::COLOR_BGR2GRAY)
32  ("RGB2GRAY", cv::COLOR_RGB2GRAY)
33  ("GRAY2BGR", cv::COLOR_GRAY2BGR)
34  ("GRAY2RGB", cv::COLOR_GRAY2RGB)
35  ("GRAY2BGRA", cv::COLOR_GRAY2BGRA)
36  ("GRAY2RGBA", cv::COLOR_GRAY2RGBA)
37  ("BGRA2GRAY", cv::COLOR_BGRA2GRAY)
38  ("RGBA2GRAY", cv::COLOR_RGBA2GRAY)
39  // convert between RGB/BGR and BGR565 (16-bit images)
40  ("BGR2BGR565", cv::COLOR_BGR2BGR565)
41  ("RGB2BGR565", cv::COLOR_RGB2BGR565)
42  ("BGR5652BGR", cv::COLOR_BGR5652BGR)
43  ("BGR5652RGB", cv::COLOR_BGR5652RGB)
44  ("BGRA2BGR565", cv::COLOR_BGRA2BGR565)
45  ("RGBA2BGR565", cv::COLOR_RGBA2BGR565)
46  ("BGR5652BGRA", cv::COLOR_BGR5652BGRA)
47  ("BGR5652RGBA", cv::COLOR_BGR5652RGBA)
48  // convert between grayscale and BGR565 (16-bit images)
49  ("GRAY2BGR565", cv::COLOR_GRAY2BGR565)
50  ("BGR5652GRAY", cv::COLOR_BGR5652GRAY)
51  // convert between RGB/BGR and BGR555 (16-bit images)
52  ("BGR2BGR555", cv::COLOR_BGR2BGR555)
53  ("RGB2BGR555", cv::COLOR_RGB2BGR555)
54  ("BGR5552BGR", cv::COLOR_BGR5552BGR)
55  ("BGR5552RGB", cv::COLOR_BGR5552RGB)
56  ("BGRA2BGR555", cv::COLOR_BGRA2BGR555)
57  ("RGBA2BGR555", cv::COLOR_RGBA2BGR555)
58  ("BGR5552BGRA", cv::COLOR_BGR5552BGRA)
59  ("BGR5552RGBA", cv::COLOR_BGR5552RGBA)
60  // convert between grayscale and BGR555 (16-bit images)
61  ("GRAY2BGR555", cv::COLOR_GRAY2BGR555)
62  ("BGR5552GRAY", cv::COLOR_BGR5552GRAY)
63  // convert between RGB/BGR and CIE XYZ
64  ("BGR2XYZ", cv::COLOR_BGR2XYZ)
65  ("RGB2XYZ", cv::COLOR_RGB2XYZ)
66  ("XYZ2BGR", cv::COLOR_XYZ2BGR)
67  ("XYZ2RGB", cv::COLOR_XYZ2RGB)
68  // convert between RGB/BGR and luma-chroma (aka YCC)
69  ("BGR2YCrCb", cv::COLOR_BGR2YCrCb)
70  ("RGB2YCrCb", cv::COLOR_RGB2YCrCb)
71  ("YCrCb2BGR", cv::COLOR_YCrCb2BGR)
72  ("YCrCb2RGB", cv::COLOR_YCrCb2RGB)
73  // convert between RGB/BGR and YUV
74  ("BGR2YUV", cv::COLOR_BGR2YUV)
75  ("RGB2YUV", cv::COLOR_RGB2YUV)
76  ("YUV2BGR", cv::COLOR_YUV2BGR)
77  ("YUV2RGB", cv::COLOR_YUV2RGB)
78  // convert between RGB/BGR and HSV (hue saturation value)
79  ("BGR2HSV", cv::COLOR_BGR2HSV)
80  ("RGB2HSV", cv::COLOR_RGB2HSV)
81  ("HSV2BGR", cv::COLOR_HSV2BGR)
82  ("HSV2RGB", cv::COLOR_HSV2RGB)
83  ("BGR2HSV_FULL", cv::COLOR_BGR2HSV_FULL)
84  ("RGB2HSV_FULL", cv::COLOR_RGB2HSV_FULL)
85  ("HSV2BGR_FULL", cv::COLOR_HSV2BGR_FULL)
86  ("HSV2RGB_FULL", cv::COLOR_HSV2RGB_FULL)
87  // convert between RGB/BGR and HLS (hue lightness saturation)
88  ("BGR2HLS", cv::COLOR_BGR2HLS)
89  ("RGB2HLS", cv::COLOR_RGB2HLS)
90  ("HLS2BGR", cv::COLOR_HLS2BGR)
91  ("HLS2RGB", cv::COLOR_HLS2RGB)
92  ("BGR2HLS_FULL", cv::COLOR_BGR2HLS_FULL)
93  ("RGB2HLS_FULL", cv::COLOR_RGB2HLS_FULL)
94  ("HLS2BGR_FULL", cv::COLOR_HLS2BGR_FULL)
95  ("HLS2RGB_FULL", cv::COLOR_HLS2RGB_FULL)
96  // convert between RGB/BGR and CIE Lab
97  ("BGR2Lab", cv::COLOR_BGR2Lab)
98  ("RGB2Lab", cv::COLOR_RGB2Lab)
99  ("Lab2BGR", cv::COLOR_Lab2BGR)
100  ("Lab2RGB", cv::COLOR_Lab2RGB)
101  ("LBGR2Lab", cv::COLOR_LBGR2Lab)
102  ("LRGB2Lab", cv::COLOR_LRGB2Lab)
103  ("Lab2LBGR", cv::COLOR_Lab2LBGR)
104  ("Lab2LRGB", cv::COLOR_Lab2LRGB)
105  // convert between RGB/BGR and CIE Luv
106  ("BGR2Luv", cv::COLOR_BGR2Luv)
107  ("RGB2Luv", cv::COLOR_RGB2Luv)
108  ("Luv2BGR", cv::COLOR_Luv2BGR)
109  ("Luv2RGB", cv::COLOR_Luv2RGB)
110  ("LBGR2Luv", cv::COLOR_LBGR2Luv)
111  ("LRGB2Luv", cv::COLOR_LRGB2Luv)
112  ("Luv2LBGR", cv::COLOR_Luv2LBGR)
113  ("Luv2LRGB", cv::COLOR_Luv2LRGB)
114  // YUV 4:2:0 family to RGB
115  ("YUV2RGB_NV12", cv::COLOR_YUV2RGB_NV12)
116  ("YUV2BGR_NV12", cv::COLOR_YUV2BGR_NV12)
117  ("YUV2RGB_NV21", cv::COLOR_YUV2RGB_NV21)
118  ("YUV2BGR_NV21", cv::COLOR_YUV2BGR_NV21)
119  ("YUV420sp2RGB", cv::COLOR_YUV420sp2RGB)
120  ("YUV420sp2BGR", cv::COLOR_YUV420sp2BGR)
121  ("YUV2RGBA_NV12", cv::COLOR_YUV2RGBA_NV12)
122  ("YUV2BGRA_NV12", cv::COLOR_YUV2BGRA_NV12)
123  ("YUV2RGBA_NV21", cv::COLOR_YUV2RGBA_NV21)
124  ("YUV2BGRA_NV21", cv::COLOR_YUV2BGRA_NV21)
125  ("YUV420sp2RGBA", cv::COLOR_YUV420sp2RGBA)
126  ("YUV420sp2BGRA", cv::COLOR_YUV420sp2BGRA)
127  ("YUV2RGB_YV12", cv::COLOR_YUV2RGB_YV12)
128  ("YUV2BGR_YV12", cv::COLOR_YUV2BGR_YV12)
129  ("YUV2RGB_IYUV", cv::COLOR_YUV2RGB_IYUV)
130  ("YUV2BGR_IYUV", cv::COLOR_YUV2BGR_IYUV)
131  ("YUV2RGB_I420", cv::COLOR_YUV2RGB_I420)
132  ("YUV2BGR_I420", cv::COLOR_YUV2BGR_I420)
133  ("YUV420p2RGB", cv::COLOR_YUV420p2RGB)
134  ("YUV420p2BGR", cv::COLOR_YUV420p2BGR)
135  ("YUV2RGBA_YV12", cv::COLOR_YUV2RGBA_YV12)
136  ("YUV2BGRA_YV12", cv::COLOR_YUV2BGRA_YV12)
137  ("YUV2RGBA_IYUV", cv::COLOR_YUV2RGBA_IYUV)
138  ("YUV2BGRA_IYUV", cv::COLOR_YUV2BGRA_IYUV)
139  ("YUV2RGBA_I420", cv::COLOR_YUV2RGBA_I420)
140  ("YUV2BGRA_I420", cv::COLOR_YUV2BGRA_I420)
141  ("YUV420p2RGBA", cv::COLOR_YUV420p2RGBA)
142  ("YUV420p2BGRA", cv::COLOR_YUV420p2BGRA)
143  ("YUV2GRAY_420", cv::COLOR_YUV2GRAY_420)
144  ("YUV2GRAY_NV21", cv::COLOR_YUV2GRAY_NV21)
145  ("YUV2GRAY_NV12", cv::COLOR_YUV2GRAY_NV12)
146  ("YUV2GRAY_YV12", cv::COLOR_YUV2GRAY_YV12)
147  ("YUV2GRAY_IYUV", cv::COLOR_YUV2GRAY_IYUV)
148  ("YUV2GRAY_I420", cv::COLOR_YUV2GRAY_I420)
149  ("YUV420sp2GRAY", cv::COLOR_YUV420sp2GRAY)
150  ("YUV420p2GRAY", cv::COLOR_YUV420p2GRAY)
151  // YUV 4:2:2 family to RGB
152  ("YUV2RGB_UYVY", cv::COLOR_YUV2RGB_UYVY)
153  ("YUV2BGR_UYVY", cv::COLOR_YUV2BGR_UYVY)
154  //("YUV2RGB_VYUY", cv::COLOR_YUV2RGB_VYUY)
155  //("YUV2BGR_VYUY", cv::COLOR_YUV2BGR_VYUY)
156  ("YUV2RGB_Y422", cv::COLOR_YUV2RGB_Y422)
157  ("YUV2BGR_Y422", cv::COLOR_YUV2BGR_Y422)
158  ("YUV2RGB_UYNV", cv::COLOR_YUV2RGB_UYNV)
159  ("YUV2BGR_UYNV", cv::COLOR_YUV2BGR_UYNV)
160  ("YUV2RGBA_UYVY", cv::COLOR_YUV2RGBA_UYVY)
161  ("YUV2BGRA_UYVY", cv::COLOR_YUV2BGRA_UYVY)
162  //("YUV2RGBA_VYUY", cv::COLOR_YUV2RGBA_VYUY)
163  //("YUV2BGRA_VYUY", cv::COLOR_YUV2BGRA_VYUY)
164  ("YUV2RGBA_Y422", cv::COLOR_YUV2RGBA_Y422)
165  ("YUV2BGRA_Y422", cv::COLOR_YUV2BGRA_Y422)
166  ("YUV2RGBA_UYNV", cv::COLOR_YUV2RGBA_UYNV)
167  ("YUV2BGRA_UYNV", cv::COLOR_YUV2BGRA_UYNV)
168  ("YUV2RGB_YUY2", cv::COLOR_YUV2RGB_YUY2)
169  ("YUV2BGR_YUY2", cv::COLOR_YUV2BGR_YUY2)
170  ("YUV2RGB_YVYU", cv::COLOR_YUV2RGB_YVYU)
171  ("YUV2BGR_YVYU", cv::COLOR_YUV2BGR_YVYU)
172  ("YUV2RGB_YUYV", cv::COLOR_YUV2RGB_YUYV)
173  ("YUV2BGR_YUYV", cv::COLOR_YUV2BGR_YUYV)
174  ("YUV2RGB_YUNV", cv::COLOR_YUV2RGB_YUNV)
175  ("YUV2BGR_YUNV", cv::COLOR_YUV2BGR_YUNV)
176  ("YUV2RGBA_YUY2", cv::COLOR_YUV2RGBA_YUY2)
177  ("YUV2BGRA_YUY2", cv::COLOR_YUV2BGRA_YUY2)
178  ("YUV2RGBA_YVYU", cv::COLOR_YUV2RGBA_YVYU)
179  ("YUV2BGRA_YVYU", cv::COLOR_YUV2BGRA_YVYU)
180  ("YUV2RGBA_YUYV", cv::COLOR_YUV2RGBA_YUYV)
181  ("YUV2BGRA_YUYV", cv::COLOR_YUV2BGRA_YUYV)
182  ("YUV2RGBA_YUNV", cv::COLOR_YUV2RGBA_YUNV)
183  ("YUV2BGRA_YUNV", cv::COLOR_YUV2BGRA_YUNV)
184  ("YUV2GRAY_UYVY", cv::COLOR_YUV2GRAY_UYVY)
185  ("YUV2GRAY_YUY2", cv::COLOR_YUV2GRAY_YUY2)
186  //("YUV2GRAY_VYUY", cv::COLOR_YUV2GRAY_VYUY)
187  ("YUV2GRAY_Y422", cv::COLOR_YUV2GRAY_Y422)
188  ("YUV2GRAY_UYNV", cv::COLOR_YUV2GRAY_UYNV)
189  ("YUV2GRAY_YVYU", cv::COLOR_YUV2GRAY_YVYU)
190  ("YUV2GRAY_YUYV", cv::COLOR_YUV2GRAY_YUYV)
191  ("YUV2GRAY_YUNV", cv::COLOR_YUV2GRAY_YUNV)
192  // alpha premultiplication
193  ("RGBA2mRGBA", cv::COLOR_RGBA2mRGBA)
194  ("mRGBA2RGBA", cv::COLOR_mRGBA2RGBA)
195  // RGB to YUV 4:2:0 family
196  ("RGB2YUV_I420", cv::COLOR_RGB2YUV_I420)
197  ("BGR2YUV_I420", cv::COLOR_BGR2YUV_I420)
198  ("RGB2YUV_IYUV", cv::COLOR_RGB2YUV_IYUV)
199  ("BGR2YUV_IYUV", cv::COLOR_BGR2YUV_IYUV)
200  ("RGBA2YUV_I420", cv::COLOR_RGBA2YUV_I420)
201  ("BGRA2YUV_I420", cv::COLOR_BGRA2YUV_I420)
202  ("RGBA2YUV_IYUV", cv::COLOR_RGBA2YUV_IYUV)
203  ("BGRA2YUV_IYUV", cv::COLOR_BGRA2YUV_IYUV)
204  ("RGB2YUV_YV12", cv::COLOR_RGB2YUV_YV12)
205  ("BGR2YUV_YV12", cv::COLOR_BGR2YUV_YV12)
206  ("RGBA2YUV_YV12", cv::COLOR_RGBA2YUV_YV12)
207  ("BGRA2YUV_YV12", cv::COLOR_BGRA2YUV_YV12)
208  // Demosaicing
209  ("BayerBG2BGR", cv::COLOR_BayerBG2BGR)
210  ("BayerGB2BGR", cv::COLOR_BayerGB2BGR)
211  ("BayerRG2BGR", cv::COLOR_BayerRG2BGR)
212  ("BayerGR2BGR", cv::COLOR_BayerGR2BGR)
213  ("BayerBG2RGB", cv::COLOR_BayerBG2RGB)
214  ("BayerGB2RGB", cv::COLOR_BayerGB2RGB)
215  ("BayerRG2RGB", cv::COLOR_BayerRG2RGB)
216  ("BayerGR2RGB", cv::COLOR_BayerGR2RGB)
217  ("BayerBG2GRAY", cv::COLOR_BayerBG2GRAY)
218  ("BayerGB2GRAY", cv::COLOR_BayerGB2GRAY)
219  ("BayerRG2GRAY", cv::COLOR_BayerRG2GRAY)
220  ("BayerGR2GRAY", cv::COLOR_BayerGR2GRAY)
221  // Demosaicing using Variable Number of Gradients
222  ("BayerBG2BGR_VNG", cv::COLOR_BayerBG2BGR_VNG)
223  ("BayerGB2BGR_VNG", cv::COLOR_BayerGB2BGR_VNG)
224  ("BayerRG2BGR_VNG", cv::COLOR_BayerRG2BGR_VNG)
225  ("BayerGR2BGR_VNG", cv::COLOR_BayerGR2BGR_VNG)
226  ("BayerBG2RGB_VNG", cv::COLOR_BayerBG2RGB_VNG)
227  ("BayerGB2RGB_VNG", cv::COLOR_BayerGB2RGB_VNG)
228  ("BayerRG2RGB_VNG", cv::COLOR_BayerRG2RGB_VNG)
229  ("BayerGR2RGB_VNG", cv::COLOR_BayerGR2RGB_VNG)
230  // Edge-Aware Demosaicing
231  ("BayerBG2BGR_EA", cv::COLOR_BayerBG2BGR_EA)
232  ("BayerGB2BGR_EA", cv::COLOR_BayerGB2BGR_EA)
233  ("BayerRG2BGR_EA", cv::COLOR_BayerRG2BGR_EA)
234  ("BayerGR2BGR_EA", cv::COLOR_BayerGR2BGR_EA)
235  ("BayerBG2RGB_EA", cv::COLOR_BayerBG2RGB_EA)
236  ("BayerGB2RGB_EA", cv::COLOR_BayerGB2RGB_EA)
237  ("BayerRG2RGB_EA", cv::COLOR_BayerRG2RGB_EA)
238  ("BayerGR2RGB_EA", cv::COLOR_BayerGR2RGB_EA);
239 }
240 
248 void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
249 {
250  // Check the number of arguments
251  nargchk(nrhs>=2 && (nrhs%2)==0 && nlhs<=1);
252 
253  // Argument vector
254  vector<MxArray> rhs(prhs, prhs+nrhs);
255 
256  // Option processing
257  int dstCn = 0;
258  for (int i=2; i<nrhs; i+=2) {
259  string key(rhs[i].toString());
260  if (key=="DstCn")
261  dstCn = rhs[i+1].toInt();
262  else
263  mexErrMsgIdAndTxt("mexopencv:error","Unrecognized option");
264  }
265 
266  // Process
267  Mat src(rhs[0].toMat(rhs[0].isUint8() ? CV_8U :
268  (rhs[0].isUint16() ? CV_16U : CV_32F))), dst;
269  int code = ColorConv[rhs[1].toString()];
270  cvtColor(src, dst, code, dstCn);
271  plhs[0] = MxArray(dst);
272 }
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
Main entry called from Matlab.
Definition: cvtColor.cpp:248
mxArray object wrapper for data conversion and manipulation.
Definition: MxArray.hpp:123
void nargchk(bool cond)
Alias for input/ouput arguments number check.
Definition: mexopencv.hpp:166
Global constant definitions.
std::map wrapper with one-line initialization and lookup method.
Definition: MxArray.hpp:927