Colormap and GIF images in Matlab - matlab

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!

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.

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.

Related

YCbCr to RGB conversion MATLAB using ycbr2rgb results in pink picture

I'm trying to convert an YCbCr image to RGB ysing MATLAB's function ycbcr2rgb. My resulting picture ends up being pink, and converting back again afterwards (should give me the original picture?) creates yet another image mostly grey.
For reference I tried to convert each channel individually by formula and it ends up the same.
I'm using a bigtiff format because of large filesize and if any help the imfinfo shows compression using JPEG.
Here is my code:
x=imread('picture.tiff','Index',9); %(9 subresolutions)
rgb=ycbcr2rgb(x);
imshow(rgb);
Can it be because of MATLABs function using the originial definition of YCbCr using ranges from 16-235 while my image is ranging from 0-255? If so is there any means of correcting this using the inbuild function?
I have added the pictures here, first image is showing imshow(rgb), while the second image is the original ycbcr. What I noticed is that in the Windows image viewer it actually shows it correct, it's just MATLAB's imshow that displays it pink after conversion.
Is there any chance you could point me in the right direction?
Thanks
Sonny
Apparently imread reads YCbCr images as RGB when loading it, which is why the problem occured.
Thanks for the help to all of you.
imread documentation
This link gives all the conversion formulae:
http://www.easyrgb.com/index.php?X=MATH&H=11
The below code converts image from RGB space to YCbCr space and back.
rgb = imread('board.tif');
imshow(rgb);
figure;
ycbcr = rgb2ycbcr(rgb);
imshow(ycbcr);
figure;
rgb2 = ycbcr2rgb(ycbcr);
imshow(rgb2);
Use MATLABs built in functions only. Also, if you're facing issues while converting from ycbcr to rgb you should probably try to convert the image to other form and then convert that form to RGB. (a dirty hack)
Just divide the image by 256 before converting it back to RGB.
y = ycbcr2rgb(z/256); % z holds the YCbCr image.
Worked for me.
Hope that helps :)

How to plot an HSV image with Matlab?

I'am interested in finding a way to plot an HSV image in Matlab. I know i can do it by converting it into RGB first but I want to figure out whether there is a direct way. Thanks.
Ther is the hsv2rgb function to convert an hsv image to rgb, in case you were about to convert the values yourself.
Since monitors work with rgb the image has to be converted at some place before display, so i think it doesn't really matter when this takes place. I don't know of any more direct matlab approach than using hsv2rgb and then show the image and i do not think there is one. (of course you could put those two into a user defined script if that helps making code more readable).

black&white to color image in Matlab

I am doing simple image processing in matlab. I turned my original image (jpg) into black and white image with the function im2bw and I did some modifications on this image. Do you know if it is possible to turn again this image to the original colors?
Grayscaling an image is a one-way function. Without the original data, you have no way of determining the hue of the color used before it was converted to grayscale, and so there is actually loss of data in the conversion.

Converting colour image to grey scale (Matlab)

I'm having some trouble converting a tif image into grey scale so I can have a look at the pixel values. I first used the code
im = imread('etc.');
im = rgb2gray(im);
imshow(im)
but this doesn't display grey scale values - it shows an RGB colour map. I've also tried loading the colour map to then convert the map
[im,map] = imread('etc.');
but the map is empty. Any pointers appreciated - I couldn't see anything useful in the Matlab help.
Edit: imshow results
There will not be a colour map in the file if it's a rgb image - only if it's an indexed image.
imshow uses a default colour map (jet). If you want a greyscale map, you have to deliberately set colormap gray.
im = rgb2gray(imread('filename'));
imshow(im)
colormap gray
I think with the .tif format you have to use the command
[X,map] = imread('imagename.tif')
newmap = rgb2gray(map)
imshow(X,newmap)
Link to online Matlab help where's this exact example: RGB to gray

How to store the images with colourmap in MATLAB

I am using HDF satellite data to retrieve bands from that I am concluding different vegetation indices. Every band in hdf data is in grey colour format, its a grey colour scale image. After HDF data processed I can convert into colour by using colour map (I am using jet for colourmap). My doubt is how to convert greyscale image into colourmaped while using imwrite. How to use colourmap within imwrite. I have tried many times, but the output is only in full blue colour, this spoil the output image. Please help me to do this.
Why use imwrite? You can use imshow.
Example:
imshow(im)
imshow(im,'Colormap',jet(255))
With reference: http://www.alecjacobson.com/weblog/?p=1655
Try using the ind2rgb function before using imwrite if you want to save to a format like .jpg, but if you are using an indexing image format (e.g. .png) you can just use imwrite directly as shown in the docs:
imwrite(X, map, filename)
where X is your greyscale image, map is your colourmap (i.e. jet) and filename is the is the name of the image you want to save ending in .png