MATLAB File Help: cv.cvtColor | Index |
Converts an image from one color space to another
dst = cv.cvtColor(src, code)
dst = cv.cvtColor(src, code, 'OptionName',optionValue, ...)
src
.src
and code
.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:
uint8
imagesuint16
imagessingle
and double
)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
to
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 cv.cvtColor, you need first to scale the image down:
img = cvtColor(img./255, 'BGR2Luv');
If you use cv.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 uint8
, 65535 for uint16
, and 1 for
single
.
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 = 0.299*R + 0.587*G + 0.114*B
and
Gray to RGB[A]: R = Y, G = Y, B = Y, A = max(ChannelRange)
The conversion from a RGB image to gray is done with:
bwsrc = cv.cvtColor(src, 'RGB2GRAY');
More advanced channel reordering can also be done with cv.mixChannels.
See also: 'BGR2GRAY', 'RGB2GRAY', 'GRAY2BGR', 'GRAY2RGB'
[X;Y;Z] = [0.412453, 0.357580, 0.180423;
0.212671, 0.715160, 0.072169;
0.019334, 0.119193, 0.950227] * [R;G;B]
[R;G;B] = [3.240479, -1.53715, -0.498535;
-0.969256, 1.875991, 0.041556;
0.055648, -0.204043, 1.057311] * [X;Y;Z]
X
, Y
, and Z
cover the whole value range (in case of floating-point
images, Z
may exceed 1).
See also: 'BGR2XYZ', 'RGB2XYZ', 'XYZ2BGR', 'XYZ2RGB'
Y = 0.299*R + 0.587G + 0.114B
Cb = (R-Y)*0.713 + delta
Cr = (B-Y)*0.564 + delta
R = Y + 1.403*(Cr-delta)
G = Y - 0.714*(Cr-delta) - 0.344*(Cb-delta)
B = Y + 1.773*(Cb-delta)
where
/ 128 for 8-bit images
delta = | 32768 for 16-bit images
\ 0.5 for floating-point images
Y
, Cr
, and Cb
cover the whole value range.
See also: 'BGR2YCrCb', 'RGB2YCrCb', 'YCrCb2BGR', 'YCrCb2RGB'
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(R,G,B)
S = / (V - min(R,G,B)) / V if V != 0
\ 0 otherwise
/ 60*(G-B) / (V - min(R,G,B)) if V=R
H = | 120 + 60*(B-R) / (V - min(R,G,B)) if V=G
\ 240 + 60*(R-G) / (V - min(R,G,B)) if V=B
If H<0
then H=H+360
. On output 0<=V<=1
, 0<=S<=1
, 0<=H<=360
.
The values are then converted to the destination data type:
V=255*V
, S=255*S
, H=H/2
(to fit to 0 to 255)V=65535*V
, S=65535*S
, H=H
H
, S
, and V
are left as isSee also: 'BGR2HSV', 'RGB2HSV', 'HSV2BGR', '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.
Vmax = max(R,G,B)
Vmin = min(R,G,B)
L = (Vmax + Vmin)/2
S = / (Vmax - Vmin)/(Vmax + Vmin) if L < 0.5
\ (Vmax - Vmin)/(2-(Vmax + Vmin)) if L >= 0.5
/ 60*(G-B) / (Vmax - Vmin) if Vmax=R
H = | 120 + 60*(B-R) / (Vmax - Vmin) if Vmax=G
\ 240 + 60*(R-G) / (Vmax - Vmin) if Vmax=B
If H<0
then H=H+360
. On output 0<=L<=1
, 0<=S<=1
, 0<=H<=360
.
The values are then converted to the destination data type:
L=255*L
, S=255*S
, H=H/2
(to fit to 0 to 255)L=65535*L
, S=65535*S
, H=H
H
, S
, and L
are left as isSee also: 'BGR2HLS', 'RGB2HLS', 'HLS2BGR', '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.
[X;Y;Z] = [0.412453, 0.357580, 0.180423;
0.212671, 0.715160, 0.072169;
0.019334, 0.119193, 0.950227] * [R;G;B]
X = X/Xn, where Xn = 0.950456
Z = Z/Zn, where Zn = 1.088754
L = / 116 * Y^(1/3) - 16 for Y > 0.008856
\ 903.3 * Y for Y <= 0.008856
a = 500*(f(X) - f(Y)) + delta
b = 200*(f(Y) - f(Z)) + delta
where
f(t) = / t^(1/3) for t < 0.008856
\ 7.787*t + 16/116 for t >= 0.008856
and
delta = / 128 for 8-bit images
\ 0 for floating-point images
This outputs 0<=L<=100
, -127<=a<=127
, -127<=b<=127
. The values are
then converted to the destination data type:
L=L*255/100
, a=a+128
, b=b+128
L
, a
, and b
are left as isSee also: 'BGR2Lab', 'RGB2Lab', 'Lab2BGR', '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 0 to 1 range.
[X;Y;Z] = [0.412453, 0.357580, 0.180423;
0.212671, 0.715160, 0.072169;
0.019334, 0.119193, 0.950227] * [R;G;B]
L = / 116 * Y^(1/3) - 16 for Y > 0.008856
\ 903.3 * Y for Y <= 0.008856
u' = 4*X/(X + 15*Y + 32*Z)
v' = 9*Y/(X + 15*Y + 32*Z)
u = 13*L*(u' - un), where un = 0.19793943
v = 13*L*(v' - vn), where vn = 0.46831096
This outputs 0<=L<=100
, -134<=u<=220
, -140<=v<=122
.
The values are then converted to the destination data type:
L=255/100*L
, u=255/354*(u+134),
v=255/262*(v+140)`L
, u
, and v
are left as isThe 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
See also: 'BGR2Luv', 'RGB2Luv', 'Luv2BGR', 'Luv2RGB'
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:
R G R G R
G (B) (G) B G
R G R G R
G B G B G
R G R G R
The output RGB components of a pixel are interpolated from 1, 2, or 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 C1
and C2
in
the conversion constants Bayer(C1)(C2)2BGR
and Bayer(C1)(C2)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.
See also: 'BayerBG2BGR', 'BayerGB2BGR', 'BayerRG2BGR', 'BayerGR2BGR', 'BayerBG2RGB', 'BayerGB2RGB', 'BayerRG2RGB', 'BayerGR2RGB'