Jet颜色映射算法及其应用

一、颜色映射简介

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图像上

四、参考文章

https://blog.csdn.net/qq_41498261/article/details/109603986

https://blog.csdn.net/kingkee/article/details/92785118

  • 1
    点赞
  • 31
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Jason~shen

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值