# Colormap Manipulations 14

Posted by **Loren Shure**,

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.

- Steve's blog article: A Lab-based uniform color scale
- Eos, Vol. 85, No. 40, 5 October 2004, The End of the Rainbow? Color Schemes for Improved Data Graphics
- MATLAB Central File Exchange: Image Processing - Color
- MATLAB's
`colormapeditor`

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

Get
the MATLAB code

Published with MATLAB® 7.3

**Category:**- Less Used Functionality

## 14 CommentsOldest to Newest

**1**of 14

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**of 14

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**of 14

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**of 14

Ian-

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

–Loren

**5**of 14

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**of 14

Hey Loren:

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

Thanks

Wang

**7**of 14

Wang-

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

–loren

**8**of 14

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**of 14

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**of 14

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

**11**of 14

The default colormaps for version 4 and 5 are listed, but how do I change that default setting?

**12**of 14

Scott-

Here’s the doc for learning how to set graphics defaults.

–Loren

**13**of 14

Hi Lauren,

Why did you say this: “Colormap is not double or not in the correct range (between 0 and 1).”?

I loaded up the clown sample pic just like you did, and then I did:

>> figure, image(X), colormap(gray(256))

>> cmap = get(gcf,’colormap’);

The resulting image looks just like yours that you posted here. But the cmap variable is indeed of type double, and it does range from 0 to 1.

Thanks

**14**of 14

Alex-

What I meant is that the colors weren’t mapped to the full gray scale since X, the clown, only has values from 1 to 81, not 1 to 256.

–Loren

## Recent Comments