【问题标题】:QImage transform returns nullQImage 转换返回 null
【发布时间】:2013-12-28 12:31:30
【问题描述】:

我有一个图像,我正在尝试使用 QImage::transformed(const QTransform &matrix, Qt::TransformationMode mode) const 方法转换图像的选定部分。 但是这个方法返回空图像。 Qt 还写入标准输出:

“Qimage:内存不足,返回空图像。”

    QImage img;
    img.load("D:\\3.png");

QPolygonF polygonIn;
polygonIn  << QPointF(73, 63)
    << QPointF(362, 22)
    << QPointF(517, 325)
    << QPointF(1, 210);

QPolygonF polygonOut;
polygonOut  << QPointF(45, 39)
    << QPointF(228, 13)
    << QPointF(228, 198)
    << QPointF(180, 198);

    QTransform transform;
auto isOk = QTransform::quadToQuad(polygonIn, polygonOut, transform);
if(!isOk)
    throw std::runtime_error("Transformation impossible with such parameters.");

auto result = img.transformed(transform, Qt::TransformationMode::SmoothTransformation);

我尝试调试 QImage 源。 qimage.cpp第6633行

QImage dImage(wd, hd, target_format);
QIMAGE_SANITYCHECK_MEMORY(dImage);

wd 和 hd 值很大。

也许有人知道我可以如何解决这个问题。顺便说一句,我使用 qt 4.8.4。

【问题讨论】:

  • 图片的宽度和高度是多少?据我所知,如果它们大于 ~32000 像素,加载这样的图像可能会出现问题。
  • 我的输入图像是 s29.postimg.org/k45fwbmsn/Untitled.png 我想将其转换为黄色多边形 s9.postimg.org/3rcfnjej3/Untitled_Transform.png 这点实际上是角坐标。
  • 您的多边形绝对不正确,并且会产生比例因子 ~1e+6 的异常变换。这就是你得到空图像的原因。

标签: c++ image qt image-processing qimage


【解决方案1】:

QImage 工作正常。问题出在你的变换中。试试下面的代码检查一下:

qDebug() << transform.map(QRectF(0, 0, img.width(), img.height())).boundingRect();

如果图像为 100x100,则返回 QRectF(-2.21401e+08,-1.9792e+08 2.21401e+08x1.97921e+08)。显然,大的图像无法放入内存。您需要更正您的变换,以便将您的图像缩放到合理的大小。我不知道你从哪里得到这些神奇的数字,但尝试获得新的。

另外img = img.scaled(img.width(), img.height()); 显然没有效果(将图像缩放到它已有的大小有什么意义?)。与painter 相关的代码似乎不完整,但这可能是复制粘贴问题。

【讨论】:

猜你喜欢
  • 2018-01-16
  • 1970-01-01
  • 1970-01-01
  • 2019-06-22
  • 1970-01-01
  • 2013-10-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多