rgb2gray
Convert RGB image or colormap to grayscale
Description
converts the truecolor image I
= rgb2gray(RGB
)RGB
to the grayscale image
I
. The rgb2gray
function converts RGB
images to grayscale by eliminating the hue and saturation information while
retaining the luminance. If you have Parallel Computing Toolbox™ installed, rgb2gray
can perform this conversion
on a GPU.
Examples
Input Arguments
Output Arguments
Tips
If the input image is a grayscale image, then the
rgb2gray
function returns an error. To avoid an error, you can use theim2gray
function instead. Theim2gray
function is identical torgb2gray
except that it can accept grayscale images as inputs, returning them unmodified. If you use theim2gray
function, code like this conditional statement is no longer necessary.if ndims(I) == 3 I = rgb2gray(I); end
Algorithms
rgb2gray
converts RGB values to grayscale values by forming a
weighted sum of the R, G, and B
components:
0.298936021293775 * R + 0.587043074451121 * G + 0.114020904255103 * B
The coefficients used to calculate grayscale values in rgb2gray
are identical to those used to calculate luminance (E'y) in Rec.ITU-R BT.601-7 after
rounding to 3 decimal places. Rec.ITU-R BT.601-7 calculates E'y using this
formula:
0.299 * R + 0.587 * G + 0.114 * B