Skip to Main Content Skip to Search
File Exchange
MATLAB Newsgroup
Link Exchange
  Blogs  
 Contest 
MathWorks.com

Loren on the Art of MATLAB

January 10th, 2007

Colormap Manipulations

We use color as a dimension to understand the world. For some datasets, judicious choice of colormaps provides a useful means for gleaning information.

Contents

Basic Colormap Manipulation

Let's start with a grayscale image from the Image Processing Toolbox and display it using imshow.

imshow cameraman.tif

and let's take a quick look at the colormap

cm = get(1,'colormap');
figure
rgbplot(cm)
legend(get(gca,'children'),{'blue' 'green' 'red'},'location','NorthWest')

or superpose a colorbar on the image plot.

figure(1)
colorbar SouthOutside

It's difficult at first to see much detail about the man's clothing. Using the MATLAB function brighten, we can change the colormap and see more detail in the darker regions.

brighten(.2)

Notice the extra detail around the buttons and glove when we brighten the colormap more (at the expense of the lighter background).

brighten(.4)

Now let's see what the individual channels look like.

cm = get(1,'colormap');
figure(2)
rgbplot(cm)
legend(get(gca,'children'),{'blue' 'green' 'red'},'location','NorthWest')
close(2)

Other "Simple" Colormap Manipulations

Colormaps contain are 3-column matrices with double values lying between 0 and 1. So what manipulations can we easily do to create new colormaps from existing ones? Here are some simple ones.

colorbar off
colormap(1-cm)

Get another monotonic colormap and see other colormap choices.

help pink
 PINK   Pastel shades of pink color map
    PINK(M) returns an M-by-3 matrix containing a "pink" colormap.
    PINK, by itself, is the same length as the current figure's
    colormap. If no figure exists, MATLAB creates one.
 
    For example, to reset the colormap of the current figure:
 
              colormap(pink)
 
    See also HSV, GRAY, HOT, COOL, BONE, COPPER, FLAG, 
    COLORMAP, RGBPLOT.

This is not quite the sepia tones for an old-fashioned photograph look, but it's getting close.

colormap(pink)
colormap(fliplr(pink))

Pitfalls

You can run into some pitfalls with colormaps. Here are a few to watch out for.

  • Using the wrong length colormap can lead to very poor results. This makes the display look totally wrong. Try this.
load clown
image(X),colormap(map)

Now make the colormap gray

colormap(gray(length(map)))

Next display the clown using an inappropriate colormap.

colormap(gray(256))
  • Colormap is not double or not in the correct range (between 0 and 1).
  • In MATLAB version 4, the default colormap was hsv. The hsv colormap starts and ends with red, making it is very hard to interpret colors as high or low.
close
image(1:64)
axis off
colormap(hsv(64))

Starting in MATLAB 5, the default colormap is jet.

colormap(jet)

Resources

Here are some resources for learning more about color and colormaps in MATLAB.

What additional resources on color would be useful? Let me know here.


Get the MATLAB code

Published with MATLAB® 7.3

