Why does "imread" generate 3 images?

2.9k Views Asked by At

The MATLAB help for imread says:

A = imread(filename, fmt)

The return value A is an array containing the image data. If the file contains a grayscale image, A is an M-by-N array. If the file contains a truecolor image, A is an M-by-N-by-3 array.

I thought it would generate 3 matrices for the RGB (one with the Red, one with the Green and one with the Blue), so I did:

img = imread('myimg')
size(img) % ans = 720 960 3
imshow(img) % the original image
imshow(img(:,:,1)) % print grayscaled image
imshow(img(:,:,2)) % print grayscaled image
imshow(img(:,:,3)) % print grayscaled image

And, as commented, the 3 images are just the original but grayscaled. So, why are 3 images generated?

(using Matlab R2011b)

2

There are 2 best solutions below

1
On BEST ANSWER

the 3 images are just the original but grayscaled"

Uh, no. There are not 3 images, there is a single image represented as a 3D matrix, or, a stack of 3 matrices, one for each channel (R,G,B). When you do img(:,:,1) you are extracting the matrix corresponding to the red channel, and when you call imshow(img(:,:,1)) you tell Matlab to interpret that matrix as an image (and because that matrix has only one channel, it interprets it as a gray scale image). The three calls to imgshow should then show each channel as a grayscale image. They should not be equal, though; to be sure of that, use some image that has eg a pure red corner: that would display as white in the first channel, black in the others.

This question is more apt for stackoverflow, though.

0
On

Each entry in img(:,:,1) tells you something like how much energy was measured at that pixel when filtering for red light during the imaging process. A purely red object will give a strong response in the red channel and zero response in the green and blue channels. Check out this wikipedia article. "Notice how the grey trees have similar brightness in all channels, the red dress is much brighter in the red channel than in the other two, and how the green part of the picture is shown much brighter in the green channel."