Convert RGB color image to grayscale.

3 visualizzazioni (ultimi 30 giorni)
Oah Joan
Oah Joan il 16 Nov 2018
Modificato: John Kelly il 15 Gen 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 Commenti
Stephen23
Stephen23 il 20 Nov 2020
Modificato: John Kelly il 15 Gen 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 il 15 Gen 2021

(Answers Dev) Restored edit

Accedi per commentare.

Risposte (1)

Image Analyst
Image Analyst il 16 Nov 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.

Categorie

Scopri di più su Convert Image Type in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by