把一个三角形变换成另一个三角形

时间:2022-12-04 19:04:43

Hi i am trying to create the affine transform that will allow me to transform a triangle into another one. What i have are the coordinates for the 2 triangles. Can you help me?

你好,我正在尝试创建仿射变换,它将允许我将一个三角形转换成另一个三角形。我有两个三角形的坐标。你能帮我吗?

Following the answer by Adam Rosenfield i came up with this code in case anyone is bored to solve the equation himself :

在亚当·罗森菲尔德的回答之后,我提出了这个代码,以防有人无聊地自己解决这个方程:

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);
}

4 个解决方案

#1


12  

I'm going to assume you're talking about 2D here. An affine transformation matrix has 9 values in it:

我假设你说的是2D。一个仿射变换矩阵有9个值:

    | a1 a2 a3 |
A = | a4 a5 a6 |
    | a7 a8 a9 |

There are 3 input vertices x1, x2, and x3, which when transformed should become y1, y2, y3. However, since we're working in homogeneous coordinates, applying A to x1 doesn't necessarily give y1 -- it gives a multiple of y1. So, we also have the unknown multipliers k1, k2, and k3, with the equations:

有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

Each of those is a vector, so we really have 9 equations in 12 unknowns, so the solution is going to be underconstrained. If we require that a7=0, a8=0, and a9=1, then the solution will be unique (this choice is natural, since it means if the input point is (x, y, 1), then the output point will always have homogeneous coordinate 1, so the resulting transform is just a 2x2 transform plus a translation).

每一个都是一个矢量,所以我们有9个方程,12个未知数,所以解是欠约束的。如果我们要求a7 = 0,a8 = 0,然后a9 = 1,该解决方案将是唯一的(这是自然的选择,因为这意味着如果输入点(x,y,1),然后输出点总有齐次坐标1,所以由此产生的变换是一个2 x2变换+翻译)。

Hence, this reduces the equations to:

因此,这将方程简化为:

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

So, k1 = k2 = k3 = 1. Plugging these in and converting to matrix form yields:

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 |

Solving this 6x6 system of equations yields you your affine transformation matrix A. It will have a unique solution if and only if the 3 points of your source triangle are not collinear.

解这个6x6方程组会得到仿射变换矩阵a,当且仅当源三角形的3个点不共线时,它会有唯一解。

#2


2  

Hey, guys, Without Loss of Generality, make the two triangles have the origin as one vertex (you can tack on the affine shift later), so they're defined by the points 0, a, b, c, d then multiply your points x by the matrix NM

嘿,伙计们,不要失去通用性,让这两个三角形的原点作为一个顶点(稍后你可以加上仿射位移),所以它们由点(0,a, b, c, d)定义然后用x乘以矩阵NM

where

在哪里

M = inverse(a b) <--- this is 2x2 matrix with the points a and b as its columns

M =逆(a b) <---这是一个2x2矩阵,以点a和b为列

and

N = (c d)

N =(c d)

That should do it.

应该做的。

#3


1  

If I understand this correctly, your triangles have the same size and angles, so you should be able to tranform them so that they have (at least) one point in common. After this, they should only differ in rotation or could be mirrored, so you could f.e. get the angles between the triangle lines and try these for rotation and could mirror one of the triangles if none of the angles works.

如果我理解正确的话,你的三角形的大小和角度是一样的,所以你应该可以把它们变形,这样它们至少有一个共同点。在此之后,它们应该只在旋转上有所不同或者可以被镜像,所以你可以得到三角形线之间的夹角,并尝试用它们来旋转,如果没有任何一个角起作用,你就可以镜像其中一个三角形。

EDIT: OK, that's not enough, affine transformations also can contain shear and scaling... Scaling could be done easily, just divide the length of the lines, this will also give you some information about corresponding lines of the triangles, but shearing will be harder...

编辑:好的,这还不够,仿射变换也可以包含剪切和缩放…缩放很容易,只需要划分线条的长度,这也会给你一些关于三角形对应线条的信息,但是剪切会更困难……

OTOH, couldn't you just solve some equation system for this? After all, there should be a transformation matrix and 3 points (new and old)...

OTOH,你不能解一些方程系统吗?毕竟,应该有一个变换矩阵和3个点(新的和旧的)……

#4


1  

Just formule the problem as a set of equations and then solve it:

把这个问题写成一组方程,然后解出来:

P1 * M = P1'
P2 * M = P2'
P3 * M = P3'

M is a 3x3 matrix like:

M是一个3x3矩阵:

[m00, m01, m02;
 m10, m11, m12;
 0  ,   0,   1]

And P_i is a tuple [k*x_i, k*y_i, k] (homogeneous coordinates)...

P_i是一个元组[k*x_i, k*y_i, k](齐次坐标)…

You can now try to expand the 3 matricial equations shown above and make a new system, with the m_ij as incognits and solve it, but if I'm not missing something (and maybe I am), you need one point more to specify completely the transformation, or otherwise you'll have an extra degree of freedom (and of course you can fix it).

