Documentation Center

  • Trials
  • Product Updates

rgb2gray

Convert RGB image or colormap to grayscale

Syntax

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

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.

gpuarrayB = rgb2gray(gpuarrayA) performs the operation on a GPU. gpuarrayA can be either an RGB image or a colormap. gpuarrayB is a gpuArray of the same type as gpuarrayA. This syntax requires the Parallel Computing Toolbox™

    Note:   A grayscale image is also called a gray-scale, gray scale, or gray-level image.

Class Support

If I 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 I is a colormap, the input and output colormaps are both of class double.

If gpuarrayA is an RGB gpuArray image, it can be uint8, uint16, single, or double. The output gpuArray image gpuarrayB has the same class as the input image. If gpuarrayA is a colormap, the input and output colormaps are gpuArrays 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 an RGB image to a grayscale image on a GPU.

I = gpuArray(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);

Convert the colormap to a grayscale colormap on a GPU. Note how the example pass the colormap to the gpuArray function.

[X,map] = imread('trees.tif');

% Pass colormap to gpuArray
gmap = rgb2gray(gpuArray(map));
figure, imshow(X,map), figure, imshow(X,gmap);

More About

expand all

Algorithms

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

| | | | |

Was this topic helpful?