Convert RGB color image to grayscale.

조회 수: 3 (최근 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].
Rena Berman
Rena Berman 2021년 1월 15일

(Answers Dev) Restored edit

댓글을 달려면 로그인하십시오.

답변 (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