0

I have an image, and I'm trying to transform selected part of the image using QImage::transformed(const QTransform &matrix, Qt::TransformationMode mode) const method. But this method return null image. Also Qt wrote to std output:

"Qimage: out of memory, returning null image."

    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);

I've tried to debug QImage sources. On line 6633 of qimage.cpp

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

wd and hd values are huge.

Maybe someone know how I can solve this problem. BTW I use qt 4.8.4.

Cœur
  • 37,241
  • 25
  • 195
  • 267
yaroslav
  • 115
  • 1
  • 11
  • 1
    What are image width and height? As far as I know if they are larger than ~32000 pixels, it could be a problem of loading such an image. – vahancho Dec 10 '13 at 21:44
  • My Input image is s29.postimg.org/k45fwbmsn/Untitled.png and I want to transform it to yellow polygon s9.postimg.org/3rcfnjej3/Untitled_Transform.png This points is actually corner coords. – yaroslav Dec 11 '13 at 09:58
  • 1
    Your polygons are definitely incorrect and produce abnormal transformation with scaling factor ~1e+6. That is why you get null image. – vahancho Dec 11 '13 at 10:36

1 Answers1

1

QImage works fine. The problem is in your transform. Try the following code to check:

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

It returns QRectF(-2.21401e+08,-1.9792e+08 2.21401e+08x1.97921e+08) if image is 100x100. Obviously the image that large cannot fit in memory. You need to correct your transform so it scales your image to reasonable size. I don't know where did you get those magic numbers from, but try to get new ones.

Also img = img.scaled(img.width(), img.height()); apparently has no effect (what's the point in scaling an image to the size that it already has?). Code related to painter seems to be incomplete but this is probably a copy-paste issue.

Pavel Strakhov
  • 39,123
  • 5
  • 88
  • 127
  • Thanks for the answer. My Input image is http://s29.postimg.org/k45fwbmsn/Untitled.png and I want to transform it to yellow polygon http://s9.postimg.org/3rcfnjej3/Untitled_Transform.png This points is actually corner cooords. Is there exist some workaround to reduce transformed image size? – yaroslav Dec 11 '13 at 08:52
  • The transformation goes horribly - see this image: http://i.stack.imgur.com/ipLM7.png (produced by manual image transformtion). It seems that the transformation for these polygons is too curved. The only thing you can do is to choose different polygons. – Pavel Strakhov Dec 11 '13 at 21:30