im2gray

Converts an RGB image to grayscale, leaving grayscale images as they are.

Category: Graphics > Images

Syntax

G = im2gray(I)

Returns the luminance of an RGB image, or I itself when it is already 2-D.

Description

Each pixel becomes 0.2989*R + 0.5870*G + 0.1140*B, weights that follow how the eye sees brightness: green counts most and blue least. Unlike rgb2gray, im2gray accepts grayscale images, so it is safe to call without checking the input first.

Input arguments

I
RGB image (m-by-n-by-3) or grayscale image (m-by-n).

Output arguments

G
m-by-n grayscale image of the same class as I.

Example

Red and green pixels

I = uint8(cat(3, [255 0], [0 255], [0 0]));
G = im2gray(I)
Output
G = 1×2 uint8 row vector

    76   150

See also

rgb2gray cmap2gray im2double imshow