Convert RGB color image to grayscale.
3 次查看(过去 30 天)
显示 更早的评论
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
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
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.
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Convert Image Type 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!