4.66667

4.7 | 3 ratings Rate this file 158 downloads (last 30 days) File Size: 21.08 KB File ID: #23342

real2rgb & colormaps

by Oliver Woodford

 

18 Mar 2009 (Updated 13 Apr 2009)

Code covered by BSD License  

Converts intensity matrices into truecolor images.

Download Now | Watch this File

File Information
Description

This function converts an MxN matrix of real-valued intensities into a truecolor image (MxNx3 double array in the range [0,1]), without discretization, by linearly interpolating between colors of the input colormap. The output can be displayed using IMAGE or IMSHOW, exported using IMWRITE, used for texture mapping, etc.

Colormaps are specified either by name, or as a JxK colormap table (K = 3 or 4). Named colormaps can be prefixed with '-' to invert the colormap, and suffixed with '*' to make each color transition cover the same number of gray levels when converted to grayscale. This latter option allows colormaps to be adapted to print to a linear grayscale when printed in black & white, e.g. 'hot*'. Colormaps can be non-linear by specifying a relative length for each color transition in the fourth column of a colormap table.

Many old and new colormap functions are included in this submission. The old colormaps have been included, with improvements, for the following reasons. All colormap functions can additionally return the most concise colormap table required to generate that colormap, and they can take in an intensity image and return the truecolor output using that colormap.

Some of the new colormaps included are:
thermal - generate the look of false-color thermal (IR) images.
temp - a blue-white-red color scheme for temperature scales.
sepia - generate the look of old photographs.

Several new colormaps have been created specifically to revert to linear grayscale when printed in black & white. These are:
hicontrast - saturated colors, for maximum level differentiation.
bright - bright colors, with good level differentiation.
pastel - pastel shades, with good level differentiation.
copper2 - similar to copper, but continues on to white.
pink2 - black to white through several distinct shades of pink.
earth - shades of green and brown.

Creating new colormap functions of your own is now incredibly simple. Specify a concise colormap table, and the included colormap_helper function does the rest. If you submit such colormap functions to the FEX, please use the tag 'real2rgb_colormap' so they can be found easily.

If you require more advanced functionality, e.g. for multi-dimensional input values, take a look at SC (FEX ID: 16233). The functionality included here is a subset of that functionality, separated for ease of use and understanding.

Acknowledgements
This submission has inspired the following:
COLORMAP and COLORBAR utilities (Sep 2009)
MATLAB release MATLAB 7.5 (R2007b)
Zip File Content  
Other Files
autumn.m,
bled.m,
bone.m,
bright.m,
cool.m,
copper.m,
copper2.m,
earth.m,
gray.m,
hicontrast.m,
hot.m,
hsv.m,
hsv2.m,
jet.m,
pastel.m,
pink.m,
pink2.m,
private/colormap_helper.m,
private/rescale.m,
real2rgb.m,
sepia.m,
spring.m,
summer.m,
temp.m,
thermal.m,
winter.m
Tags for This File  
Everyone's Tags
Tags I've Applied
Add New Tags Please login to tag files.
Comments and Ratings (5)
18 Mar 2009 us

nice colormaps...
us

29 Apr 2009 Stanley  
28 Oct 2009 Malte

Very handy, worked right away, helped me to superimpose transparent color map on grayscale image. Thanks!

18 Nov 2009 Richard

Hi Oliver.

I don't understand what happens when a colour map with more than 256 elements is requested. I'm trying to create a jet colour map with 1024 elements to display a 10 bit image using the image function. If I do this using either your version of jet or the Matlab one, I get the same result - all blue! 512 creates blue and green strips. 300 elements gives the normal jet colour map between 0 and 255, then it looks like it starts over again from blue. This is not what I would expect - values lower than 256 correctly fit the full colour range into the number of colour map entries requested - I don't see why this should be any different for larger colour maps.

The work around I'm using is to use pcolor instead of image to plot the data, as this seems to work with a smaller (presumably 256 element) colour map and map the data onto this. While this works, I don't like the pcolor function very much. It's not intended for displaying images. The axes labels align with grid lines instead of pixels, the data cursor selects points instead of pixels, and I need to have an extra row and column of data.

Is there a way of getting a 1024 element colour map that behaves as would be expected?

Thanks!
Richard.

18 Nov 2009 Oliver Woodford

Richard: This issue is do do with the way MATLAB deals with the colormap, rather than the colormap itself, which is as it should be. MATLAB can only handle 256 level colormaps for indexed images (on Windows). Pcolor avoids the problem. So does using truecolor (rather than indexed) images. You can use real2rgb to generate a truecolor image from your 1024 level colormap and your indexed image.

Please login to add a comment or rating.
Updates
20 Mar 2009

Two important bug fixes, increased colormap size used to 1024 (from 256), and added pink (standard) and bright (new) colormaps.

25 Mar 2009

Now faster when using non-linear colormaps, thanks to a suggestion by Steve Lord to use histc.

11 Apr 2009

A few bug fixes and efficiency improvements. Also, colormap functions return concise table when input == Inf rather than < 0.

13 Apr 2009

Bug fix

Tag Activity for this File
Tag Applied By Date/Time
truecolor Oliver Woodford 18 Mar 2009 11:15:41
colormap Oliver Woodford 18 Mar 2009 11:15:41
sepia Oliver Woodford 18 Mar 2009 11:15:41
thermal Oliver Woodford 18 Mar 2009 11:15:41
real2rgb_colormap Oliver Woodford 18 Mar 2009 11:15:41
truecolour Oliver Woodford 13 Apr 2009 10:17:52
height map Oliver Woodford 13 Apr 2009 10:17:52
false color Oliver Woodford 13 Apr 2009 10:17:52
color Oliver Woodford 13 Apr 2009 10:17:52
colour Oliver Woodford 13 Apr 2009 10:17:52
intensity Oliver Woodford 13 Apr 2009 10:17:52
false color Alberto Cantillo 06 Aug 2009 08:11:15
real2rgb Oliver Woodford 27 Aug 2009 08:13:26
ind2rgb Oliver Woodford 27 Aug 2009 08:13:27
 

MATLAB Central Terms of Use

NOTICE: Any content you submit to MATLAB Central, including personal information, is not subject to the protections which may be afforded information collected under other sections of The MathWorks, Inc. Web site. You are entirely responsible for all content that you upload, post, e-mail, transmit or otherwise make available via MATLAB Central. The MathWorks does not control the content posted by visitors to MATLAB Central and, does not guarantee the accuracy, integrity, or quality of such content. Under no circumstances will The MathWorks be liable in any way for any content not authored by The MathWorks, or any loss or damage of any kind incurred as a result of the use of any content posted, e-mailed, transmitted or otherwise made available via MATLAB Central. Read the complete Terms prior to use.

Contact us at files@mathworks.com