从3点产生的AffineTransform(Generate AffineTransform fro

2019-10-19 06:24发布

给定3个点(x和y坐标)在坐标系A,和在坐标系统B 3个的对应点,我怎样才能获得的AffineTransform将从甲转换为B.

我的问题是相似的创建转变,从一个矩形到另一个地图? 除了这个问题只有2个点优惠 - 即,它假定没有旋转。

Answer 1:

假设你的变换形式

x' = px + qy + r
y' = sx + ty + u

写你的六个点为(A1x, A1y), (A2x, A2y), (A3x, A3y), (B1x, B1y), (B2x, B2y), (B3x, B3y) 这表示以矩阵形式给出

/               \       /           \   /               \
| B1x  B2x  B3x |       | p   q   r |   | A1x  A2x  A3x |
|               |   =   |           |   |               |
| B1y  B2y  B3y |       | s   t   u |   | A1y  A2y  A3y |
\               /       \           /   |               |
                                        |  1    1    1  |
                                        \               /

现在,找到右侧的3×3矩阵的逆。 你会在网上找到大量的算法告诉你如何做到这一点。 还有一个在http://www.econ.umn.edu/undergrad/math/An%20Algorithm%20for%20Finding%20the%20Inverse.pdf ,例如。

由3×3矩阵的逆后乘以等式两边以上,得到的值p, q, r, s, t, u, v



Answer 2:

如果这是别人有用的,这里是我以前做这个的Java代码。

    public static AffineTransform deriveAffineTransform(
        double oldX1, double oldY1,
        double oldX2, double oldY2,
        double oldX3, double oldY3,
        double newX1, double newY1,
        double newX2, double newY2,
        double newX3, double newY3) {

    double[][] oldData = { {oldX1, oldX2, oldX3}, {oldY1, oldY2, oldY3}, {1, 1, 1} };
    RealMatrix oldMatrix = MatrixUtils.createRealMatrix(oldData);

    double[][] newData = { {newX1, newX2, newX3}, {newY1, newY2, newY3} };
    RealMatrix newMatrix = MatrixUtils.createRealMatrix(newData);

    RealMatrix inverseOld = new LUDecomposition(oldMatrix).getSolver().getInverse();
    RealMatrix transformationMatrix = newMatrix.multiply(inverseOld);

    double m00 = transformationMatrix.getEntry(0, 0);
    double m01 = transformationMatrix.getEntry(0, 1);
    double m02 = transformationMatrix.getEntry(0, 2);
    double m10 = transformationMatrix.getEntry(1, 0);
    double m11 = transformationMatrix.getEntry(1, 1);
    double m12 = transformationMatrix.getEntry(1, 2);

    return new AffineTransform(m00, m10, m01, m11, m02, m12);       
}


文章来源: Generate AffineTransform from 3 points