QImage: как преобразовать серое изображение в RGB карту тепла - PullRequest
2 голосов
/ 22 октября 2019

Я пытаюсь преобразовать изображение 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 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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 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 

1 Ответ

2 голосов
/ 23 октября 2019

Хитрость заключается в том, чтобы сначала преобразовать в QImage::Format_Indexed8 без любую карту цветов и установить ее в отдельном вызове:

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

После этого вы можете сделать второе преобразованиена QImage::Format_RGB888 или что вам нужно.

...