C++实现类似Matlab的colormap Jet

发表于:2014-1-16 10:17

字体: | 上一篇 | 下一篇 | 我要投稿

 作者:鼓Sk8er    来源:51Testing软件测试网采编

  Matlab使用colormap Jet 可以将灰度图像生成彩色的热度图,灰度值越高,色彩偏向暖色调。相反亦然。
// ColorMap.h
#ifndef COLORMAP_H
#define COLORMAP_H
class colormap
{
public:
static void GroundColorMix(BYTE* color, double x, double min, double max);
static void Convert2Colormap(BYTE* Image, int *imSize);
};
#endif
//ColorMap.cpp
#include "ColorMap.h"
#define BChannel  2
#define GChannel  1
#define RChannel  0
void colormap::GroundColorMix(BYTE* color, double x, double min, double max)
{
double posSlope = (max-min)/60;
double negSlope = (min-max)/60;
if( x < 60 )
{
color[RChannel] = max;
color[GChannel] = posSlope*x+min;
color[BChannel] = min;
return;
}
else if ( x < 120 )
{
color[RChannel] = negSlope*x+2*max+min;
color[GChannel] = max;
color[BChannel] = min;
return;
}
else if ( x < 180  )
{
color[RChannel] = min;
color[GChannel] = max;
color[BChannel] = posSlope*x-2*max+min;
return;
}
else if ( x < 240  )
{
color[RChannel] = min;
color[GChannel] = negSlope*x+4*max+min;
color[BChannel] = max;
return;
}
else if ( x < 300  )
{
color[RChannel] = posSlope*x-4*max+min;
color[GChannel] = min;
color[BChannel] = max;
return;
}
else
{
color[RChannel] = max;
color[GChannel] = min;
color[2] = negSlope*x+6*max;
return;
}
}
void colormap::Convert2Colormap(BYTE* Image, int *imSize)
//Image为三通道的灰度图像,R=G=B
{
const int stride = imSize[1]*imSize[2];
const int height = imSize[0];
const int width = imSize[1];
for (int i=0; i<height; i++)
{
BYTE *rowIndex = &Image[i*stride];
for (int j=0; j<stride; j+=3)
{
GroundColorMix(&rowIndex[j],rowIndex[j],0.0,255.0);
}
}
}
#undef BChannel
#undef  GChannel
#undef  RChannel
21/212>
《2023软件测试行业现状调查报告》独家发布~

关注51Testing

联系我们

快捷面板 站点地图 联系我们 广告服务 关于我们 站长统计 发展历程

法律顾问:上海兰迪律师事务所 项棋律师
版权所有 上海博为峰软件技术股份有限公司 Copyright©51testing.com 2003-2024
投诉及意见反馈:webmaster@51testing.com; 业务联系:service@51testing.com 021-64471599-8017

沪ICP备05003035号

沪公网安备 31010102002173号