简体   繁体   English

如何获取坐标变换的二维平移矩阵

[英]How to get translation matrix 2D for coordinates transformation

I have two 2D coordinate systems A and B. I know how 3 points from the coordinate system A are translated into the coordinate system B.我有两个 2D 坐标系 A 和 B。我知道坐标系 A 中的 3 个点是如何转换到坐标系 B 中的。

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

Now, I have a point A4 from the coordinate system A and I need to calculate a point B4 it's position in the coordinate system B.现在,我有一个来自坐标系 A 的点 A4,我需要计算一个点 B4,它是坐标系 B 中的 position。

I don't know how to calculate a translation matrix from points A1, A2, A3, B1, B2, B3 and how to use this matrix to calculate the point B4 from A4 using C#.我不知道如何从点 A1、A2、A3、B1、B2、B3 计算平移矩阵,以及如何使用该矩阵使用 C# 从 A4 计算点 B4。

I didn't find any useful classes or methods in .NET framework documentation.我在 .NET 框架文档中没有找到任何有用的类或方法。

I've checked the MathNet library but this library is so huge that it makes the MathNet library unreadable for a newbie.我检查了 MathNet 库,但这个库太大了,以至于新手无法阅读 MathNet 库。

Given the list of point-pairs给定点对列表

IReadOnlyList<(Vector2 A, Vector2 B)> list

you can find the transform between points A and B with:您可以通过以下方式找到点 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]) ;

This uses Math.net.Numerics as the matrix solver, and System.Numerics.Matrix3x2 for the result.这使用 Math.net.Numerics 作为矩阵求解器,并使用 System.Numerics.Matrix3x2 作为结果。 This is for affine transforms using 3+ point pairs.这适用于使用 3+ 点对的仿射变换 You can also calculate the homography in a similar way, but the math is a bit different.你也可以用类似的方式计算单应性,但数学有点不同。

use Vector2.Transform to apply the transformation使用Vector2.Transform应用转换

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM