【问题标题】:Generate AffineTransform from 3 points从 3 个点生成 AffineTransform
【发布时间】:2014-01-21 23:03:07
【问题描述】:

给定坐标系 A 中的 3 个点(x 和 y 坐标)和坐标系 B 中的 3 个对应点,我如何推导出从 A 转换为 B 的 AffineTransform。

我的问题与Create transform to map from one rectangle to another? 类似,只是该问题仅涉及 2 点 - 即,它假定没有轮换。

【问题讨论】:

  • 提示:将您的转换表示为 3x2 矩阵。为了解决它的条目,您最终不得不反转一个 3x3 矩阵。如果没有其他人发布答案,我会尝试稍后找到。

标签: java awt affinetransform


【解决方案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  |
                                        \               /

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

将上述等式两边乘以 3x3 矩阵的逆矩阵,得到 p, q, r, s, t, u, v 的值。

【讨论】:

  • 感谢您的回答,但矩阵的东西对我来说有点神秘。是否可以提供一个构建 AffineTransform 对象的 Java 代码示例?或者如果不是这样,您是否知道任何从基础开始的教程?到目前为止,我能够找到的信息要么从矩阵开始(并立即失去我),要么只涉及 AffineTransform 方法的直接使用。
  • 你问我那里有很多代码。但关键是找到 3x3 矩阵的逆矩阵。你为什么不找到一个可以做到这一点的库,或者开始将我链接到的算法转换为代码?
  • 我认为我可以使用 Apache commons-math 库进行矩阵求逆和乘法运算,然后在 AffineTransform 构造函数中使用该结果是否正确?
  • 我不知道。大概。我从来没用过。
【解决方案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);       
}

【讨论】:

  • 这会抛出 SingularMatrixException:矩阵是奇异的
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-05-14
  • 2018-01-29
相关资源
最近更新 更多