本文介绍了把四边形变成长方形?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的场景由一个任意四边形组成.我需要能够将该四边形转换为矩形.每个四边形都在 2d 坐标中,因此它们有 4 个顶点 (x_i, y_i).

I have scene composed of one arbitrary quadrilateral. I need to be able to transform that quadrilateral into a rect. Each quad is in 2d coordinates, so they have 4 vertex (x_i, y_i).

转换需要有一个逆,因为这个想法是在操纵矩形后回到原来的四边形.

The transformation need to have an inverse because the idea is to go back to the original quad after manipulating the rectangle.

执行此操作的最简单方法是什么?我听说这叫做透视变换,但我发现了一些小线索,让我觉得这很容易做到.

What would be the easiest way to perform this operation ? I've heard it's called a perspective transformation, but I've found some small clues that lead me to think this could be quite easy to do.

推荐答案

你知道你想要的矩形的尺寸是多少吗?如果是这种情况,您可以使用透视变换将任何凸四边形映射到具有可逆变换的矩形.您所要做的就是获得4个对应点(在四边形和矩形之间),例如,(X,Y),(X,Y), (X,Y), (X,Y) 对应于四边形和对应的 (x,y), (x,y), (x,y), (x,y)对于矩形.然后将其代入 Borealid's 链接中的最后一个等式,您就设置好了:

Do you know what the size of the desired rectangle is? You can map any convex quadrilateral to a rectangle with an invertible transformation with a perspective transformation if this is the case. All you have to do is get 4 corresponding points (between the quadrilateral and the rectangle), say, (X,Y), (X,Y), (X,Y), (X,Y) for the quadrilateral and correspondingly (x,y), (x,y), (x,y), (x,y) for the rectangle. Then plug it into the final equation in Borealid's link and you're set:

上述方程的解(其中n = 4)将为您提供可逆透视变换矩阵的元素(a,b,c,d,e,...,h),

The solution of the above equation (where n = 4) will give you the elements (a,b,c,d,e,...,h) of the invertible perspective transformation matrix,

这将允许您将矩形上的点转换为四边形上的点.对于反向变换,只需反转变换矩阵.

This will allow you to transform the points on the rectangle to the points on the quadrilateral. For the reverse transformation, just invert the transformation matrix.

另请注意,一旦获得变换坐标的向量 [XW YW W],您需要对其进行归一化,使 W = 1.即,您的最终答案是 [XW/W YW/WW/W] 等于 [XY 1],即所需的答案.

Also note that once you obtain the vector [XW YW W] of transformed coordinates, you need to normalize it such that W = 1. I.e., your final answer is [XW/W YW/W W/W] which is equal to [X Y 1], the desired answer.

这篇关于把四边形变成长方形?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-15 01:13