QImage变换返回null

时间:2013-12-10 19:47:18

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

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

  

“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。

1 个答案:

答案 0 :(得分: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相关的代码似乎不完整,但这可能是一个复制粘贴问题。