Convert RGB color image to grayscale.

5 visualizaciones (últimos 30 días)
Oah Joan
Oah Joan el 16 de Nov. de 2018
Editada: John Kelly el 15 de En. de 2021
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 comentarios
Stephen23
Stephen23 el 20 de Nov. de 2020
Editada: John Kelly el 15 de En. de 2021
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 el 15 de En. de 2021

(Answers Dev) Restored edit

Iniciar sesión para comentar.

Respuestas (1)

Image Analyst
Image Analyst el 16 de Nov. de 2018
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.

Categorías

Más información sobre Convert Image Type en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by