How to display grayscale image using imshow function
Show older comments
Why the below code not showing grayscale image
f = imread('lena.bmp');
imshow(f, [0 80])
I'm reading a book Digital Image Processing using Matlab, they have discussed the below syntax to show grayscale image.
imshow(f, [low high])
displays as black all values less than or equal to low, and as white all values greater than or equal to high
I'm using Matlab R2015a
1 Comment
Thorsten
on 21 Oct 2015
This code works fine for me (R2012a). What are you getting?
Answers (2)
The problem is that your f (what a bad variable name for an image!) is not a greyscale image as greyscale is not supported by the 'bmp' format. It is an RGB image that may happen to have all three channels identical, so it appear grey. A true greyscale image has only one colour channel.
imshow(f(:, :, 1), [0 80]) %since all three channels should be identical
%or
imshow(rgb2gray(f), [0 80]) %works even if all 3 channels are not identical, but then the original image is not greyscale
imshow with a RGB image appears to ignore the 'DisplayRange' argument.
4 Comments
That's not true, bmp (unlike jpg, if I recall correctly) supports a single color plane, i.e., gray-scale.
>> I = imread('http://www.ece.rice.edu/~wakin/images/lena512.bmp');
>> whos I
Name Size Bytes Class Attributes
I 512x512 262144 uint8
Guillaume
on 21 Oct 2015
Yes, I was a bit wrong with my statement. BMP does support a single colour plane. Strictly speaking, it's an indexed image though (you just ignored the palette in your imread call). For BMP with a bit-per-pixel <= 8, the pixel values are always indices in the colour palette (which itself is always RGB encoded), for bit-per-pixel > 8, the pixel values are always RGB.
To go back to the OP question, if imshow ignores the DisplayRange argument, it's because the image is not greyscale but RGB. So, most likely, his lena image has been saved as RGB, and my solution still stand.
To know for sure:
imfinfo('lena.bmp')
whos f
By the way, it's why I always recommend using PNG as an image format. The PNG format support all three types of images (indexed, greyscale, rgb), plus optional transparency, plus up to 16 bit per channel, plus storage of metadata. And it's got fast non-lossy compression to boot.
Atinesh S
on 24 Oct 2015
Atinesh S
on 24 Oct 2015
4 Comments
Walter Roberson
on 24 Oct 2015
jpg images are always RGB images.
Atinesh S
on 24 Oct 2015
Guillaume
on 24 Oct 2015
You have to differentiate between images in memory and image file format. You can have a greyscale image in memory but if you save it to BMP it has to be converted either to indexed or rgb. In JPG, it's always RGB. Regardless, IF you know the image is grayscale, you can simply load it and convert the rgb/indexed image back to grayscale with no loss of data. With indexed, just discard the colour map, with rgb just discard two of the channels.
There are not many image formats that will store an image as greyscale. As mentioned in my answer, PNG is one of them, and the one I'd recommend. TIF is another (I think, that format is a mess).
Walter Roberson
on 25 Oct 2015
With indexed, do not just discard the colormap: the index is not necessarily in order of grayscale level. You should instead use ind2gray()
Categories
Find more on Image Processing Toolbox 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!