Converting colour image to grey scale (Matlab) - 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

Related

Creating a binary image

clear all;
clc;
imag = imread('286502.png');
image_binary = im2bw(imag,0.85); %converte image to binary
image_binary = not(image_binary);
figure(1);clf
imagesc(image_binary);colormap(gray)
I am using this code to genarate a binary image of an ellipse having its inside as white while outside as black.But , the problem i am facing is that whenever the foreground and background of my input ellipse is either light or dark then the binary image becomes all black or all white.
if you want to keep the gray-scale nature of our image, you may remove the line im2bw command because that flattens the gray-scale values into binary (0-1 or white/black).
if you just want to show a binary image in gray color, you can try customizing the colormap
colormap([0 0 0;0.5 0.5 0.5]);
im2bw(imag,0.85) thresholds at a fixed value, which will work for some images but not for others. I recommend that you binarize with a method such as Otsu, which determines an optimal threshold for each image individually.
image_binary = imbinarize(imag);

How can I display a grayscale raster in color in Matlab?

I have a .tif file of a landmass that denotes elevation. I want to display this raster with a color ramp as opposed to a grayscale ramp. How would I do this in Matlab?
I looked at the information associated with the tiff using:
[Z, R] = geotiffread('Landmass.tif')
which denotes the heading 'ColourType' as 'grayscale'. I tried to change this to 'winter' (one of matlabs in-built color schemes) but it made no difference.
At the moment I am using the following commands to display the tiff:
[Z, R] = geotiffread('Landmass.tif');
e=uint8(Z);
mapshow(e,R);
All the higher areas are white and everything else is black...even around the landmass (which I think I may have to cut/mask the landmass out to get rid of).
All the black colour is making it too difficult for me to display other shapefiles on top of the tiff, so I want to change the color scheme from grayscale to something lighter.
How do I do this?
The reason colormap winter is not working is because the output of mapshow(e,R); is RGB image format.
Even when the displayed image is gray, it is actually RGB, when r=g=b for each pixel.
I took Matlab mapshow example, converted boston image to Grayscale, and used mapshow.
For using colormap winter, I got image using getimage, convert it to Grayscale using rgb2gray, and then colormap winter worked when showing the image.
Check the following example:
[boston, R] = geotiffread('boston.tif');
boston = rgb2gray(boston); %Convert to Grayscale for testing.
figure
mapshow(boston, R);
axis image off
%Get image data, note: size of I is 2881x4481x3 (I is not in Grayscale format).
I = getimage(gca);
%Convert I from RGB (R=G=B) formtat to Grayscale foramt, note: size of J is
%2881x4481 (J is Grayscale format).
%%%%%%%Avoid image being rotated%%%%%%%%%%%%%
%Close old image and open new figure
close Figure 1
Figure
J = rgb2gray(I);
imshow(J);
colormap winter %Now it's working...
Boston with winter colormap:

Colormap and GIF images in 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.

imshow() displays a white image for a grey image

I have computed an image with values between 0 and 255. When I use imageview(), the image is correctly displayed, in grey levels, but when I want to save this image or display it with imshow, I have a white image, or sometimes some black pixels here and there:
Whereas with imageview():
Can some one help me?
I think that you should use imshow(uint8(image)); on the image before displaying it.
Matlab expects images of type double to be in the 0..1 range and images that are uint8 in the 0..255 range. You can convert the range yourself (but change values in the process), do an explicit cast (and potentially loose precision) or instruct Matlab to use the minimum and maximum value found in the image matrix as the white and black value to scale to when visualising.
See the following example with an uint8 image present in Matlab:
im = imread('moon.tif');
figure; imshow(im);
figure; imshow(double(im));
figure; imshow(double(im), []);
figure; imshow(im2double(im));

Display transparent image on axes (MATLAB)

So I have this transparent image (http://imgur.com/fyqslAx) that I want to display on an axes in MATLAB with its transperancy. To do this, I used the code below, which works with other transparent png images that I have:
[A, map, alpha] = imread('fyqslAx.png');
h = imshow(A, map)
set(h, 'AlphaData', alpha);
This code however, does not seem to work with the image above. Im guessing this is because it has something to do with the image being grayscale and having a bitdepth of 1, resulting in the map and alpha having nothing in it (whereas the other png transparent images I have, have something in map and alpha). If I just use this:
A = imread('fyqslAx.png');
h = imshow(A)
A black background appears where the image should be transparent.
How do I display this http://imgur.com/fyqslAx with its transperancy on an axes?
EDIT: horchler's method worked; Thanks!!
If you run imfinfo('fyqslAx.png'), you'll see that the 'Transparency' is specified as 'simple' and the 'SimpleTransparencyData' is set to 0 (false). I can't find documentation on this, but I think that this may indicate that the alpha channel has been compressed into the image data because the the image is binary and grayscale. Effectively the image is a binary mask, either transparent or not. You can display your image like this:
A = imread('fyqslAx.png');
h = imshow(A);
set(h, 'AlphaData', A);
If you try to return a colormap and/or alpha channel from imread using extra output arguments, you'll see that both are empty.