Qmage:使用颜色表从灰度转换为RGB

QImage: convert from grayscale to RGB with a color table

本文关键字:灰度 转换 RGB 颜色 Qmage      更新时间:2023-10-16

使用Qt,我尝试使用颜色表定义的自定义转换规则将Format_Indexed8图像转换为Format_RGB30。我认为这很简单,因为QImage::convertToFormat可以将颜色表作为参数,但我无法使其工作。

这是我的代码:

QImage image = QImage(data, width, height, QImage::Format_Indexed8);
QVector<QRgb> colorTable(256);
for (int i = 0; i < 255; i++)
    colorTable[i] = qRgb(255 - i, i, i);
image = image.convertToFormat(QImage::Format_RGB30, colorTable);

这段代码只是给了我一个RGB格式的图像,但看起来与灰度图像的眼睛相同。

我认为QImage::convertToFormat中的颜色表参数需要从 RGB 转换为索引,而您正在以相反的方式进行转换。

我会尝试直接在索引文件(源(中设置颜色表,使用 QImage::setColorTable ,然后调用 convertToFormat 仅传递格式参数:

QImage image = QImage(data, width, height, QImage::Format_Indexed8);
image.setColorTable(colorTable);
image = image.convertToFormat(QImage::Format_RGB30);

自Qt 5.5(2015年7月发布(以来,这不是必需的。您现在可以使用 QImage::Format_Grayscale8 .您的代码很简单:

QImage image{data, width, height, QImage::Format_Grayscale8};