matlab jet color mapping C / C++ / VC 实现

在matlab中调用imagesc()将一幅灰阶图像以彩色显示时,默认使用的color mapping是Jet,其color bar 为:

matlab jet color mapping C / C++ / VC 实现

Jet的color mapping图为:

matlab jet color mapping C / C++ / VC 实现

Color mapping图的绘制(Matlab代码):

function showRGBchannels(Fignr,Map); 
      x = linspace(0,1, size(Map, 1));
      figure(Fignr)
      lw = 4;
      plot( x, Map(:,1),'color',[1,0,0],'linewidth',lw,...
         x, Map(:,2),'color',[0,1,0],'linewidth',lw,... 
         x, Map(:,3),'color',[0,0,1],'linewidth',lw,...
         x, mean(Map,2),'color',[0.7,0.7,0.7],'o'); 
      xlabel 'fraction'
      ylabel 'intensity'
      end

调用:showRGBchannels(1,jet(500))

根据此color mapping图,我将C / C++代码实现如下:

void mapJet(double v, double vmin, double vmax, BYTE& r, BYTE& g, BYTE& b)
{
    r = 255;
    g = 255;
    b = 255;

if (v < vmin) {
        v = vmin;
    }

if (v > vmax) {
        v = vmax;
    }

double dr, dg, db;

if (v < 0.1242) {
        db = 0.504 + ((1.-0.504) / 0.1242)*v;
        dg = dr = 0.;
    } else if (v < 0.3747) {
        db = 1.;
        dr = 0.;
        dg = (v - 0.1242) * (1. / (0.3747-0.1242));
    } else if (v < 0.6253) {
        db = (0.6253 - v) * (1. / (0.6253-0.3747));
        dg = 1.;
        dr = (v - 0.3747) * (1. / (0.6253-0.3747));
    } else if (v < 0.8758) {
        db = 0.;
        dr = 1.;
        dg = (0.8758 - v) * (1. / (0.8758-0.6253));
    } else {
        db = 0.;
        dg = 0.;
        dr = 1. - (v - 0.8758) * ((1.-0.504) / (1.-0.8758));
    }

r = (BYTE) (255 * dr);
    g = (BYTE) (255 * dg);
    b = (BYTE) (255 * db);
}

其中,v为归一化的图像像素值(0.-1.)。

函数使用:

BYTE red, green, blue;

mapJet(image[i][j], 0., 1., red, green, blue);

上一篇:Delphi7打开项目提示'one or more lines were too long and has been truncated'


下一篇:Bootstrap系列 -- 37. 基础导航样式