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

matlab

Solution

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

Problem

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

Original source