Search code examples
matlabcolorstiffraster

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?


Solution

  • 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:

    enter image description here