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:
The version here will not be maintained.
Thanks for this :) I've been trying to convert 16-bit images to color on my own, and this worked right away with no issues.
Thanks Oliver... It's very useful. Sorry for the same question.
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).
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?
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 ?
Amazing Job mate, can edit any files really easily and create new color maps using the original maps in the files..
Bui: real2rgb is not appropriate for this problem. I suggest you pose your question on the newsgroup.
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:
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]);
Many thanks in advance.
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.
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.
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:
you will see that this output is exactly linear with the '*' option.
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*');
shows a nonlinear curve, whereas
[B lims mapgray]=real2rgb(peaks(256),'-hot');
Shouldn't it be the other way round?
A very useful tool, some lovely colormaps, and excellent support from the author.
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.
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?
Very handy, worked right away, helped me to superimpose transparent color map on grayscale image. Thanks!
Updated description as this function is to be maintained in the sc package only.
Fix bug when converting matrices containing Inf or -Inf without specifying limits. Thanks to Ming Wu for reporting it.
Fixed bug reported by Peter Nave (thanks!) whereby colormaps of 1024x3 are returned (too big for Windows colorbars).
Added cold and dusk colormaps
A few bug fixes and efficiency improvements. Also, colormap functions return concise table when input == Inf rather than < 0.
Now faster when using non-linear colormaps, thanks to a suggestion by Steve Lord to use histc.
Two important bug fixes, increased colormap size used to 1024 (from 256), and added pink (standard) and bright (new) colormaps.
Inspired by: Cold Colormap
Download apps, toolboxes, and other File Exchange content using Add-On Explorer in MATLAB.