问题描述
我有2D的4个点的坐标,它们形成一个矩形,并且在应用了透视变换之后它们的坐标.
透视变换是在同构坐标中计算的,并由3x3矩阵 M
定义.如果矩阵未知,如何从给定点计算出来?
一分的计算公式为:
|M11 M12 M13 ||P1.x ||w * P1'.x ||M21 M22 M23 |* |P1.y |= |w * P1'.y ||M31 M32 M33 ||1 ||w * 1 |
要同时计算所有点,我将它们一起写在一个矩阵 A
中,并类似地将变换后的点写在矩阵 B
中:
|P1.x P2.x P3.x P4.x |A = |P1.y P2.y P3.y P4.y ||1 1 1 1 |
因此等式为 M * A = B
,这可以通过 M = B/A
或 M =(A'\ B')'
.
但这不是那么容易.我知道变换后点的坐标,但我不知道确切的 B
,因为存在因子 w
并且在齐次变换后不需要 1.因为在同构坐标中,向量的每个倍数都是同一点,所以我不知道会得到哪个倍数.
考虑到这些未知因素,我将等式写为 M * A = B * W
其中 W
是一个对角矩阵,对角线上 B
中每个点的因子均为w1 ... w4.所以 A
和 B
现在完全已知,我必须为 M
和 W
求解这个方程.>
如果我可以将等式重新排列为 x * A = B
或 A * x = B
的形式,其中 x
类似于 M * W
我可以解决它,而知道 M * W
的解决方案可能已经足够了.但是,尽管尝试了所有可能的重排,但我都没有设法这样做.直到我不敢封装(M * W)
,因为一个是3x3矩阵,另一个是4x4矩阵.在这里,我被卡住了.
也 M * A = B * W
没有针对 M
的单一解决方案,因为 M
的每一个倍数都是相同的转换.将其写为线性方程组系统,只需将 M
的条目之一固定即可获得单个解决方案.此外,可能有些输入根本无法解决 M ,但是暂时不用担心.
我实际上想要实现的是某种矢量图形编辑程序,用户可以在内部计算变换矩阵的同时拖动形状边界框的角进行变换.
实际上我在JavaScript中需要它,但是如果我什至不能在MATLAB中解决这个问题,我就会完全陷入困境.
应该是一个简单的问题.那么如何将 M * A = B * W
转换为可解决的形式?这只是矩阵乘法,因此我们可以将其写为线性方程组.您会知道: M11 * A11 + M12 * A21 + M13 * A31 = B11 * W11 + B12 * W21 + B13 * W31 + B14 * W41
.每个线性方程组都可以用 Ax = b
的形式编写,或者避免与我的问题中已使用的变量混淆: N * x = y
.就是这样.
根据我的问题的示例:我生成一些输入数据,这些输入数据具有已知的 M
和 W
:
M = [1 2 3;4 5 6;7 8 1];A = [0 0 1 1;0 1 0 1;1 1 1 1];W = [4 0 0 0;0 3 0 0;0 0 2 0;0 0 0 1];B = M * A *(W ^ -1);
然后我忘记了 M
和 W
.这意味着我现在有13个要解决的变量.我将 M * A = B * W
重写为线性方程组,并从那里重写为 N * x = y
.在 N
中,每一列都有一个变量的因数:
N = [A(1,1)A(2,1)A(3,1)0 0 0 0 0 0 -B(1,1)0 0 0;0 0 0 A(1,1)A(2,1)A(3,1)0 0 0 -B(2,1)0 0 0;0 0 0 0 0 0 0 A(1,1)A(2,1)A(3,1)-B(3,1)0 0 0;A(1,2)A(2,2)A(3,2)0 0 0 0 0 0 0 -B(1,2)0 0;0 0 0 A(1,2)A(2,2)A(3,2)0 0 0 0 -B(2,2)0 0;0 0 0 0 0 0 A(1,2)A(2,2)A(3,2)0 -B(3,2)0 0;A(1,3)A(2,3)A(3,3)0 0 0 0 0 0 0 0 -B(1,3)0;0 0 0 A(1,3)A(2,3)A(3,3)0 0 0 0 0 -B(2,3)0;0 0 0 0 0 0 A(1,3)A(2,3)A(3,3)0 0 -B(3,3)0;A(1,4)A(2,4)A(3,4)0 0 0 0 0 0 0 0 0 -B(1,4);0 0 0 A(1,4)A(2,4)A(3,4)0 0 0 0 0 0 -B(2,4);0 0 0 0 0 0 A(1,4)A(2,4)A(3,4)0 0 0 -B(3,4);0 0 0 0 0 0 0 0 1 0 0 0 0];
y
是:
y = [0;0;0;0;0;0;0;0;0;0;0;0;1];
请注意,根据 y
, N
中最后一行描述的方程的解为1.这就是我在问题中提到的内容,您必须修复 M
的条目之一才能获得单个解决方案.(我们之所以这样做,是因为 M
的每一个倍数都是相同的变换.)并且用这个等式,我说M33应该是1.
我们为 x
解决了此问题:
x = N \ y
并获得:
x = [1.00000;2.00000;3.00000;4.00000;5.00000;6.00000;7.00000;8.00000;1.00000;4.00000;3.00000;2.00000;1.00000]
是 [M11,M12,M13,M21,M22,M23,M31,M32,M33,w1,w2,w3,w4]的解决方案
在JavaScript中执行此操作时,我可以使用数字JavaScript 库,该库具有所需的功能解决以解决Ax = b.
I've got coordinates of 4 points in 2D that form a rectangle and their coordinates after a perspective transformation has been applied.
The perspective transformation is calculated in homogeneous coordinates and defined by a 3x3 matrix M
. If the matrix is not known, how can I calculate it from the given points?
The calculation for one point would be:
| M11 M12 M13 | | P1.x | | w*P1'.x |
| M21 M22 M23 | * | P1.y | = | w*P1'.y |
| M31 M32 M33 | | 1 | | w*1 |
To calculate all points simultaneously I write them together in one matrix A
and analogously for the transformed points in a matrix B
:
| P1.x P2.x P3.x P4.x |
A = | P1.y P2.y P3.y P4.y |
| 1 1 1 1 |
So the equation is M*A=B
and this can be solved for M
in MATLAB by M = B/A
or M = (A'\B')'
.
But it's not that easy. I know the coordinates of the points after transformation, but I don't know the exact B
, because there is the factor w
and it's not necessary 1 after a homogeneous transformation. Cause in homogeneous coordinates every multiple of a vector is the same point and I don't know which multiple I'll get.
To take account of these unknown factors I write the equation as M*A=B*W
where W
is a diagonal matrix with the factors w1...w4 for every point in B
on the diagonal. So A
and B
are now completely known and I have to solve this equation for M
and W
.
If I could rearrange the equation into the form x*A=B
or A*x=B
where x
would be something like M*W
I could solve it and knowing the solution for M*W
would maybe be enough already. However despite trying every possible rearrangement I didn't managed to do so. Until it hit me that encapsulating (M*W)
would not be possible, since one is a 3x3 matrix and the other a 4x4 matrix. And here I'm stuck.
Also M*A=B*W
does not have a single solution for M
, because every multiple of M
is the same transformation. Writing this as a system of linear equations one could simply fix one of the entries of M
to get a single solution. Furthermore there might be inputs that have no solution for M
at all, but let's not worry about this for now.
What I'm actually trying to achieve is some kind of vector graphics editing program where the user can drag the corners of a shape's bounding box to transform it, while internally the transformation matrix is calculated.
And actually I need this in JavaScript, but if I can't even solve this in MATLAB I'm completely stuck.
Should have been an easy question. So how do I get M*A=B*W
into a solvable form? It's just matrix multiplications, so we can write this as a system of linear equations. You know like: M11*A11 + M12*A21 + M13*A31 = B11*W11 + B12*W21 + B13*W31 + B14*W41
. And every system of linear equations can be written in the form Ax=b
, or to avoid confusion with already used variables in my question: N*x=y
. That's all.
An example according to my question: I generate some input data with a known M
and W
:
M = [
1 2 3;
4 5 6;
7 8 1
];
A = [
0 0 1 1;
0 1 0 1;
1 1 1 1
];
W = [
4 0 0 0;
0 3 0 0;
0 0 2 0;
0 0 0 1
];
B = M*A*(W^-1);
Then I forget about M
and W
. Meaning I now have 13 variables I'm looking to solve. I rewrite M*A=B*W
into a system of linear equations, and from there into the form N*x=y
. In N
every column has the factors for one variable:
N = [
A(1,1) A(2,1) A(3,1) 0 0 0 0 0 0 -B(1,1) 0 0 0;
0 0 0 A(1,1) A(2,1) A(3,1) 0 0 0 -B(2,1) 0 0 0;
0 0 0 0 0 0 A(1,1) A(2,1) A(3,1) -B(3,1) 0 0 0;
A(1,2) A(2,2) A(3,2) 0 0 0 0 0 0 0 -B(1,2) 0 0;
0 0 0 A(1,2) A(2,2) A(3,2) 0 0 0 0 -B(2,2) 0 0;
0 0 0 0 0 0 A(1,2) A(2,2) A(3,2) 0 -B(3,2) 0 0;
A(1,3) A(2,3) A(3,3) 0 0 0 0 0 0 0 0 -B(1,3) 0;
0 0 0 A(1,3) A(2,3) A(3,3) 0 0 0 0 0 -B(2,3) 0;
0 0 0 0 0 0 A(1,3) A(2,3) A(3,3) 0 0 -B(3,3) 0;
A(1,4) A(2,4) A(3,4) 0 0 0 0 0 0 0 0 0 -B(1,4);
0 0 0 A(1,4) A(2,4) A(3,4) 0 0 0 0 0 0 -B(2,4);
0 0 0 0 0 0 A(1,4) A(2,4) A(3,4) 0 0 0 -B(3,4);
0 0 0 0 0 0 0 0 1 0 0 0 0
];
And y
is:
y = [ 0; 0; 0; 0; 0; 0; 0; 0; 0; 0; 0; 0; 1 ];
Notice the equation described by the last row in N
whose solution is 1 according to y
. That's what I mentioned in my question, you have to fix one of the entries of M
to get a single solution. (We can do this because every multiple of M
is the same transformation.) And with this equation I'm saying M33 should be 1.
We solve this for x
:
x = N\y
and get:
x = [ 1.00000; 2.00000; 3.00000; 4.00000; 5.00000; 6.00000; 7.00000; 8.00000; 1.00000; 4.00000; 3.00000; 2.00000; 1.00000 ]
which are the solutions for [ M11, M12, M13, M21, M22, M23, M31, M32, M33, w1, w2, w3, w4 ]
When doing this in JavaScript I could use the Numeric JavaScript library which has the needed function solve to solve Ax=b.
这篇关于在MATLAB中从4个点计算2D均匀透视变换矩阵的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!