Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Grayscale to Red-Green-Blue (MATLAB Jet) color scale

Tags:

c++

colors

matlab

I was given a data set that is essentially an image, however each pixel in the image is represented as a value from -1 to 1 inclusive. I am writing an application that needs to take these -1 to 1 grayscale values and map them to the associated RGB value for the MATLAB "Jet" color scale (red-green-blue color gradient).

I am curious if anyone knows how to take a linear value (like -1 to 1) and map it to this scale. Note that I am not actually using MATLAB for this (nor can I), I just need to take the grayscale value and put it on the Jet gradient.

Thanks, Adam

like image 452
Adam Shook Avatar asked Oct 09 '11 20:10

Adam Shook


People also ask

How do you change scale color in Matlab?

You can change the color scheme by specifying a colormap. Colormaps are three-column arrays containing RGB triplets in which each row defines a distinct color. For example, here is a surface plot with the default color scheme. f = figure; surf(peaks);

How do you use colormap jet in Matlab?

Description. c = jet returns the jet colormap as a three-column array with the same number of rows as the colormap for the current figure. If no figure exists, then the number of rows is equal to the default length of 256. Each row in the array contains the red, green, and blue intensities for a specific color.

How do you show Colormap in Matlab?

Starting in R2019b, you can display a tiling of plots using the tiledlayout and nexttile functions. Call the tiledlayout function to create a 2-by-1 tiled chart layout. Call the nexttile function to create the axes. Then display a surface plot in each axes with a colorbar.

How do Colormaps work?

A colormap is an array of colors that are indexed and then mapped to values. It is represented by a [Nx3] matrix, where N is the number of colors available in it. Each color is coded with three values corresponding its relative levels of red, green and blue. In Matlab, the colors are coded between 0 and 1.


1 Answers

Consider the following function (written by Paul Bourke -- search for Colour Ramping for Data Visualisation):

/*    Return a RGB colour value given a scalar v in the range [vmin,vmax]    In this case each colour component ranges from 0 (no contribution) to    1 (fully saturated), modifications for other ranges is trivial.    The colour is clipped at the end of the scales if v is outside    the range [vmin,vmax] */  typedef struct {     double r,g,b; } COLOUR;  COLOUR GetColour(double v,double vmin,double vmax) {    COLOUR c = {1.0,1.0,1.0}; // white    double dv;     if (v < vmin)       v = vmin;    if (v > vmax)       v = vmax;    dv = vmax - vmin;     if (v < (vmin + 0.25 * dv)) {       c.r = 0;       c.g = 4 * (v - vmin) / dv;    } else if (v < (vmin + 0.5 * dv)) {       c.r = 0;       c.b = 1 + 4 * (vmin + 0.25 * dv - v) / dv;    } else if (v < (vmin + 0.75 * dv)) {       c.r = 4 * (v - vmin - 0.5 * dv) / dv;       c.b = 0;    } else {       c.g = 1 + 4 * (vmin + 0.75 * dv - v) / dv;       c.b = 0;    }     return(c); } 

Which, in your case, you would use it to map values in the range [-1,1] to colors as (it is straightforward to translate it from C code to a MATLAB function):

c = GetColour(v,-1.0,1.0); 

This produces to the following "hot-to-cold" color ramp:

color_ramp

It basically represents a walk on the edges of the RGB color cube from blue to red (passing by cyan, green, yellow), and interpolating the values along this path.

color_cube


Note this is slightly different from the "Jet" colormap used in MATLAB, which as far as I can tell, goes through the following path:

#00007F: dark blue #0000FF: blue #007FFF: azure #00FFFF: cyan #7FFF7F: light green #FFFF00: yellow #FF7F00: orange #FF0000: red #7F0000: dark red 

Here is a comparison I did in MATLAB:

%# values num = 64; v = linspace(-1,1,num);  %# colormaps clr1 = jet(num); clr2 = zeros(num,3); for i=1:num     clr2(i,:) = GetColour(v(i), v(1), v(end)); end 

Then we plot both using:

figure subplot(4,1,1), imagesc(v), colormap(clr), axis off subplot(4,1,2:4), h = plot(v,clr); axis tight set(h, {'Color'},{'r';'g';'b'}, 'LineWidth',3) 

jethot_to_cold

Now you can modify the C code above, and use the suggested stop points to achieve something similar to jet colormap (they all use linear interpolation over the R,G,B channels as you can see from the above plots)...

like image 68
Amro Avatar answered Oct 18 '22 08:10

Amro