LabKit MATLAB Workbench

reference

API ReferenceImage API

labkit.image.rgb2gray

Convert RGB data using MATLAB's rgb2gray call contract.

Syntax

gray = labkit.image.rgb2gray(rgb)

Description

Converts an RGB image with the ITU-R BT.601 luma coefficients 0.2989, 0.5870, and 0.1140 for red, green, and blue. The output retains the input numeric class. Integer results therefore use MATLAB's normal cast and rounding behavior.

For an M-by-3 colormap, each row is converted to one luma value and that value is repeated across three columns. This preserves the colormap shape while removing color. The function does not clamp floating-point input.

Inputs

rgb
Real, nonsparse M-by-N-by-3 RGB image or M-by-3 colormap of class uint8, uint16, int16, single, or double.

Outputs

gray
M-by-N grayscale image or M-by-3 grayscale colormap with the same numeric class as rgb.

Errors

labkit:image:rgb2gray:InvalidShape
rgb is neither an RGB image nor an M-by-3 colormap.

Example

rgb = cat(3, ones(2), zeros(2), zeros(2));
gray = labkit.image.rgb2gray(rgb);

Source

This page is generated from the MATLAB help text in +labkit/+image/rgb2gray.m.