Why does 'imread' reads grayscale images as two-dimensional

According to matlab documentation imread does the following: "If the file contains a grayscale intensity image, A is a two-dimensional array", but I simply don't get what the second dimension means.
As far as I can see the second dimension just stores 255 value, but what is this supposed to mean?

 Accepted Answer

Copied from comments above with minor edits:
It's actually allowed for TIFFs to have more planes than one would normally think. For example, an RGB TIFF could have a 4th layer for opacity or alpha, just so long as the first 3 layers are R, G, and B. It would also be required in that case (according to the TIFF spec) for the TIFF to define another tag called 'ExtraSamples' that accurately describes that 4th layer, either as "associated alpha", "unassociated alpha", or a 3rd catch-all "unspecified".
The ExtraSamples tag is present in this particular file, although it has an incorrect value. Basically the only interpretation would be for the first layer to be considered the "real" grayscale part of the image, and that 2nd layer as unassociated alpha (read the TIFF spec, section 18, for an explanation of unassociated alpha).

More Answers (2)

The first dimension is rows. The second dimension is columns.
In a "true-color" image, the third dimension would be the color planes, such as Red, Green, Blue, or HSV, or CMYK.

6 Comments

I think I made myself misunderstood, I'm talking about two planes in a grayscale image (*<51x512x2 uint8>*), the first plane shows the grayscale values but the second has only 255 values in it (white color). I don't know what is this meant for.
For example the
Which document are you examining?
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. For TIFF files containing color images that use the CMYK color space, A is an M-by-N-by-4 array
Which version are you using? What kind of file are you reading?
I have never seen the result you are mentioning.
I read it here http://www.weizmann.ac.il/matlab/techdoc/ref/imread.html, I'm using Matlab r2009a and I'm working with a tiff picture. The two-dimensional array in the documentation maybe is just width and height like some others said, but I'm also getting a second plane. The specific image I'm working with was found here http://www.ece.utk.edu/~gonzalez/ipweb2e/downloads/standard_test_images/standard_test_images.zip and is called 'lake.tif'. The result of imread is a 51x512x2 uint8 array
"two-dimensional" in that document means M x N, not two bit planes.
imfinfo() indicates that particular image, lake.tif, has a bitdepth of 16. 16 bit grayscale TIFF images are not supported by imread().
The TIFF class reports that there are problems with the TIFF header, and interprets the file as 8 bit.
The details reported by imfinfo() hint that the file might be a 16 bit TIFF image that was stored something like the first 8 bits are in one bit plane and the second 8 bits are in a second bit plane. The all-255 result for the second bit plane hints that there is not actually any useful information in that second bit plane, and that you should simply extract the first bitplane, A(:,:,1)
Thanks Walter, in fact is what I did to handle that image. I just wanted to know what that second layer means. Would you mind posting your comment as the answer? or do you think John's answer(in below comment) is more accurated?
John's answer sounds good to me.

Sign in to comment.

A two dimensional image has two independent coordinates that define the location, and a value that the image has at that location. For example, you might want to look at row 100, column 200. That is two dimensions. Now the image there will have a value, say it's 255 or 123 or 42 or whatever, but the fact that is has a value does not change the fact that it's still two dimensions. There are two independent variables (row and column) - doesn't that mean 2D in your book? Having a value doesn't make it a 3D image because there are not 3 independent values you can specify. Once you've specified two, the value is determined.
Look at a simpler example, a curve y = x^2. Just because this traces out a curve on a flat 2D piece of paper, does not make it a 2 dimensional curve. It's still 1D. You can only specify the x value and then the y value is completely determined.

7 Comments

Yep I know that the two dimensions are width and height (or rows and columns) but I'm talking about a second plane. RGB images have 3 planes, one for each component color, however I don't get why grayscale images have 2 planes (or al least the one I'm working with).
Hi Jorge, it's actually allowed for TIFFs to have more planes than one would normally think. For example, an RGB TIFF could have a 4th layer for opacity or alpha, just so long as the first 3 layers are R, G, and B. It would also be required in that case (according to the TIFF spec) for the TIFF to define another tag called 'ExtraSamples' that accurately describes that 4th layer.
The ExtraSamples tag is present in your file, although it has an illegal value. Basically the only interpretation would be for the first layer to be considered the "real" grayscale part of the image, and that 2nd layer as unassociated alpha (read the TIFF spec, section 18, for an explanation of unassociated alpha).
Hope this makes sense.
Can't you also have a tiff stack, where 2 or more grayscale images are all stored in the same tiff file?
Yes, that is definitely more common, but the ExtraSamples tag IS part of the TIFF specification and allows one to tack on as many extra channels or planes as one wants, so long as the intent of the extra channels is made clear by the ExtraSamples tag. And it's really the only way to properly handle opacity.
A TIFF stack is handled by storing "subimages" if I understand correctly. That would show up quite differently. imread() by default reads only the first image.
Thanks guys, now I get a better idea of what's going on. Can you please move the comment as the anwer?
Only John can do that if his name is to be attached to the answer. I'd have to copy it to a new answer but then it would have my name attached to it.

Sign in to comment.

Categories

Find more on Convert Image Type in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!