need a grayscale non-indexed image

5 views (last 30 days)
Hi all!
I'm trying to convert some images to gray scale.
I first used rgb2gray(); But while checking results using info=iminfo(image_name) I found that info.ColorType='indexed'
So I tried I=ind2gray(image_name,map) where map was obtained from info.Colormap but I got I=0
My guess is that I put the wrong colormap. What is the right one to put???
In general, how do I get a grayscale non-indexed image????
Thanks!!!! Ilaria

Accepted Answer

Guillaume
Guillaume on 20 Jun 2015
rgb2gray and ind2gray work on an image in memory. They will both convert an image to greyscale exactly as you want.
imfinfo (I assume that's what you meant, there's no iminfo) works on an image on disk. You haven't shown us the step where you saved the image. This is probably where you've gone wrong. Most likely you've chosen an image format that do not support greyscale images. BMP for example can only store colour or indexed images so a greyscale image would have to be converted to one of these. Similarly, GIF only store images as indexed.
A format that can store greyscale (and coloured and indexed) images is PNG.
  3 Comments
Image Analyst
Image Analyst on 21 Jun 2015
Ilaria:
Like I suggested, the problem was with imwrite (the code you didn't show us) rather than rgb2gray (the code you did show us). But like I explained below even though you saved it as BMP, you must have saved it with a colormap.
It's not true that BMP can save only color or indexed images. BMP can store monochrome images (see this reference ) but you have to do it correctly when you call imwrite(), and that is without saving a colormap. Want proof? See this demo where it saves it as a grayscale BMP.
grayImage = imread('cameraman.tif');
filename = 'deleteme.bmp';
% Correct way to save grayscale image.
imwrite(grayImage, filename);
% You did this:
%myColormap = gray(256);
%imwrite(grayImage, myColormap, filename); % saved indexed
% Recall and see what we get
recalledImage = imread(filename);
whos recalled
size(recalledImage)
delete(filename); % Delete temporary image.
See, it recalls as grayscale, not indexed and not color.
That said, whenever possible I use PNG format - I think it's pretty much become the standard for generic images.
Guillaume
Guillaume on 21 Jun 2015
Hi Image Analyst,
I stand by my statement that the BMP format does not have a greyscale format. I've written enough image decoding code in various languages to know the intricacies of most image formats.
As per the wikipedia link you've provided (see the pixel format section particularly), BMP supports different bit-per-pixel values (the biBitCount field of the BITMAPINFOHEADER). With 1, 2, 4, or 8 bit per pixels, the pixel values are always an index into the bmiColors table. With 16, 24, and 32 bit per pixels, the pixel values are always an rgb triplet (+alpha with 32 bits). The colour table for 1, 2, 4, 8 bpp is also always an RGB triplet or RGBA quadruplet. So no greyscale at all.
Matlab may well detect that the colour table is pure greyscale, and convert the image, but intrinsically, the BMP format does not support it.
Nowadays there is no point in using BMP at all. PNG is a lot more flexible and feature-full.

Sign in to comment.

More Answers (1)

Image Analyst
Image Analyst on 20 Jun 2015
Using
grayImage = rgb2gray(rgbImage);
will give you a gray scale image. I don't know what image_name is, but I suspect you saved a colormap with it when you called imwrite().
I don't think you understand what an indexed image is. An indexed image is just a single valued image, like a uint8 image. If you look at the image with a gray(256) colormap applied, it can look like a normal gray scale version of the image. You can also apply some crazy colormap - color or weird gray scales - and then it might look bizarre. All it means is that the value of the image is to be used as a row index into a colormap so that everywhere you see that value, replace it with the color in the colormap. Now if you call rgb2ind() and tell it to do, like, 8 colors, then the indexed image will have only 8 values, and the colormap will have 8 rows. That image will most likely not look anything like a gray scale version of your color image. But what you call a non-indexed gray scale image could be an indexed image - all you have to do is to display it with a colormap applied and voila! - it's an indexed image even though you didn't change anything in the image itself. So a single-valued image can or cannot be an indexed image - it's just a matter of how you consider it and use or display the gray levels. For example
rgbImage = imread('peppers.png');
grayImage = rgb2gray(rgbImage);
% grayImage can be thought of as a non-indexed image.
% Apply the jet colormap
thisColorMap = jet(256);
colormap(thisColorMap);
% Ta-da! grayImage is now an indexed image because of how it's being used,
% even though I didn't make any actual changes to the grayImage variable.

Community Treasure Hunt

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

Start Hunting!