Converts an image from one color space to another.
The function converts an input image from one color space to another. In case
of a transformation to-from RGB color space, the order of the channels should
be specified explicitly (RGB or BGR).
Note that the default color format in OpenCV is often referred to as RGB but
it is actually BGR (the bytes are reversed). So the first byte in a standard
(24-bit) color image will be an 8-bit Blue component, the second byte will be
Green, and the third byte will be Red. The fourth, fifth, and sixth bytes
would then be the second pixel (Blue, then Green, then Red), and so on.
The conventional ranges for R, G, and B channel values are:
- 0 to 255 for
CV_8U
images
- 0 to 65535 for
CV_16U
images
- 0 to 1 for
CV_32F
images
In case of linear transformations, the range does not matter.
But in case of a non-linear transformation, an input RGB image should be
normalized to the proper value range to get the correct results, for example,
for RGB-> L*u*v* transformation. For example, if you have a 32-bit
floating-point image directly converted from an 8-bit image without any
scaling, then it will have the 0..255 value range instead of 0..1 assumed by
the function. So, before calling cvtColor
, you need first to
scale the image down:
// C++ code:
img *= 1./255;
cvtColor(img, img, CV_BGR2Luv);
If you use cvtColor
with 8-bit images, the conversion will have
some information lost. For many applications, this will not be noticeable but
it is recommended to use 32-bit images in applications that need the full
range of colors or that convert an image before an operation and then convert
back.
If conversion adds the alpha channel, its value will set to the maximum of
corresponding channel range: 255 for CV_8U
, 65535 for
CV_16U
, 1 for CV_32F
.
The function can do the following transformations:
- RGB GRAY (
CV_BGR2GRAY, CV_RGB2GRAY, CV_GRAY2BGR,
CV_GRAY2RGB
) Transformations within RGB space like adding/removing the
alpha channel, reversing the channel order, conversion to/from 16-bit RGB
color (R5:G6:B5 or R5:G5:B5), as well as conversion to/from grayscale using:
RGB[A] to Gray: Y
and
Gray to RGB[A]: R
The conversion from a RGB image to gray is done with:
// C++ code:
cvtColor(src, bwsrc, CV_RGB2GRAY);
More advanced channel reordering can also be done with "mixChannels".
- RGB CIE XYZ.Rec 709 with D65 white point
(
CV_BGR2XYZ, CV_RGB2XYZ, CV_XYZ2BGR, CV_XYZ2RGB
):
X
Z ltBR gt
R
B ltBR gt
X, Y and Z cover the whole value range (in case of
floating-point images, Z may exceed 1).
- RGB YCrCb JPEG (or YCC) (
CV_BGR2YCrCb,
CV_RGB2YCrCb, CV_YCrCb2BGR, CV_YCrCb2RGB
)
Y
Cr
Cb
R
G
B
where
delta =
Y, Cr, and Cb cover the whole value range.
- RGB HSV (
CV_BGR2HSV, CV_RGB2HSV, CV_HSV2BGR,
CV_HSV2RGB
) In case of 8-bit and 16-bit images, R, G, and B are
converted to the floating-point format and scaled to fit the 0 to 1 range.
V
S
H
If H<0 then H . On output 0 ,
0 , 0 .
The values are then converted to the destination data type:
V
- 16-bit images (currently not supported)
V <- 65535 V, S <- 65535 S, H <- H
- 32-bit images H, S, and V are left as is
- RGB HLS (
CV_BGR2HLS, CV_RGB2HLS, CV_HLS2BGR,
CV_HLS2RGB
).
In case of 8-bit and 16-bit images, R, G, and B are converted to the
floating-point format and scaled to fit the 0 to 1 range.
V_(max)
V_(min)
L
S = 0.5)
H
If H<0 then H . On output 0 ,
0 , 0 .
The values are then converted to the destination data type:
V
- 16-bit images (currently not supported)
V <- 65535 * V, S <- 65535 * S, H <- H
- 32-bit images H, S, V are left as is
- RGB CIE L*a*b* (
CV_BGR2Lab, CV_RGB2Lab, CV_Lab2BGR,
CV_Lab2RGB
).
In case of 8-bit and 16-bit images, R, G, and B are converted to the
floating-point format and scaled to fit the 0 to 1 range.
[X Y Z]
X
Z
L
a
b
where
f(t)= t^(1/3) for t>0.008856; 7.787 t+16/116 for t
and
delta = 128 for 8-bit images; 0 for floating-point images
This outputs 0 , -127 , -127 . The values are then converted to the destination data type:
L
- 16-bit images (currently not supported)
- 32-bit images L, a, and b are left as is
- RGB CIE L*u*v* (
CV_BGR2Luv, CV_RGB2Luv, CV_Luv2BGR,
CV_Luv2RGB
).
In case of 8-bit and 16-bit images, R, G, and B are converted to the
floating-point format and scaled to fit 0 to 1 range.
[X Y Z]
L
u'
v'
u
v
This outputs 0 , -134 , -140 .
The values are then converted to the destination data type:
L
- 16-bit images (currently not supported)
- 32-bit images L, u, and v are left as is
The above formulae for converting RGB to/from various color spaces have been
taken from multiple sources on the web, primarily from the Charles Poynton
site http://www.poynton.com/ColorFAQ.html
- Bayer -> RGB (
CV_BayerBG2BGR, CV_BayerGB2BGR,
CV_BayerRG2BGR, CV_BayerGR2BGR, CV_BayerBG2RGB, CV_BayerGB2RGB,
CV_BayerRG2RGB, CV_BayerGR2RGB
). The Bayer pattern is widely used in
CCD and CMOS cameras. It enables you to get color pictures from a single
plane where R,G, and B pixels (sensors of a particular component) are
interleaved as follows: The output RGB components of a pixel are interpolated
from 1, 2, or
// C++ code:
4 neighbors of the pixel having the same color. There are several
modifications of the above pattern that can be achieved by shifting
the pattern one pixel left and/or one pixel up. The two letters
C_1 and
C_2 in the conversion constants CV_Bayer
C_1
C_2 2BGR
and CV_Bayer
C_1 C_2
2RGB
indicate the particular pattern
type. These are components from the second row, second and third
columns, respectively. For example, the above pattern has a very
popular "BG" type.