gpt4 book ai didi

c++ - QImage 使用 setColorTable 自定义索引颜色

转载 作者:行者123 更新时间:2023-11-28 05:50:23 28 4
gpt4 key购买 nike

在我的项目中,我需要将具有多种颜色的图像转换为仅使用我在自定义 colorTable 中设置的 144 种预定颜色中的任何一种的图像。

这是我的代码:

    QImage convImage(128, 128, QImage::Format_Indexed8);
convImage.setColorCount(144);
convImage.setColorTable(colorTable); //colorTable is a const QVector with 144 qRgb values.

//scaledImage is the source image
convImage = scaledImage.convertToFormat(QImage::Format_Indexed8,Qt::ThresholdDither|Qt::AutoColor);

ui->mapView->setPixmap(QPixmap::fromImage(convImage));

我希望 convImage 只包含我创建的 colorTable 中存在的颜色,但它似乎完全忽略了我设置的表格,而是创建了它自己的最多 256 种颜色的独特表格。

我可以通过遍历每个像素并找到一种从 colorTable 中准确选择颜色的方法来自行索引所有内容,但我想知道我是否只是错误地使用了 colorTable。我在文档中找不到任何解释为什么要创建新表的内容。

感谢您的宝贵时间。

最佳答案

好吧,问问你自己:convertToFormat()scaledImage 的调用应该如何知道你应用于 convImage 的颜色表?它对左侧的 convImage 一无所知。

幸运的是,convertToFormat 有一个重载,它需要一个 colortable 并且应该完成这项工作:

QImage convImage = scaledImage.convertToFormat (QImage::Format_Indexed8, 
colorTable,
Qt::ThresholdDither|Qt::AutoColor);

关于c++ - QImage 使用 setColorTable 自定义索引颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35382088/

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