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

时间:2019-10-22 17:04:32

标签: c++ qt qt5 heatmap qimage

我正在尝试将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 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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 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 

1 个答案:

答案 0 :(得分:2)

技巧是首先转换为QImage::Format_Indexed8 而没有任何颜色映射,然后在单独的调用中进行设置:

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

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