如何在不使用rgb2gray的情况下将RGB图像转换为Matlab中的灰度 [英] How to convert RGB images to grayscale in matlab without using rgb2gray

查看:92
本文介绍了如何在不使用rgb2gray的情况下将RGB图像转换为Matlab中的灰度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我当前正在使用代码:

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 x1.

It shows only a white screen. Also the newly generated dimensions are n x m x 3 whereas it should be only m x n x 1.

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

If I use mat2gray it display the image like this

推荐答案

由于图像是PNG,因此

Since the image is a PNG, imread() is returning an integer image, with intensity values in the range [0 255] or equivalent, depending on the original bit depth. The conversion formula makes a a double image, which is expected to have intensities in the range [0 1]. Since all the pixel values in a are probably much greater than 1, they get clipped to 1 (white) by imshow().

最好的选择是明确转换图像格式在您开始之前-这将确保正确缩放内容:

The best option is to explicitly convert the image format before you start - this will take care of scaling things correctly:

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);

这篇关于如何在不使用rgb2gray的情况下将RGB图像转换为Matlab中的灰度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