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

后端 未结 7 1827
囚心锁ツ
囚心锁ツ 2020-12-12 15:21

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 need

相关标签:
7条回答
  • 2020-12-12 16:01

    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)
    

    jet hot_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)...

    0 讨论(0)
提交回复
热议问题