简体   繁体   English

透视投影,4分

[英]Perspective projection, 4 points

I am trying to implement a perspective projection. 我正在尝试实施透视投影。 For example, I have a square with a given size, and known corners' coordinates (A1 .. A4) . 例如,我有一个给定大小的正方形和已知角点的坐标(A1 .. A4)。 I have an image where this square is displayed at some specific position. 我有一个图像,这个方块显示在某个特定的位置。 I know its position and the coordinates of the corners of the displayed square B1 .. B4. 我知道它的位置和显示的方形B1 .. B4的角的坐标。 I want to find a transformation matrix M that will translate B -> A. 我想找到一个转换矩阵M,它将翻译B - > A.

I followed method Mathematical Illustrations , chapter 10, and I managed to transform 3 corners well. 我按照方法数学插图 ,第10章,我设法很好地改变了3个角落。 But I cannot find the matrix to transform correctly 4 corners... I find the matrix as on page 7, but it does not translate all 4 points well. 但我发现矩阵无法正确转换4个角...我发现矩阵如第7页所示,但它并没有很好地转换所有4个点。

~

As your reference indicates, the standard way to represent a general projective transformation is to augment your (x,y) 2-D coordinates with a w coordinate (to get homogeneous coordinates [x,y,w] ), and use a 3x3 matrix to transform them. 正如您的参考所示,表示一般投影变换的标准方法是用w坐标增加(x,y) 2-D坐标(以获得齐次坐标 [x,y,w] ),并使用3x3矩阵转变他们。

It is not clear from your question exactly how you are currently trying to use your points, but I suspect that there is some confusion with how to use the extra coordinate. 从您的问题中不清楚您目前是如何尝试使用您的积分,但我怀疑如何使用额外坐标存在一些混淆。 Mathematically, the thing to remember is that you can multiply the entire homogeneous vector by an arbitrary nonzero scaling factor (because in the end dividing by the third coordinate will cancel out the scaling factor). 在数学上,要记住的是,您可以将整个齐次向量乘以任意非零比例因子(因为最后除以第三个坐标将抵消缩放因子)。 However, it is sometimes hard to figure out what this means in a practical sense... 但是,有时很难弄清楚这在实际意义上意味着什么......

For this problem, set up the system as follows (I am treating homogeneous coordinates as row vectors here, to match your reference): 对于此问题,请按如下方式设置系统(我将齐次坐标视为行向量,以匹配您的参考):

Find a 3x3 matrix T, such that:
  it maps each point b to corresponding point a:  a = b  T
  specifically:

     w' * [a_x a_y 1] = w * [b_x b_y 1] * [ T_xx T_xy T_xw ]
                                          [ T_yx T_yy T_yw ]
                                          [ T_wx T_wy T_ww ]

The tricky bit: you can't just set w and w' to 1. This is because a projective transformation T doesn't necessarily leave the third coordinate unchanged ! 棘手的一点:你不能只将ww'设置为1.这是因为投影变换T 不一定会保持第三个坐标不变 (If it did, you could skip the homogeneous bit altogether, and get an affine transform using only three point pairs...). (如果确实如此,你可以完全跳过同质位,并仅使用三个点对进行仿射变换......)。

One way to express this (in a way that can be solved readily) is to add a parameter, K = w' / w . 表达这种情况的一种方式(以易于解决的方式)是添加参数K = w' / w Then you can express the above as a linear system, as follows: 然后你可以将上面表达为一个线性系统,如下:

for each point b and corresponding point a,
  [a_x a_y 1] * K = [b_x b_y 1] * [ T_xx T_xy T_xw ]
                                  [ T_yx T_yy T_yw ]
                                  [ T_wx T_wy T_ww ]

Remember, all the a and b values are fixed constants ; 请记住,所有ab值都是固定常数 ; you're trying to solve for the nine elements of your T matrix. 你正试图解决T矩阵的九个元素。 Each point pair adds three constraints (from the vector equality above) and one additional parameter (the K for that equation), for a total of 4*3=12 constraints, and 9+4=13 parameters. 每个点对添加三个约束(来自上面的向量相等)和一个附加参数(该等式的K),总共4 * 3 = 12个约束,9 + 4 = 13个参数。 So, we're missing one constraint here... 所以,我们在这里缺少一个约束......

An additional constraint is needed because the matrix T effectively has an arbitrary scaling factor: it is mapping between homogeneous coordinates (which all divide out an arbitrary scaling factor anyway), so there is no inherent way to decide what this scaling factor is. 需要一个额外的约束,因为矩阵T有效地具有任意缩放因子:它是齐次坐标之间的映射(无论如何都将任意比例因子分开),因此没有固有的方法来决定这个比例因子是什么。 One way to fix this factor is to arbitrarily set T_ww = 1 : 解决这个因素的一种方法是任意设置T_ww = 1

for each point pair (a, b):
  [a_x a_y 1] * K = [b_x b_y 1] * [ T_xx T_xy T_xw ]
                                  [ T_yx T_yy T_yw ]
                                  [ T_wx T_wy  1   ]

This gives you a fully-determined linear system: 这为您提供了一个完全确定的线性系统:

  0  = b1_x*T_xx + b1_y*T_yx + 1*T_wx - a1_x*K1
  0  = b1_x*T_xy + b1_y*T_yy + 1*T_wy - a1_y*K1
-1*1 = b1_x*T_xw + b1_y*T_yw          -    1*K1

  0  = b2_x*T_xx + b2_y*T_yx + 1*T_wx - a2_x*K2
  0  = b2_x*T_xy + b2_y*T_yy + 1*T_wy - a2_y*K2
-1*1 = b2_x*T_xw + b2_y*T_yw          -    1*K2

  0  = b3_x*T_xx + b3_y*T_yx + 1*T_wx - a3_x*K3
  0  = b3_x*T_xy + b3_y*T_yy + 1*T_wy - a3_y*K3
-1*1 = b3_x*T_xw + b3_y*T_yw          -    1*K3

  0  = b4_x*T_xx + b4_y*T_yx + 1*T_wx - a4_x*K4
  0  = b4_x*T_xy + b4_y*T_yy + 1*T_wy - a4_y*K4
-1*1 = b4_x*T_xw + b4_y*T_yw          -    1*K4

You now have 12 linear equations in 12 variables (matrix elements T_** and additional scaling parameters K* ). 您现在在12个变量中有12个线性方程(矩阵元素T_**和附加缩放参数K* )。 Use a linear algebra library to solve the system and get your results (assuming you don't feel like writing your own solver...). 使用线性代数库来解决系统并获得结果(假设您不想编写自己的求解器......)。

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

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