rgb2gray - Convert RGB image or colormap to grayscale

Syntax

I = rgb2gray(RGB)
newmap = rgb2gray(map)

Description

I = rgb2gray(RGB) converts the truecolor image RGB to the grayscale intensity image I. rgb2gray converts RGB images to grayscale by eliminating the hue and saturation information while retaining the luminance.

newmap = rgb2gray(map) returns a grayscale colormap equivalent to map.

Class Support

If the input is an RGB image, it can be of class uint8, uint16, single, or double. The output image I is of the same class as the input image. If the input is a colormap, the input and output colormaps are both of class double.

Examples

Convert an RGB image to a grayscale image.

I = imread('board.tif');
J = rgb2gray(I);
figure, imshow(I), figure, imshow(J);

Convert the colormap to a grayscale colormap.

[X,map] = imread('trees.tif');
gmap = rgb2gray(map);
figure, imshow(X,map), figure, imshow(X,gmap);

Algorithm

rgb2gray converts RGB values to grayscale values by forming a weighted sum of the R, G, and B components:

0.2989 * R + 0.5870 * G + 0.1140 * B 

Note that these are the same weights used by the rgb2ntsc function to compute the Y component.

See Also

ind2gray, ntsc2rgb, rgb2ind, rgb2ntsc

  


 © 1984-2008- The MathWorks, Inc.    -   Site Help   -   Patents   -   Trademarks   -   Privacy Policy   -   Preventing Piracy   -   RSS