【问题标题】:QImage Custom Indexed Colors Using setColorTableQImage 使用 setColorTable 自定义索引颜色
【发布时间】:2016-02-13 16:07:48
【问题描述】:

在我的项目中,我需要将具有多种颜色的图像转换为仅使用我在自定义 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 错误。我在文档中找不到任何解释为什么要创建新表的内容。

感谢您的宝贵时间。

【问题讨论】:

    标签: c++ qt qimage color-space


    【解决方案1】:

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

    幸运的是,convertToFormat 的重载需要一个颜色表并且应该可以完成这项工作:

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

    【讨论】:

    • 啊完美!我怎么错过了?节省了我很多时间。不过我很好奇,你知道用哪种技术来确定最接近的颜色吗?
    • 嗯,如果我指定一个颜色表,那么漫反射抖动不再起作用......
    • 对;文档说:“从 32 位索引转换为 8 位索引是一项缓慢的操作,将使用直接的最接近颜色方法,没有抖动。” 我自己从未使用过可着色的图像......跨度>
    猜你喜欢
    • 1970-01-01
    • 2017-12-05
    • 2021-01-05
    • 1970-01-01
    • 1970-01-01
    • 2013-12-21
    • 2012-01-09
    • 2020-06-20
    • 2021-11-23
    相关资源
    最近更新 更多