I = rgb2gray(X) %RGB2GRAY Convert RGB image or colormap to grayscale. % RGB2GRAY converts RGB images to grayscale by eliminating the % hue and saturation information while retaining the % luminance. % % I = RGB2GRAY(RGB) converts the truecolor image RGB to the % grayscale intensity image I. % % NEWMAP = RGB2GRAY(MAP) returns a grayscale colormap % equivalent to MAP. % % Class Support % ------------- % If the input is an RGB image, it can be uint8, uint16, double, or % single. The output image I has the same class as the input image. If the % input is a colormap, the input and output colormaps are both of class % double. % % Notes % ----- % RGB2GRAY converts RGB values to grayscale values by forming a weighted % sum of the R, G, and B components: % % 0.2989 * R + 0.5870 * G + 0.1140 * 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 the following formula: % % 0.299 * R + 0.587 * G + 0.114 * B % % Example % ------- % I = imread('example.tif'); % % J = rgb2gray(I); % figure, imshow(I), figure, imshow(J); % % indImage = load('clown'); % gmap = rgb2gray(indImage.map); % figure, imshow(indImage.X,indImage.map), figure, imshow(indImage.X,gmap); % % See also RGB2IND, RGB2LIGHTNESS. % Copyright 1992-2018 The MathWorks, Inc.
时间: 2024-03-04 10:49:50 浏览: 138
gray2rgb.zip_gray2rgb_图像密度_着色 matlab
这段代码是 MATLAB 中的 `rgb2gray` 函数的说明文档。该函数用于将 RGB 彩色图像或颜色映射矩阵转换为灰度图像或灰度颜色映射矩阵。如果输入为 RGB 彩色图像,则该函数通过加权求和的方式将 R、G、B 三个通道转换为灰度值,具体的权重系数为 0.2989、0.5870 和 0.1140。如果输入为颜色映射矩阵,则该函数计算出与原始颜色映射矩阵对应的灰度颜色映射矩阵。
该函数支持的输入和输出格式包括 uint8、uint16、double 和 single 类型的 RGB 彩色图像,以及 double 类型的颜色映射矩阵。输出图像的类型与输入图像类型相同。在计算灰度值时,该函数使用的权重系数与 Rec.ITU-R BT.601-7 规范中用于计算亮度(luminance)的系数相同,经过舍入后保留 3 位小数。
阅读全文