rgb2gray - Convert RGB image or colormap to grayscale - MATLAB (original) (raw)

Convert RGB image or colormap to grayscale

Syntax

Description

[I](#buiz8mj-1-I) = rgb2gray([RGB](#buiz8mj-1-RGB)) converts the truecolor image RGB to the grayscale imageI. 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.

example

[newmap](#buiz8mj-1-newmap) = rgb2gray([map](#buiz8mj-1-map)) returns a grayscale colormap equivalent to map.

example

Examples

collapse all

Read a sample RGB image, then display the image.

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

Figure contains an axes object. The hidden axes object contains an object of type image.

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

I = rgb2gray(RGB); imshow(I)

Figure contains an axes object. The hidden axes object contains an object of type image.

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

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

Display the indexed image with the RGB colormap.

Figure contains an axes object. The hidden axes object contains an object of type image.

Convert the RGB colormap to a grayscale colormap.

Display the indexed image with the grayscale colormap.

Figure contains an axes object. The hidden axes object contains an object of type image.

Input Arguments

collapse all

Truecolor image, specified as an_m_-by-_n_-by-3 numeric array.

The rgb2gray function expects truecolor images of data type double and single to have values in the range [0, 1]. If an image has values outside the range [0, 1], then you can rescale values to the expected range by using the rescale function.

If you have Parallel Computing Toolbox installed, RGB can also be agpuArray object.

Data Types: single | double | uint8 | uint16

Colormap, specified as a _c_-by-3 numeric matrix with values in the range [0, 1]. Each row of map is a three-element RGB triplet that specifies the red, green, and blue components of a single color of the colormap.

If you have Parallel Computing Toolbox installed, map can also be agpuArray object.

Data Types: double

Output Arguments

collapse all

Grayscale image, returned as an_m_-by-n numeric array.

If you have Parallel Computing Toolbox installed, then I can also be agpuArray object.

Grayscale colormap, returned as an _c_-by-3 numeric matrix with values in the range [0, 1]. The three columns ofnewmap are identical, so that each row ofmap specifies a single intensity value.

If you have Parallel Computing Toolbox installed, then newmap can also be agpuArray object.

Data Types: double

Tips

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

Extended Capabilities

expand all

When generating code, if you choose the genericMATLAB Host Computer target platform,rgb2gray generates code that uses a precompiled, platform-specific shared library. Use of a shared library preserves performance optimizations but limits the target platforms for which code can be generated.

The rgb2gray function fully supports GPU arrays. To run the function on a GPU, specify the input data as a gpuArray (Parallel Computing Toolbox). For more information, see Run MATLAB Functions on a GPU (Parallel Computing Toolbox).

Version History

Introduced before R2006a

expand all

Previously, rgb2gray required Image Processing Toolbox™.