嗨,我正在尝试创建一个仿射变换,让我将一个三角形变成另一个.我有两个三角形的坐标.你可以帮我吗?
按照亚当·罗森菲尔德的答案,我想出了这个代码,万一有人无聊自己解决方程式:
public static AffineTransform createTransform(ThreePointSystem source,ThreePointSystem dest) { double x11 = source.point1.getX(); double x12 = source.point1.getY(); double x21 = source.point2.getX(); double x22 = source.point2.getY(); double x31 = source.point3.getX(); double x32 = source.point3.getY(); double y11 = dest.point1.getX(); double y12 = dest.point1.getY(); double y21 = dest.point2.getX(); double y22 = dest.point2.getY(); double y31 = dest.point3.getX(); double y32 = dest.point3.getY(); double a1 = ((y11-y21)*(x12-x32)-(y11-y31)*(x12-x22))/ ((x11-x21)*(x12-x32)-(x11-x31)*(x12-x22)); double a2 = ((y11-y21)*(x11-x31)-(y11-y31)*(x11-x21))/ ((x12-x22)*(x11-x31)-(x12-x32)*(x11-x21)); double a3 = y11-a1*x11-a2*x12; double a4 = ((y12-y22)*(x12-x32)-(y12-y32)*(x12-x22))/ ((x11-x21)*(x12-x32)-(x11-x31)*(x12-x22)); double a5 = ((y12-y22)*(x11-x31)-(y12-y32)*(x11-x21))/ ((x12-x22)*(x11-x31)-(x12-x32)*(x11-x21)); double a6 = y12-a4*x11-a5*x12; return new AffineTransform(a1,a4,a2,a5,a3,a6); }
解决方法
我会假设你在这里谈论2D.仿射变换矩阵中有9个值:
| a1 a2 a3 | A = | a4 a5 a6 | | a7 a8 a9 |
有3个输入顶点x1,x2和x3,当变换后应该变为y1,y2,y3.然而,由于我们正在使用齐次坐标,所以将A应用于x1并不一定会给出y1 – 它给出y1的倍数.所以,我们也有未知的乘法器k1,k2和k3,用等式:
A*x1 = k1*y1 A*x2 = k2*y2 A*x3 = k3*y3
每个都是一个向量,所以我们在12个未知数中真的有9个方程,所以解决方案将是不受约束的.如果我们要求a7 = 0,a8 = 0和a9 = 1,那么解将是唯一的(这个选择是自然的,因为这意味着如果输入点是(x,y,1),那么输出点将总是具有均匀坐标1,所以得到的变换只是一个2×2变换加一个转换).
因此,这将方程式减少到:
a1*x11 + a2*x12 + a3 = k1*y11 a4*x11 + a5*x12 + a6 = k1*y12 1 = k1 a1*x21 + a2*x22 + a3 = k2*y21 a4*x21 + a5*x22 + a6 = k2*y22 1 = k2 a1*x31 + a2*x32 + a3 = k3*y31 a4*x31 + a5*x32 + a6 = k3*y32 1 = k3
因此,k1 = k2 = k3 = 1将这些插入并转换为矩阵形式得到:
| x11 x12 1 0 0 0 | | a1 | | y11 | | x21 x22 1 0 0 0 | | a2 | | y21 | | x31 x32 1 0 0 0 | * | a3 | = | y31 | | 0 0 0 x11 x12 1 | | a4 | | y12 | | 0 0 0 x21 x22 1 | | a5 | | y22 | | 0 0 0 x31 x32 1 | | a6 | | y32 |
解决这个6×6方程组会产生您的仿射变换矩阵A.当且仅当源三角形的3点不共线时,它将具有唯一的解.