Colormap and GIF images in Matlab

2019-08-13 12:14发布

问题:

I have a problem understanding colormaps in Matlab and using them to import and diplay .gif images.

I would like to import an image using im = imread('I.gif') and then display it using imshow(im) but the result is wrong

If I do [im,map] = imread('I.gif') and then display it using imshow(im,map) it works properly, but still I don't understand the need of this colormap

Is there a way to import and convert my gif image to greyscale so that when I do imshow(im) it shows the correct greyscale image without having to worry about the colormap?

SOrry for the noob question but I am just starting with image processing in Matlab and I would really appreciate some help. It is my first question! :)

Bye and thanks!

回答1:

If you want to convert your gif to grayscale, use ind2gray:

[im,map] = imread('I.gif');
imGray = ind2gray(im,map);

The reason that you need the colormap is that the gif format doesn't store image intensities, it stores indices into the colormap. So color 0 could be red or green or a very light shade of mauve. It's the colormap that stores the actual RGB colors that the image needs. ind2gray will take each of those colors, convert them to a grayscale intensity, and replace the indices in the image with those intensities.



回答2:

It looks like you've really already answered the question. It seems that .gif files support an indexed color format, as a way of saving space. See:

https://en.wikipedia.org/wiki/Indexed_color

This is different than a more typical RGB color, which is what is often received from an IMREAD call.

To convert to a grayscale, you would need to go through the colormap and assign a grayscale value to each color, and then substitute those values back into the im variable.