Code covered by the BSD License  

Highlights from
real2rgb & colormaps

4.85714

4.9 | 7 ratings Rate this file 67 Downloads (last 30 days) File Size: 22.9 KB File ID: #23342
image thumbnail

real2rgb & colormaps

by

 

18 Mar 2009 (Updated )

Converts intensity matrices into truecolor images.

| 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.
dusk - pastel shades from cold and dark to warm and light.
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.

This function is now included in the sc package:
http://www.mathworks.com/matlabcentral/fileexchange/16233
The version here will not be maintained.

Acknowledgements

Cold Colormap inspired this file.

This file inspired Bichromatic Colormap For Real2rgb, Bipolar Colormap, Mat2im, Advanced Colormap, and Sc Powerful Image Rendering.

MATLAB release MATLAB 7.5 (R2007b)
Tags for This File   Please login to tag files.
Please login to add a comment or rating.
Comments and Ratings (21)
02 Dec 2013 Georgia

Thanks Oliver... It's very useful. Sorry for the same question.

02 Dec 2013 Oliver Woodford

Georgia: As I stated to Bui Yuki below, the rescale function comes in the zip file of this submission, in the subdirectory called "private". You need to put the folder containing the files on your MATLAB path or else cd to that directory (as with any function).

02 Dec 2013 Georgia

Hi Oliver,

When I run the real2rgb function, display the following error: "Undefined function 'rescale' ... " I can not found the function.

I used the following function, but maybe is not that it's want. And the color points displayed as black.

function [HsN,GsN] = rescale(Hs,Gs,cfsRESN)

cfsRES = cfsRESN(2) - cfsRESN(1);
HsN = Hs*cfsRES;
GsN = Gs*cfsRES;

Could you help me?

Thanks!

07 Mar 2012 bella  
06 Mar 2012 zhanyh

Hello Oliver and other friends,

If I plot two figures using real2rgb, HOW can I let them use a same colorbar?

If so, the log color of one figure must be changed to match that of the other one. How to realize such a thing ?

26 Jan 2011 Amjed

Amazing Job mate, can edit any files really easily and create new color maps using the original maps in the files..

A++++++

27 Oct 2010 Mathias Vanwolleghem  
31 Mar 2010 Bui Yuki

Hi Oliver,
Thanks anyway.
Regards,
Bui.

31 Mar 2010 Oliver Woodford

Bui: real2rgb is not appropriate for this problem. I suggest you pose your question on the newsgroup.

30 Mar 2010 Bui Yuki

Hi Oliver,

Many thanks for quick reply. I didn't notice the rescale function in the private folder.

Anyhow, I haven't yet solved my problem. Perhaps, I made some wrong setting. Therefore, your helps would be greatly appreciated.

A simple example is as follows:
x=1:1:100;
y=1:1:100;
color=1:1:100;
scatter(x,y,500,color,'.');
colorbar;

If I plot image as above, the color will distribute linearly from 1-100. However, I need to specify the color in five ranges 0-20; 20-40; 40-60; 60-80; 80-100. I guest your function could help to solve my problem. With your guidance, I should do something likes:
map = [0 0 0.5 20; 0 0 1 40-20; 0 0.5 1 60-40; 0 1 1 80-60; 0.5 1 0.5 100-80];
im = real2rgb(color, map, [0 100]);

What's next?
Many thanks in advance.

Bui

30 Mar 2010 Oliver Woodford

Bui: The rescale function clearly comes in the zip file, as can be seen from the list of files (on this page). You need to put the folder containing the files on your MATLAB path or else cd to that directory (as with any function). Other than that I cannot help, except to suggest that the error is with your setup, and not with the real2rgb package.

30 Mar 2010 Bui Yuki

Hi Oliver,

I am using your function (real2rgb) to control color ranges in the colorbar. But I encounter an error which needs your help to fix this problem.

My problem is as follow:
I have a pressure data, say p(:,1) with pmin = 0 and pmax = 2e5. I want to represent this pressure profile in 5 ranges with different colors. I using your function as follows:

map=[0 0 0.5 0.5e5; 0 0 1 1e5-0.5e5; 0 0.5 1 1.5e5-1e5; 0 1 1 2e0-1.5e5; 0.5 1 0.5 2.5e5-2e5];
im =real2rgb(p(:,1),map,[0 2.5e5]);

The error that I found is:
??? Undefined command/function 'rescale'.
Error ==> real2rgb at 114
[B lims] = rescale(B,lims,[0 maxInd]);

Could you please explain why I have this error? Thank you very much in advance.

Bui

15 Jan 2010 Antoine Gerber

Thanks Oliver.

15 Jan 2010 Oliver Woodford

Roger: Printers generally don't compute gray level from color by summing (or averaging) the R, G and B values. Rather, they use the same algorithm as found in the rgb2gray function. If you replace your plot function with:
plot(rgb2gray(mapgray));
you will see that this output is exactly linear with the '*' option.

15 Jan 2010 Antoine Gerber

It seems to me that the option '*' produces non-linear grayscale maps, whereas non-starred colormaps are linear in grayscale. E.g.
[B lims mapgray]=real2rgb(peaks(256),'-hot*');
plot(sum(mapgray,2));

shows a nonlinear curve, whereas
[B lims mapgray]=real2rgb(peaks(256),'-hot');
plot(sum(mapgray,2));

is linear.
Shouldn't it be the other way round?

11 Dec 2009 Ged Ridgway

A very useful tool, some lovely colormaps, and excellent support from the author.

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.

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.

28 Oct 2009 Malte

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

29 Apr 2009 Stanley  
18 Mar 2009 us

nice colormaps...
us

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

10 Dec 2009

Added cold and dusk colormaps

20 Aug 2010

Fixed bug reported by Peter Nave (thanks!) whereby colormaps of 1024x3 are returned (too big for Windows colorbars).

14 Jan 2011

Fix bug when converting matrices containing Inf or -Inf without specifying limits. Thanks to Ming Wu for reporting it.

08 Jul 2014

Updated description as this function is to be maintained in the sc package only.

Contact us