【问题标题】:Printing/saving QGraphicsScene to monochrome image very poor quality将 QGraphicsScene 打印/保存为单色图像质量很差
【发布时间】:2015-04-23 16:33:16
【问题描述】:

我想以 Mono 格式打印/保存 QGraphicsScene。我让场景只包含一个颜色对象(在其中一个窗口中,它实际上是黑白的)。

OutputView::OutputView(QWidget *parent)
    : QGraphicsView(parent) {...}


void OutputView::saveToImage() 
{
    QImage image(scene()->sceneRect().size().toSize(), QImage::Format_Mono);
    image.fill(Qt::transparent);
    QPainter painter(&image);
    painter.setRenderHint(QPainter::Antialiasing); // seems to do nothing
    render(&painter);
    image.save("output.png");
    painter.end();

    // to test rgb, i am adding that - but I really need only mono
    // I don't know why the background is black, but since I don't intend to use the color version, it is not relevant
    QImage image1(scene()->sceneRect().size().toSize(), QImage::Format_RGB32);
    image1.fill(Qt::transparent);
    QPainter painter1(&image1);
    render(&painter1);
    image1.save("output1.png");
    painter1.end();
}

黑白图像的质量很差!

我预计红色像素会变成黑色,但只有部分像素会变成黑色。对于第 4 个窗格,我希望所有黑色像素都在单声道输出中放置为黑色,但同样,只有其中一些像素这样做了。

有没有什么方法可以在物体有颜色的地方用黑色像素制作黑白图像,而在其他地方用白色像素制作黑白图像?

我可以轻松更改生成颜色部分的代码,以输出黑色而不是原始颜色,但这似乎并不能解决问题(第 4 个面板有黑色,但单色图像不包含所有像素)。

由于内存成本,我需要单声道...

如何让场景正确输出到 QImage 中?

更新1:我什至尝试抖动彩色图像 - 结果很可怕(在应该更暗的区域太亮,奇怪的伪影,另外我添加了一个耗时的额外步骤,最糟糕的是,添加一个非常大的 rgb 图像......这正是我想要避免的,由于资源非常有限,我需要单色图像)

    QImage image(scene()->sceneRect().size().toSize(), QImage::Format_RGB32);
    image.fill(Qt::transparent);
    QPainter painter(&image);
    painter.setRenderHint(QPainter::Antialiasing); // seems to do nothing
    render(&painter);
    painter.end();
    QImage image11 = image.convertToFormat(QImage::Format_Mono,
              Qt::MonoOnly | Qt::DiffuseDither);
    image1.save("output.png");

使用内置的 qt 抖动方法,我看到渲染到单色图像使用Qt::ThresholdDither

如果至少我可以在一个步骤中对渲染应用不同的抖动,而不必先构建彩色图像然后抖动到新的单色图像,那将是一个巨大的改进。记忆和时间都很重要。

虽然也许我真正需要的是一种快速的方法来遍历每个像素,如果它的强度高于 0.5 则将其设置为 1,如果低于则设置为 0?不过,这仍然意味着在创建 rgb32 图像之后需要单独的步骤……并且由于某种原因,“手动”操作总是比内置方法慢。

Update2:本例中绘制的项目是一个带有渐变画笔的矩形。我还将颜色分为 4 个 QGraphicsScene:如果 color == color1 then color2,3,4 = Qt::white 等等。

void Item::paint(QPainter *painter, const QStyleOptionGraphicsItem */*option*/, QWidget */*widget*/)
{
    QRadialGradient radialGradient(0, 0, 300, 100, 100);
    radialGradient.setColorAt(0.0, color1);
    radialGradient.setColorAt(0.27, color2);
    radialGradient.setColorAt(0.44, Qt::white);
    radialGradient.setColorAt(0.76, color3);
    radialGradient.setColorAt(1.0, color4);
    QBrush brush = QBrush(radialGradient);
    painter->setBrush(brush);
    painter->drawRect(boundingRect());
}

【问题讨论】:

  • 抖动的单色图像对我来说也很完美。你有什么特别的问题?简单地对非抖动图像进行阈值处理以减少平均误差。您需要发布完整尺寸的示例图像以及用于渲染它们的确切代码;如果有 Qt 错误就是测试用例。照原样,您没有提供足够的详细信息。
  • 我对渲染的输入是一个 QGraphicsScene,其中包含一个单一颜色的对象集合。由于内存限制,输出必须是单色位图。尽快拥有它也很棒。我展示的代码 - 首先将对象的单声道集合渲染为巨型 rgb 图像,然后将巨型 rgb 图像抖动回单声道,存在以下问题:(1)添加巨型 rgb 图像(2)添加非常耗时步骤(3)输出质量低(错误传播点,颜色较浅)

标签: c++ image qt monochrome


【解决方案1】:

目前我的解决方案是:

  • 对于场景中的每个项目,如果需要将其转换为图像,请将其与所有碰撞并位于其上方的项目一起放置在场景中

  • 将场景的项目矩形剪辑保存为彩色图像

  • 使用Qt::OrderedDither 将图像转换为单色,这样会产生更少的伪影

  • 将图像放置在场景中的项目位置

  • 使用默认(Qt::ThresholdDither)将场景转换为非单色

这可能会使用更少的内存(取决于他的项目有多大) - 但很有可能重复应用相同的过程,并以图像格式存储彼此重叠的区域。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-04
    • 1970-01-01
    • 2021-06-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多