【问题标题】:How to get translation matrix 2D for coordinates transformation如何获取坐标变换的二维平移矩阵
【发布时间】:2021-01-15 15:11:16
【问题描述】:

我有两个二维坐标系 A 和 B。我知道坐标系 A 中的 3 个点是如何转换到坐标系 B 中的。

A1[x, y] => B1[x, y]
A2[x, y] => B2[x, y]
A3[x, y] => B3[x, y]

现在,我有一个来自坐标系 A 的点 A4,我需要计算一个点 B4,它在坐标系 B 中的位置。

我不知道如何从点 A1、A2、A3、B1、B2、B3 计算平移矩阵,以及如何使用该矩阵使用 C# 从 A4 计算点 B4。

我在 .NET 框架文档中没有找到任何有用的类或方法。

我检查了 MathNet 库,但这个库太大了,以至于新手无法阅读 MathNet 库。

【问题讨论】:

  • 使用“transform”而不是“translation”可能会更好。后者意味着系统之间的差异是一个不断的运动,然后这个问题是微不足道的。

标签: c# mathnet-numerics


【解决方案1】:

给定点对列表

IReadOnlyList<(Vector2 A, Vector2 B)> list

你可以找到点 A 和 B 之间的变换:

    var nRows = list.Count* 2;
    const int nCols = 6;
    var A = new DenseMatrix(nRows, nCols);
    var b = new DenseMatrix(nRows, 1);

    for (var p = 0; p < list.Count; p++)
    {
        var row = p * 2;
        A[row, 0] = list[p].A.X;
        A[row, 1] = list[p].A.Y;
        A[row, 2] = 1;
        b[row, 0] = list[p].B.X;
        row++;
        A[row, 3] = list[p].A.X;
        A[row, 4] = list[p].A.Y;
        A[row, 5] = 1;
        b[row, 0] = list[p].B.Y;
    }
    var x = A.Solve(b);
    var affine = new Matrix3x2(
        (float)x[0, 0], 
        (float)x[3, 0], 
        (float)x[1, 0], 
        (float)x[4, 0], 
        (float)x[2, 0], 
        (float)x[5, 0]) ;

这使用 Math.net.Numerics 作为矩阵求解器,并使用 System.Numerics.Matrix3x2 作为结果。这是affine transforms 使用 3+ 点对。你也可以用类似的方法计算单应性,但数学有点不同。

使用Vector2.Transform 应用转换

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-22
    • 1970-01-01
    • 2012-02-08
    • 2015-10-27
    • 2012-02-07
    • 1970-01-01
    相关资源
    最近更新 更多