我想我發現一種解決方案,其計算由步驟變換矩陣步驟。
// 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