gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 14:49:53 28 4
gpt4 key购买 nike

我正在尝试将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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 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或任何您需要的转换。

关于c++ - QImage:如何将灰度图像转换为RGB热图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58509179/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com