Qmage:如何将灰色图像转换为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 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 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 

诀窍是首先转换为没有任何颜色映射表QImage::Format_Indexed8,并将其设置为单独的调用:

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

在此之后,您可以第二次转换为QImage::Format_RGB888或您需要的任何内容。

最新更新