gpt4 book ai didi

matlab - 如何在不使用 rgb2gray 的情况下在 matlab 中将 RGB 图像转换为灰度

转载 作者:太空宇宙 更新时间:2023-11-03 20:00:03 24 4
gpt4 key购买 nike

我目前正在使用代码:

i = imread('/usr/share/icons/matlab.png');
for k=1:1:m
for l=1:1:n
%a(k,l)=m*n;
a(k,l) = (.299*i(k,l,1))+(.587*i(k,l,2))+(.114*i(k,l,3));
end
end
imshow(a);

它只显示一个白屏。此外,新生成的尺寸为 n x m x 3,而它应该仅为 m x n x 1。

如果我使用 mat2gray,它会显示这样的图像

最佳答案

由于图像是 PNG,imread() is returning an integer image ,强度值在 [0 255] 或等效范围内,具体取决于原始位深度。转换公式使 a 成为 double 图像,其强度预计在 [0 1] 范围内。由于 a 中的所有像素值可能都远大于 1,因此它们会被 imshow() 裁剪为 1(白色)。

最好的选择是explicitly convert the image format在你开始之前——这将负责正确地缩放事物:

i = imread('/usr/share/icons/matlab.png');
i = im2double(i);
a = .299*i(:,:,1) + .587*i(:,:,2) + .114*i(:,:,3); % no need for loops
imshow(a);

关于matlab - 如何在不使用 rgb2gray 的情况下在 matlab 中将 RGB 图像转换为灰度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21513325/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com