What is the actual ratio of color channels used in function rgb2gray?

2 ビュー (過去 30 日間)
Abinas
Abinas 2025 年 4 月 23 日
移動済み: Cris LaPierre 2025 年 4 月 23 日
I was curious to know the actual ratio of color channels used in rgb2gray function. The documentation mentioned about the ratio used in the function i.e.
(0.298936021293775 * R + 0.587043074451121 * G + 0.114020904255103 * B)
(0.299 * R + 0.587 * G + 0.114 * B)
To confirm it I wrote a script. Two gray image histogram are compared where one gray image is generated using inbuilt rgb2gray function and other gray image is generated using the mentioned ratio of color channels.
path = "OG/550_img_.png";
img = imread(path);
gray1 = rgb2gray(img);
gray2 = 0.298936021293775 * img(:,:,1) ...
+ 0.587043074451121 * img(:,:,2) ...
+ 0.114020904255103 * img(:,:,3) ;
plot(imhist(gray1),color='r');
hold on;
plot(imhist(gray2),Color='b');
As you can can see from the figure the histogram is not matching. So what is the actual ratio used by rgb2gray function?

採用された回答

Constantino Carlos Reyes-Aldasoro
Constantino Carlos Reyes-Aldasoro 2025 年 4 月 23 日
移動済み: Cris LaPierre 2025 年 4 月 23 日
This is interesting, it looks like rgb2gray is rounding or filtering in some way.
  1 件のコメント
Abinas
Abinas 2025 年 4 月 23 日
移動済み: Cris LaPierre 2025 年 4 月 23 日
You are right. I tried the following.
path = "OG/550_img_.png";
img = imread(path);
gray1 = rgb2gray(img);
img = im2double(img); % new
gray2 = 0.298936021293775 * img(:,:,1) ...
+ 0.587043074451121 * img(:,:,2) ...
+ 0.114020904255103 * img(:,:,3) ;
gray2 = im2uint8(gray2); % new
plot(imhist(gray1),color='r');
hold on;
plot(imhist(gray2),Color='b');
And now the histogram matches exactly. Thank you.

サインインしてコメントする。

その他の回答 (0 件)

製品


リリース

R2024b

Community Treasure Hunt

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

Start Hunting!

Translated by