How to convert RGB images to grayscale in matlab without using rgb2gray

匿名 (未验证) 提交于 2019-12-03 02:30:02

问题:

I'm currently using code:

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

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.

If I use mat2gray it display the image like this

回答1:

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


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!