Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create QTransform given 4 points defining the transformed unit square

Given 4 points being be the result of

QPolygon poly = transform.mapToPolygon(QRectF(0, 0, 1, 1));

how can I find QTransform transform? (Even better: also given an arbitrary source rectangle)

Motivation: Given the four corner points of an image to be drawn in a perspectively distorted coordinate system, how can I draw the image using QPainter?

Illustration of the problem

This is a screenshot illustrating the problem in GIMP, where one can transform a layer by moving around the 4 corners of the layer. This results in a perspective transformation. I want to do exactly the same in a Qt application. I know that QTransform is not restricted to affine transformations but can also handle perspective transformations.

like image 348
leemes Avatar asked Jul 16 '12 21:07

leemes


1 Answers

You should be able to do this with QTransform.squareToQuad. Just pass it the QPolygonF you want to transform to.

I've sometimes had some issues getting squareToQuad to do what I want, and have had to use QTransform.quadToQuad instead, defining my own starting quad, but you might have more luck.

like image 83
Daniel Avatar answered Sep 18 '22 15:09

Daniel