我想我发现一种解决方案,其计算由步骤变换矩阵步骤。
// some example points:
QPointF p1(1.0, 2.0);
QPointF p2(2.0, 2.5);
QPointF p3(1.5, 4.0);
QPointF p4(3.0, 5.0);
// define the affine transformation which will position p1, p2, p3 correctly:
QTransform trans;
trans.translate(p1.x(), p1.y());
trans.scale(p2.x() - p1.x(), p3.y() - p1.y());
trans.shear((p3.x() - p1.x())/trans.m11(), (p2.y() - p1.y())/trans.m22());
直到现在,反式描述了一个平行四边形转变。在这个paralellogram中,我发现p4(相对)在下一步。我认为这可以使用不涉及反式反转的直接公式来完成。
// relative position of the 4th point in the transformed coordinate system:
qreal px = trans.inverted().map(p4).x();
qreal py = trans.inverted().map(p4).y();
// this defines the perspective distortion:
qreal y = 1 + (py - 1)/px;
qreal x = 1 + (px - 1)/py;
值x
和y
很难解释。仅给出其中的一个(另一组到1
),这限定了仅p4
相对缩放。但是x和y两种透视变换的组合,x和y的含义都很难;我通过试验和错误找到了这些公式。
// and thus the perspective matrix:
QTransform persp(1/y, 0, 1/y-1,
0, 1/x, 1/x-1,
0, 0, 1);
// premultiply the perspective matrix to the affine transformation:
trans = persp * trans;
一些测试表明,这导致了正确的结果。但是,我没有测试过两点相等或者其中一个点在两个点之间的线段上的特殊情况;我认为这种解决方案可能会在这种情况下破裂。
所以,我还是寻找一些直接式矩阵值m11
,m12
... m33
,给出的点的坐标p1.x()
,p1.y()
... p4.x()
,p4.y()
。
注:我公司目前正在努力解决这个问题。我从p1开始(这是映射的左上角的点(0,0)):因此,我只使用一个转换矩阵。通过使用缩放和剪切矩阵可以达到p2和p3。但我有深远P4的问题:我不明白的透视变换背后的数学,因此不知道在第三列的矩阵表项的数量如何影响转型。 – leemes 2012-07-16 21:48:53