rgb2gray

Converts an RGB image or a colormap to grayscale.

Category: Graphics > Images

Syntax

G = rgb2gray(RGB)

Converts a truecolor m-by-n-by-3 image to an m-by-n grayscale image.

newmap = rgb2gray(map)

Converts an m-by-3 colormap to a grayscale colormap.

Description

The gray level is 0.2989*R + 0.5870*G + 0.1140*B, which preserves luminance. For a uint8 input the result is rounded to an integer. A matrix with three columns is treated as a colormap, not an image.

Input arguments

RGB
Truecolor image of class uint8, uint16, single or double.
map
Colormap with three columns.

Output arguments

G
Grayscale image of the same class as the input, or a three-column grayscale colormap.

Example

Two pixels

RGB = zeros(1, 2, 3);
RGB(1, 1, :) = [1 0 0];
RGB(1, 2, :) = [0 1 0];
G = rgb2gray(RGB)
rgb2gray(uint8(RGB * 255))
Output
G = 1×2

    0.2989    0.5870
ans = 1×2 uint8 row vector

    76   150

See also

im2gray cmap2gray rgb2ind imshow