一、颜色映射简介
opencv中cv::applyColorMap()包含了20中颜色映射算法,autumn bone jet winter rainbow ocean summer spring cool hsv pink hot parula magma inferno plasma viridis cividis twilight twilight_shifted


二、JET映射规律与实现

c++实现
void jetColorMap(double gray, int &r, int &g, int &b)
{
if (gray < 0 && gray > 255)
{
r = 0;
g = 0;
b = 0;
return;
}
//0~31
if(gray >= 0 && gray <= 31)
{
r = 0;
g = 0;
b = 128 + 4 * (gray - 0);
return;
}
//32
if(gray == 32)
{
r = 0;
g = 0;
b = 255;
return;
}
//33~95
if(gray >= 33 && gray <= 95)
{
r = 0;
g = 4 + 4 * (gray - 33);
b = 255;
return;
}
//96
if(gray == 32)
{
r = 2;
g = 255;
b = 254;
return;
}
//97~158
if(gray >= 97 && gray <= 158)
{
r = 6 + 4 * (gray - 97);
g = 255;
b = 250 - 4 * (gray - 97);
return;
}
//159
if(gray == 32)
{
r = 254;
g = 255;
b = 1;
return;
}
//160~223
if(gray >= 160 && gray <= 223)
{
r = 255;
g = 252 - 4 * (gray - 160) ;
b = 0;
return;
}
//224~255
if(gray >= 224 && gray <= 255)
{
r = 252 - 4 * (gray - 224);
g = 0;
b = 0;
return;
}
}
三、彩色点云应用
3.1、彩色点云
点云图,配合opengl,根据高度大下,显示不同的颜色
点云显示可以参考我之前的文章
https://blog.csdn.net/qq_40602000/article/details/128293336

关键代码
void PointCloudGLWidget::drawPointdata()
{
glPushMatrix();
glBegin(GL_POINTS);
for(int i = 0; i < m_pointVector.size(); ++i)
{
QVector3D vec3d = m_pointVector.at(i);
QColor color = m_colorVector.at(i);
setGLMaterialColor(color);
glVertex3d(vec3d.x(), vec3d.y(), vec3d.z());
}
glEnd();
glFlush();
glPopMatrix();
}
3.2、颜色条

重写QWidget的paintEvent函数
#ifndef COLORBAR_H
#define COLORBAR_H
#include <QWidget>
class ColorBar : public QWidget
{
Q_OBJECT
public:
explicit ColorBar(QWidget *parent = nullptr);
void setColors(const QVector<QColor> &colors);
QColor getColor(double value);
static void jetColorMap(const double data, int &r, int &g, int &b);
protected:
void paintEvent(QPaintEvent *event) override;
public slots:
void setMinMax(double min, double max);
private:
QVector<QColor> m_colors;
double min = 0;
double max = 1;
const int MINHEIGHT = 255;
};
#endif // COLORBAR_H
#include "ColorBar.h"
#include <QPainter>
#include <QDebug>
ColorBar::ColorBar(QWidget *parent) : QWidget(parent)
{
int r, g, b;
for(int i = 0; i < 255; ++i)
{
jetColorMap(i, r, g, b);
m_colors.push_back(QColor(r, g, b));
}
}
void ColorBar::setColors(const QVector<QColor> &colors)
{
m_colors.clear();
m_colors = colors;
}
void ColorBar::paintEvent(QPaintEvent *event)
{
QPainter painter(this);
if (height() < MINHEIGHT)
{
return;
}
for (int i = 0 ; i < MINHEIGHT; i++)
{
QRect rect(0, MINHEIGHT - i + 20, 20, 1);
painter.fillRect(rect, m_colors.at(i));
}
for (int i = 0; i < 6; i++)
{
QString strValue;
strValue.sprintf("%.2f", min + i * (max - min) / 5);
QFont font;
font.setFamily("Microsoft YaHei");
font.setPointSize(8);
QFontMetrics fm(font);
QRect rec = fm.boundingRect(strValue);
int textWidth = rec.width();
int textHeight = rec.height();
QRect textRect(25, MINHEIGHT + textHeight - (MINHEIGHT / 5)*i, textWidth + 20, textHeight);
painter.setPen(QColor(Qt::red));
painter.drawText(textRect, strValue);
}
}
void ColorBar::setMinMax(double min, double max)
{
this->min = min;
this->max = max;
update();
}
QColor ColorBar::getColor(double value)
{
if (value > max || value < min)
{
return QColor(0, 0, 0);
}
int index = ((value - min) / (max - min)) * MINHEIGHT;
return m_colors.at(index);
}
void ColorBar::jetColorMap(const double data, int &r, int &g, int &b)
{
int gray = data;
if (gray < 0 && gray > 255)
{
r = 0;
g = 0;
b = 0;
return;
}
//0~31
if(gray >= 0 && gray <= 31)
{
r = 0;
g = 0;
b = 128 + 4 * (gray - 0);
return;
}
//32
if(gray == 32)
{
r = 0;
g = 0;
b = 255;
return;
}
//33~95
if(gray >= 33 && gray <= 95)
{
r = 0;
g = 4 + 4 * (gray - 33);
b = 255;
return;
}
//96
if(gray == 32)
{
r = 2;
g = 255;
b = 254;
return;
}
//97~158
if(gray >= 97 && gray <= 158)
{
r = 6 + 4 * (gray - 97);
g = 255;
b = 250 - 4 * (gray - 97);
return;
}
//159
if(gray == 32)
{
r = 254;
g = 255;
b = 1;
return;
}
//160~223
if(gray >= 160 && gray <= 223)
{
r = 255;
g = 252 - 4 * (gray - 160) ;
b = 0;
return;
}
//224~255
if(gray >= 224 && gray <= 255)
{
r = 252 - 4 * (gray - 224);
g = 0;
b = 0;
return;
}
}
3.3、彩色高度图像
激光扫描的高度数据映射到rgb图像上
