c++ - 格式 Indexed8 的 QImage 假着色
问题描述
我目前在使用我生成的颜色图对 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 的输出
解决方案
改变
QImage tmp = pixmap.toImage();
至
QImage tmp = pixmap.toImage().convertToFormat(QImage::Format_Grayscale8);
QPixmap 将图像存储为 RGB,因此当您从 QPixmap 转换为 QImage 时,格式将为 32 bpp ( Format_RGB32
)。转换回Format_Grayscale8
, 将为您提供预期的 8 bpp 灰度格式。
推荐阅读
- python - 迭代python中所有以前的值
- r - 如何使用非标准评估 NSE 来评估 data.table 上的参数?
- url - 撇号(有效字符)是百分比编码的 - 但只是有时
- java - 每周处理 S3 中的所有文件
- android - 检测软键盘是否打开的简单方法?
- three.js - 使用相机作为背景,同时在 A 帧中显示对象
- keras - 是否可以通过在 Keras 中将 model.compile 中的 loss_weights 设置为零来删除损失函数项?
- c++ - 关于 std::cout,为什么使用“extern”而不是“单例模式”
- mongodb - 为什么在 mongodb 文档中使用 slug 字段?
- angular - 无法解析配置“:classpath”的所有工件