【发布时间】:2020-03-30 22:03:39
【问题描述】:
我目前在使用我生成的颜色图对 QImage 进行错误着色时遇到了一些麻烦。我已将代码精简为一些基本步骤(通常分布在多个类中)。
我可以用这段代码重现问题:
//fill color table
QVector< QRgb > colors;
for (unsigned int i = 0; i < 256; ++i) {
double fac = (double(i) / 255.) * 0.8;
QColor color = QColor::fromHslF(fac, 0.95, 0.5);
colors.push_front(color.rgba());
}
//load original
QImage origImg;
origImg.load("lena.jpg");
QImage::Format f = origImg.format(); //outputs Format_Grayscale8 (24)
origImg.save("out1.jpg");
//convert to pixmap
QPixmap pixmap;
pixmap.convertFromImage(origImg);
//convert back to Image
QImage tmp = pixmap.toImage();
tmp.save("out2.jpg");
//make false color version
QImage fc = QImage(tmp.bits(), tmp.width(), tmp.height(), QImage::Format_Indexed8);
fc.setColorTable(colors);
//save false color version
fc.save("fc.jpg");
我的临时结果 out1.jpg 和 out2.jpg 看起来不错(原件的副本),但最终结果已损坏。谁能告诉我出了什么问题?
原图:
fc.jpg 的输出
【问题讨论】:
-
仅供参考:SO: How can I apply a colortable to my grayscale 8 bit image and convert it correctly in Qt?(值得注意的是,我的示例代码中的
colorize()函数可能会有所帮助。) -
您的最后一张图像看起来像 R、G、B、A 字节已“按字面意思”存储到您的图像中,然后逐字节解释为索引值。原图部分的“网格”和“幽灵”外观让我想到了这一点。
-
在光栅 Qt 后端(您正在使用)中,
QPixmap只是QImage的包装,使用QPixmap没有意义,除非某些现有 API 需要它,在这种情况下,无论如何都需要只读访问,并且您可以在需要时将图像“转换”为像素图。 “转换”很便宜。 -
@ReinstateMonica 这不是一个全新的实现,而是在大型软件中纠正了这种行为。直接使用 QImage 并在渲染时进行转换会更聪明,因为 QPixmap 会丢失要转换回来的信息。 AFAIK 它不能应用颜色表的倒数,因此灰色信息会在途中丢失
标签: c++ qt image-processing qimage