Convert RGB image to grayscale
converts the specified truecolor imageI
= im2gray(RGB
)RGB
to a grayscale intensity imageI
. Theim2gray
function accepts grayscale images as inputs and returns them unmodified.
Theim2gray
function converts RGB images to grayscale by eliminating the hue and saturation information while retaining the luminance.
Theim2gray
function is identical torgb2gray
except that it can accept grayscale images as inputs, returning them unmodified. Thergb2gray
function returns an error if the input image is a grayscale image. If you use theim2gray
function, code like this loop is no longer necessary.
if ndims(I) == 3 I = rgb2gray(I); end
Unlike thergb2gray
function, theim2gray
function does not accept colormaps as an input. To convert a colormap to grayscale, use thecmap2gray
function.
Theim2gray
function converts RGB values to grayscale values by forming a weighted sum of theR,G, andBcomponents:
0.2989 * 0.5870 R + * G + 0.1140 * B
These are the same weights used by thergb2ntsc
(Image Processing Toolbox)function to compute theYcomponent.
The coefficients used to calculate grayscale values in theim2gray
用来计算函数是相同的luminance (E'y) in Rec.ITU-R BT.601-7 after rounding to three decimal places.
Rec.ITU-R BT.601-7 calculates E'y using the following formula:
0.299 * R + 0.587 * G + 0.114 * B
ind2gray
(Image Processing Toolbox)|rgb2gray
|mat2gray
(Image Processing Toolbox)|ntsc2rgb
(Image Processing Toolbox)|rgb2ind
|rgb2ntsc
(Image Processing Toolbox)|cmap2gray