QImage:如何将灰度图像转换为RGB热图

永远学习

我正在尝试将Format_Grayscale8图像转换Format_ARGB32(或任何其他8bpp RGB格式)。我尝试使用QImage::convertToFormat色表,但无法正常工作。输入是一个小的8位灰度图像(PGM格式的文​​本字符串)。

QImage img;
img.loadFromData(gray_map.toLatin1());
QImage heatmap = img.convertToFormat(QImage::Format_Indexed8, color_gradient.getColorMap());

手动执行即可(每像素像素):

// gray_img format is QImage::Format_Grayscale8
QImage createHeatMap(QImage gray_img)
{
    QImage heatmap(gray_img.width(), gray_img.height(), QImage::Format_ARGB32);

    // custom heatmap, size() is 256
    const QVector<QRgb> color_map = color_gradient.getColorMap();

    // Default color
    heatmap.fill(color_map[0]);

    for (int y = 0; y < gray_img.height(); y++)
    {
        for (int x = 0; x < gray_img.width(); x++)
        {
            const uchar* img_ptr = gray_img.bits();
            int offset = x + y*gray_img.bytesPerLine();
            uchar gray_pix = *(img_ptr + offset);

            // just in case
            if (gray_pix < color_map.size())
                heatmap.setPixel(x, y, color_map[gray_pix]);
        }
    }
    return heatmap;
}

QImage img;
img.loadFromData(doppler_map.toLatin1());
QImage heatmap = createHeatMap(img);

我对更简单,更有效的解决方案感兴趣。谢谢!

编辑

这是生成颜色渐变的代码:

// Heatmap color lookup table generator, inspired from:
// http://www.andrewnoske.com/wiki/Code_-_heatmaps_and_color_gradients

#include <QColor>
class ColorGradient
{
private:
    struct ColorPoint
    {
        float r, g, b;
        float val;          // Position of our color along the gradient (between 0 and 1).
        ColorPoint(float red, float green, float blue, float value)
            : r(red), g(green), b(blue), val(value)
        {
        }
    };
    std::vector<ColorPoint> mColors;            // An array of color points in ascending value.
    uint                    mTableSize = 0;
    QVector<QRgb>           mColorMap;

    // hidden
    ColorGradient();

public:

    inline auto getColorMap()
    {
        return mColorMap;
    }

    ColorGradient(uint table_size)
    {
        createDefaultHeatMapGradient(table_size);
    }

    //-- Places a 6 color heapmap gradient into the "color" vector
    #define CF64(val)  ((float)(val) / 64.0)
    #define CF256(val) ((float)(val) / 256.0)
    void createDefaultHeatMapGradient(uint table_size)
    {
        mTableSize = table_size;
        mColorMap.clear();
        mColors.clear();

        // ascending order
        mColors.push_back(ColorPoint(0,         0,  CF256(96),  CF64(00)));     // Dark Blue
        mColors.push_back(ColorPoint(0,         0,  1,          CF64(06)));     // Blue
        mColors.push_back(ColorPoint(0,         1,  1,          CF64(22)));     // Cyan
        mColors.push_back(ColorPoint(1,         1,  0,          CF64(39)));     // Yellow
        mColors.push_back(ColorPoint(1,         0,  0,          CF64(55)));     // Red
        mColors.push_back(ColorPoint(CF256(159),0,  0,          CF64(63)));     // Dark Red

        QColor color;

        // Generate the color table
        for (uint n = 0; n < table_size; n++)
        {
            float value = (float)n / (float)table_size;
            bool found = false;

            for (int i = 0; i < mColors.size(); i++)
            {
                ColorPoint &currC = mColors[i];
                if (value < currC.val)
                {
                    ColorPoint &prevC = mColors[std::max(0, i - 1)];
                    float valueDiff = (prevC.val - currC.val);
                    float fractBetween = (valueDiff == 0) ? 0 : (value - currC.val) / valueDiff;

                    float r = (prevC.r - currC.r)*fractBetween + currC.r;
                    float g = (prevC.g - currC.g)*fractBetween + currC.g;
                    float b = (prevC.b - currC.b)*fractBetween + currC.b;
                    color.setRgbF(r, g, b);
                    mColorMap.append(color.rgb());
                    found = true;
                    break;
                }
            }

            if (!found)
            {
                color.setRgbF(mColors.back().r, mColors.back().g, mColors.back().b);
                mColorMap.append(color.rgb());
            }
        }
    }
};

和一个示例文本图像文件:

P2
40 17
64
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 0 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0 0 0 0 0 
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 
乔巴特

诀窍是先转换为QImage::Format_Indexed8 不带任何颜色图,然后在单独的调用中进行设置:

QImage heatmap = img.convertToFormat(QImage::Format_Indexed8);
heatmap.setColorTable(color_gradient.getColorMap());

此后,您可以进行第二次转换为QImage::Format_RGB888或所需的任何转换

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何使用Python OpenCV将灰度图像转换为热图图像

如何将1d数组转换为3d数组(将灰度图像转换为rgb格式)?

将QImage转换为灰度

如何将灰度图像转换为像素值列表?

如何将选定的矩形从图像转换为灰度?

如何将嵌套数组转换为灰度图像?

如何将RGBA字节串转换为灰度图像?

OpenCV如何将图像转换为灰度?

如何将多索引转换为热图

如何将图像从像素转换为单热编码?

将灰度图像转换为 rgb 会损坏图像

如何在Python中将RGB图像转换为灰度图像?

编写我自己的图像函数,将灰度(0-255)转换为 ar、g、b、alpha 热图

将灰度转换为 RGB

如何将QImage转换为QByteArray?

如何将PDF转换为灰度

使用imagemagick将JPG图像从灰度转换为RGB

如何将3D RGB标签图像(在语义分割中)转换为2D灰度图像,并且类索引从0开始?

如何使用自定义颜色映射将灰度图像转换为 RGB?

如何使用python将二进制图像转换为灰度和RGB?

如何将RGB图像像素转换为L * a * b *?

如何将RGB图像转换为numpy数组?

如何将RGB图像转换为cmyk?

如何在python中将rgb图像转换为灰度

如何在OpenCV(Python)中将灰度图像转换为RGB?

如何在OpenCV(Python)中将灰度图像转换为RGB?

如何将灰度图像的张量转换为3通道图像?

如何将PNG图像(特别是灰度图像)转换为索引颜色

如何将图像转换为0-255灰度图像