你现在可以尝试扩大上面所示的3 matricial方程,使一个新系统,与m_ij incognits并解决它,但是如果我不缺少一些(或许我),你更需要一个点指定完全转换,或者你会有一个额外的*度(当然你可以修复它)。

#1


12  

I'm going to assume you're talking about 2D here. An affine transformation matrix has 9 values in it:

我假设你说的是2D。一个仿射变换矩阵有9个值:

    | a1 a2 a3 |
A = | a4 a5 a6 |
    | a7 a8 a9 |

There are 3 input vertices x1, x2, and x3, which when transformed should become y1, y2, y3. However, since we're working in homogeneous coordinates, applying A to x1 doesn't necessarily give y1 -- it gives a multiple of y1. So, we also have the unknown multipliers k1, k2, and k3, with the equations:

有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

Each of those is a vector, so we really have 9 equations in 12 unknowns, so the solution is going to be underconstrained. If we require that a7=0, a8=0, and a9=1, then the solution will be unique (this choice is natural, since it means if the input point is (x, y, 1), then the output point will always have homogeneous coordinate 1, so the resulting transform is just a 2x2 transform plus a translation).

每一个都是一个矢量,所以我们有9个方程,12个未知数,所以解是欠约束的。如果我们要求a7 = 0,a8 = 0,然后a9 = 1,该解决方案将是唯一的(这是自然的选择,因为这意味着如果输入点(x,y,1),然后输出点总有齐次坐标1,所以由此产生的变换是一个2 x2变换+翻译)。

Hence, this reduces the equations to:

因此,这将方程简化为:

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

So, k1 = k2 = k3 = 1. Plugging these in and converting to matrix form yields:

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 |

Solving this 6x6 system of equations yields you your affine transformation matrix A. It will have a unique solution if and only if the 3 points of your source triangle are not collinear.

解这个6x6方程组会得到仿射变换矩阵a,当且仅当源三角形的3个点不共线时,它会有唯一解。

#2


2  

Hey, guys, Without Loss of Generality, make the two triangles have the origin as one vertex (you can tack on the affine shift later), so they're defined by the points 0, a, b, c, d then multiply your points x by the matrix NM

嘿,伙计们,不要失去通用性,让这两个三角形的原点作为一个顶点(稍后你可以加上仿射位移),所以它们由点(0,a, b, c, d)定义然后用x乘以矩阵NM

where

在哪里

M = inverse(a b) <--- this is 2x2 matrix with the points a and b as its columns

M =逆(a b) <---这是一个2x2矩阵,以点a和b为列

and

N = (c d)

N =(c d)

That should do it.

应该做的。

#3


1  

If I understand this correctly, your triangles have the same size and angles, so you should be able to tranform them so that they have (at least) one point in common. After this, they should only differ in rotation or could be mirrored, so you could f.e. get the angles between the triangle lines and try these for rotation and could mirror one of the triangles if none of the angles works.

如果我理解正确的话,你的三角形的大小和角度是一样的,所以你应该可以把它们变形,这样它们至少有一个共同点。在此之后,它们应该只在旋转上有所不同或者可以被镜像,所以你可以得到三角形线之间的夹角,并尝试用它们来旋转,如果没有任何一个角起作用,你就可以镜像其中一个三角形。

EDIT: OK, that's not enough, affine transformations also can contain shear and scaling... Scaling could be done easily, just divide the length of the lines, this will also give you some information about corresponding lines of the triangles, but shearing will be harder...

编辑:好的,这还不够,仿射变换也可以包含剪切和缩放…缩放很容易,只需要划分线条的长度,这也会给你一些关于三角形对应线条的信息,但是剪切会更困难……

OTOH, couldn't you just solve some equation system for this? After all, there should be a transformation matrix and 3 points (new and old)...

OTOH,你不能解一些方程系统吗?毕竟,应该有一个变换矩阵和3个点(新的和旧的)……

#4


1  

Just formule the problem as a set of equations and then solve it:

把这个问题写成一组方程,然后解出来:

P1 * M = P1'
P2 * M = P2'
P3 * M = P3'

M is a 3x3 matrix like:

M是一个3x3矩阵:

[m00, m01, m02;
 m10, m11, m12;
 0  ,   0,   1]

And P_i is a tuple [k*x_i, k*y_i, k] (homogeneous coordinates)...

P_i是一个元组[k*x_i, k*y_i, k](齐次坐标)…

You can now try to expand the 3 matricial equations shown above and make a new system, with the m_ij as incognits and solve it, but if I'm not missing something (and maybe I am), you need one point more to specify completely the transformation, or otherwise you'll have an extra degree of freedom (and of course you can fix it).

你现在可以尝试扩大上面所示的3 matricial方程,使一个新系统,与m_ij incognits并解决它,但是如果我不缺少一些(或许我),你更需要一个点指定完全转换,或者你会有一个额外的*度(当然你可以修复它)。