Convert RGB color image to grayscale.

5 次查看(过去 30 天)
Oah Joan
Oah Joan 2018-11-16
编辑: John Kelly 2021-1-15
How do I write a function rgb2luma(colourimage) that converts an RGB colour image to a grayscale one based on “luma” defined as Y = 0.2126 R + 0.7152 G + 0.0722 B. I also want to normalize the result so that Y is in the range [0 1].
  2 个评论
Stephen23
Stephen23 2020-11-20
编辑:John Kelly 2021-1-15
Original question on 16 Nov 2018
Convert RGB color image to grayscale.
How do I write a function rgb2luma(colourimage) that converts an RGB colour image to a grayscale one based on “luma” defined as Y = 0.2126 R + 0.7152 G + 0.0722 B. I also want to normalize the result so that Y is in the range [0 1].

请先登录,再进行评论。

回答(1 个)

Image Analyst
Image Analyst 2018-11-16
Try this:
% Sample call
% colourimage = imread('peppers.png');
% Y = rgb2luma(colourimage);
% imshow(Y);
function Y = rgb2luma(colourimage)
R = double(colourimage(:, :, 1)); % Extract red channel.
G = double(colourimage(:, :, 2)); % Extract green channel
B = double(colourimage(:, :, 3)); % Extract blue channel.
Y = 0.2126 * R + 0.7152 * G + 0.0722 * B;
Y = Y / max(Y(:));
end
Note that if you use mat2gray() to normalize the Y, it maps the min Y value to 0 and you will NOT have luminance anymore.

类别

Help CenterFile Exchange 中查找有关 Convert Image Type 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by