Documentation

rgb2gray

Convert RGB image or colormap to grayscale

Syntax

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

Description

example

I= rgb2gray(RGB)converts the truecolor imageRGBto the grayscale intensity imageI. Thergb2grayfunction converts RGB images to grayscale by eliminating the hue and saturation information while retaining the luminance. If you have Parallel Computing Toolbox™ installed,rgb2graycan perform this conversion on a GPU.

example

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

Examples

collapse all

Read and display an RGB image, and then convert it to grayscale.

Read the sample file,peppers.png, and display the RGB image.

RGB = imread('peppers.png'); imshow(RGB)

Convert the RGB image to a grayscale image and display it.

I = rgb2gray(RGB); figure imshow(I)

Read an indexed image with an RGB colormap. Then, convert the colormap to grayscale.

Read the sample file,corn.tif, which is an indexed image with an RGB colormap.

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

Display the image.

imshow(X,map)

Convert the RGB colormap to a grayscale colormap and redisplay the image.

newmap = rgb2gray(map); imshow(X,newmap)

Input Arguments

collapse all

Truecolor image, specified as 3-D numeric array.

If you have Parallel Computing Toolbox installed,RGBcan also be a gpuArray.

Data Types:single|double|uint8|uint16

Colormap, specified as anm-by-3 numeric array.

If you have Parallel Computing Toolbox installed,mapcan also be a gpuArray.

Data Types:double

Output Arguments

collapse all

Grayscale image, returned as a numeric array.

If you have Parallel Computing Toolbox installed, thenIcan also be a gpuArray.

Grayscale colormap, returned as anm-by-3 numeric array.

If you have Parallel Computing Toolbox installed, thennewmapcan also be a gpuArray.

Tips

  • rgb2graysupports the generation of C code usingMATLAB®Coder™.

Algorithms

rgb2grayconverts 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 thergb2ntscfunction to compute theYcomponent.

Extended Capabilities

C/C++ Code Generation
Generate C and C++ code using MATLAB® Coder™.

Was this topic helpful?