10 Responses to “Colormap Manipulations”

  1. Gordon Saksena replied on :

    This page has a bunch of important color-related links

    http://www.stonesc.com/color/index.htm

    If you navigate via the other tabs, you will find notes related to a good course the site owner teaches.

  2. Ian replied on :

    What if you wanted to call the class method ‘get’ from another class? I have a number of different get methods (all stored in their particular @class_name folders), so I assumed that just passing the object would return the correct method ‘get’ (for the specific object). Maybe not?

  3. Jody Klymak replied on :

    Colormap are very important. It would be great to see better support for perceptual colormaps such as described at http://www.research.ibm.com/dx/proceedings/pravda/truevis.htm

    It would also be nice if more than one colormap could be used per figure. There is a fileexchange entry that does this quite well, but is resource intensive.

    Thanks, Jody

  4. Loren replied on :

    Ian-

    You can’t call a method from a class unless you pass in an object of that class.

    –Loren

  5. Loren replied on :

    Jody-

    Your request for multiple colormaps per figure is in our enhancement database and will be incorporated into a future release (not sure which one yet since that’s only a piece of what the team is working on).

    –Loren

  6. Wang replied on :

    Hey Loren:
    Do you know how to construct a sparse symbolic matrix, it seems sparse command doesn’t support Syms Data
    Thanks
    Wang

  7. Loren replied on :

    Wang-

    sparse only works on certain numeric types. Can you describe your use case for needing sparse with symbolic variables?

    –loren

  8. Wang replied on :

    Hey Loren:
    I just need to estimate the blur kernel of the images, as you know, if the image is m-by-n matrix, it’s blur matrix is m-by-n by m-by-n sparse matrix. If we don’t know the value of the blur kernel, we can only represent it as the symbolic format. Thank you very much.
    Wang

  9. Eric Dick replied on :

    Hi Loren,

    I’m interested in colormaps with respect to the imshow function. What are the limitations, if any, on the number of grayscale shades that can be displayed with imshow (ie. is it limited to 255 bins from 0 to 1) ?

    Thank You,

    -Eric

  10. Brendan Hannigan replied on :

    Hi Eric,

    The values of MATLAB colormaps must be in the range [0..1]. You can see if you try to set one otherwise you will get an error:

    close
    f = figure(’colormap’,[1.1 1.2 1])
    ??? Error using ==> figure
    All colormap intensities must be between 0 and 1
    >>

    As for the number of rows allowed in a grayscale colormap, it depends on the figure renderer. By default, figures displaying images use the ‘Painters’ renderer. This renderer has the limitation that it cannot correctly display images with colormaps with more than 256 entries in them. You can see this in the following example:

    close
    size = 300;
    im = repmat(1:size,size,1);
    him = image(im,’CDataMapping’,'direct’);
    set(gcf,’Colormap’,gray(size));
    colorbar southOutside
    >> get(gcf,’Renderer’)
    ans =
    painters
    >>

    Here you can see that the entries at the end of the colormap are not rendering correctly. This limitation may be removed in a future version of MATLAB.

    One solution is to change the figure’s renderer property to either ‘zbuffer’ or ‘opengl’, like this:

    close
    size = 300;
    im = repmat(1:size,size,1);
    him = image(im,’CDataMapping’,'direct’);
    set(gcf,’Colormap’,gray(size));
    colorbar southOutside
    set(gcf,’Renderer’,'zbuffer’)

    Now we see that the gradient goes smoothly all the way to the edge of the image, and the colormap entries beyond #256 are rendering correctly.

    If you are displaying indexed images using the IMSHOW function, we check for this issue and change the renderer to ‘zbuffer’ automatically when trying to display indexed images with long colormaps, like this:

    close
    size = 300;
    im = repmat(1:size,size,1);
    him = imshow(im,gray(size));
    colorbar southOutside
    get(gcf,’Renderer’)
    ans =
    zbuffer
    >>

    hope this helps!
    Brendan

Leave a Reply


Loren Shure works on design of the MATLAB language at The MathWorks. She writes here about once a week on MATLAB programming and related topics.

  • J.B. Brown: Ah, and I am at fault for simply testing collinearity with the origin in the example above.
  • J.B. Brown: Indeed, > collinear( [0 3],[0 8],[0 -1e21+2e-15] ) ans = 1 > collinear( [0 3],[0 8],[0 -1e22+2e-15]...
  • OkinawaDolphin: Loren, thank you for telling me where to download timeit. Here are the two functions I just tested...
  • Loren: JB- It looks to me like Ilya’s solution and therefore yours are equivalent to the determinant. As Tim...
  • Loren: OkinawaDolphin, timeit can be downloaded from the File Exchange. Steve Eddins is the author. It does not ship...
  • OkinawaDolphin: It seems that neither R2007a nor R2007b have the function timeit, but I investigated computation time...
  • J.B. Brown: It would appear to me that Ilya Rozenfeld’s solution would be the cleanest. Just to help those who...
  • Loren: Markus- Congratulations on winning! And a nice illustration of how the size matters. Small enough, and the...
  • Markus: Hi Loren, which version is fastest also depends very much on the matrix dimensions. Look at my test function:...
  • Duncan: OkinawaDolphin, Regarding why your third example is slower than your second example, the result is in fact...

These postings are the author's and don't necessarily represent the opinions of The MathWorks.

Related Topics