详细介绍:从仿射矩阵得到旋转量平移量缩放量

仿射变换原理

仿射变换是一种线性变换,可以包括平移、旋转、缩放和剪切等操作。其一般公式可以表示为:
$$\mathbf{x’} = A \mathbf{x} + \mathbf{b} ]
其中:

  • (\mathbf{x}) 是输入向量,通常表示一个点在二维或三维空间中的坐标。
  • (\mathbf{x’}) 是输出向量,表示经过仿射变换后的点。
  • (A) 是一个矩阵,称为仿射变换矩阵,它包含了线性变换的部分。
  • 一个向量,称为平移向量,它含有了平移的部分。就是(\mathbf{b})

具体来说,对于二维空间中的点 ((x, y)),仿射变换可能表示为:
( x ′ y ′ ) = ( a b c d ) ( x y ) + ( e f )

(xy)
=
(abcd)
(xy)
+
(ef)
(xy)=(acbd)(xy)+(ef)
其中:

  • (a, b, c, d) 是仿射变换矩阵 (A) 的元素。
  • 平移向量 (\mathbf{b}) 的元素。就是(e, f)

齐次坐标下:
( x ′ y ′ 1 ) = ( a b e c d f 0 0 1 ) ( x y 1 )

(xy1)
=
(abecdf001)
(xy1)
xy1=ac0bd0ef1xy1

仿射变换是一种在二维或三维空间中保持点之间相对位置的几何变换。它包括平移、旋转、缩放和剪切等核心变换。仿射变换可以用矩阵表示,并且可以通过矩阵乘法进行组合。在计算机图形学和图像处理中,仿射变换是常用的技巧。

齐次坐标

齐次坐标是一种将二维或三维坐标扩展到更高维度的途径,以便于表示平移变换。在二维空间中,齐次坐标是将( x , y ) (x, y)(x,y) 扩展为 ( x , y , 1 ) (x, y, 1)(x,y,1)。这使得平移、旋转、缩放等变换都可以用矩阵乘法表示,从而简化了变换的组合和计算。

例如,二维平移的齐次坐标表示为:
[ x ′ y ′ 1 ] = [ 1 0 t x 0 1 t y 0 0 1 ] [ x y 1 ]

[xy1]
=
[10tx01ty001]
[xy1]
xy1=100010txty1xy1

通过启用齐次坐标,我们可以将所有的仿射变换统一表示为矩阵乘法,从而简化了变换的组合和计算。

基本变换

仿射变换的公式可以通过线性代数的基本原理推导出来。例如,旋转矩阵的推导基于三角函数和线性组合。

组合所有基础仿射变换(平移、旋转、缩放和剪切)的总变换可以通过矩阵乘法来实现。为了简化计算,我们使用齐次坐标。在二维空间中,每个变换都可以表示为一个 3x3 矩阵。组合这些变换时,我们需要按照特定的顺序应用这些矩阵。

假设我们有一个点P ( x , y ) P(x, y)P(x,y),我们想要先对其进行缩放,然后旋转,接着平移,最后进行剪切。变换的顺序很关键,缘于它会影响最终结果。

  1. 缩放矩阵S SS
    S = [ s x 0 0 0 s y 0 0 0 1 ] S =

    [sx000sy0001]
    S=sx000sy0001

  2. 旋转矩阵R RR
    R = [ cos ⁡ θ − sin ⁡ θ 0 sin ⁡ θ cos ⁡ θ 0 0 0 1 ] R =

    [cosθsinθ0sinθcosθ0001]
    R=cosθsinθ0sinθcosθ0001

  3. 平移矩阵T TT
    T = [ 1 0 t x 0 1 t y 0 0 1 ] T =

    [10tx01ty001]
    T=100010txty1

  4. 斜切矩阵H HH
    H = [ 1 − sin ⁡ ( Theta ) 0 0 cos ⁡ ( Theta ) 0 0 0 1 ] H =

    [1sin(Theta)00cos(Theta)0001]
    H=100sin(Theta)cos(Theta)0001

组合这些变换的矩阵乘法顺序是从右到左,即最终一个变换矩阵在最右边。因此,总的变换矩阵M MM 为:
M = H × T × R × S M = H \times T \times R \times SM=H×T×R×S

计算该乘积,大家得到:
M = [ 1 − sin ⁡ ( Theta ) 0 0 cos ⁡ ( Theta ) 0 0 0 1 ] × [ 1 0 t x 0 1 t y 0 0 1 ] × [ cos ⁡ θ − sin ⁡ θ 0 sin ⁡ θ cos ⁡ θ 0 0 0 1 ] × [ s x 0 0 0 s y 0 0 0 1 ] M =

[1sin(Theta)00cos(Theta)0001]
\times
[10tx01ty001]
\times
[cosθsinθ0sinθcosθ0001]
\times
[sx000sy0001]
M=100sin(Theta)cos(Theta)0001×100010txty1×cosθsinθ0sinθcosθ0001×sx000sy0001

hom_mat2d_to_affine_par computes the affine transformation parameters corresponding to the homogeneous 2D transformation matrix HomMat2D. The parametersS_x and S_ydetermine how the transformation scales the original x- and y-axes, respectively. The two scaling factors are always positive. The angleThetadescribes whether the transformed coordinate axes are orthogonal (Theta= 0) or slanted. If (|\Theta| > \pi/2), the transformation contains a reflection. The anglePhidetermines the rotation of the transformed x-axis with respect to the original x-axis. The parameterst_x and t_ydetermine the translation of the two coordinate systems. The matrixHomMat2Dcan be constructed from the six transformation parameters by the following operator sequence: This is equivalent to the following chain of transformation matrices:
来源:hom_mat2d_to_affine_par (算子名称)

HomMat2D = [ 1 0 t x 0 1 t y 0 0 1 ] ⋅ [ cos ⁡ ( Phi ) − sin ⁡ ( Phi ) 0 sin ⁡ ( Phi ) cos ⁡ ( Phi ) 0 0 0 1 ] ⋅ [ 1 − sin ⁡ ( Theta ) 0 0 cos ⁡ ( Theta ) 0 0 0 1 ] ⋅ [ S x 0 0 0 S y 0 0 0 1 ] = [ S x cos ⁡ ( Phi ) − S x sin ⁡ ( Phi ) sin ⁡ ( Theta ) + S y cos ⁡ ( Phi ) cos ⁡ ( Theta ) S x sin ⁡ ( Phi ) cos ⁡ ( Theta ) + S y cos ⁡ ( Phi ) sin ⁡ ( Theta ) + t x S y sin ⁡ ( Phi ) S x cos ⁡ ( Phi ) cos ⁡ ( Theta ) + S y sin ⁡ ( Phi ) sin ⁡ ( Theta ) − S x cos ⁡ ( Phi ) sin ⁡ ( Theta ) + S y sin ⁡ ( Phi ) cos ⁡ ( Theta ) + t y 0 0 1 ] \text{HomMat2D} =

[10tx01ty001]
\cdot
[cos(Phi)sin(Phi)0sin(Phi)cos(Phi)0001]
\cdot
[1sin(Theta)00cos(Theta)0001]
\cdot
[Sx000Sy0001]
\\ =
[Sxcos(Phi)Sxsin(Phi)sin(Theta)+Sycos(Phi)cos(Theta)Sxsin(Phi)cos(Theta)+Sycos(Phi)sin(Theta)+txSysin(Phi)Sxcos(Phi)cos(Theta)+Sysin(Phi)sin(Theta)Sxcos(Phi)sin(Theta)+Sysin(Phi)cos(Theta)+ty001]
HomMat2D=100010txty1cos(Phi)sin(Phi)0sin(Phi)cos(Phi)0001100sin(Theta)cos(Theta)0001Sx000Sy0001=Sxcos(Phi)Sysin(Phi)0Sxsin(Phi)sin(Theta)+Sycos(Phi)cos(Theta)Sxcos(Phi)cos(Theta)+Sysin(Phi)sin(Theta)0Sxsin(Phi)cos(Theta)+Sycos(Phi)sin(Theta)+txSxcos(Phi)sin(Theta)+Sysin(Phi)cos(Theta)+ty1

HomMat2D = [ 1 0 t x 0 1 t y 0 0 1 ] ⋅ [ cos ⁡ ( ϕ ) − sin ⁡ ( ϕ ) 0 sin ⁡ ( ϕ ) cos ⁡ ( ϕ ) 0 0 0 1 ] ⋅ [ 1 − sin ⁡ ( θ ) 0 0 cos ⁡ ( θ ) 0 0 0 1 ] ⋅ [ S x 0 0 0 S y 0 0 0 1 ] = [ 1 0 t x 0 1 t y 0 0 1 ] ⋅ [ cos ⁡ ( ϕ ) − cos ⁡ ( ϕ ) sin ⁡ ( θ ) − sin ⁡ ( ϕ ) cos ⁡ ( θ ) 0 sin ⁡ ( ϕ ) − sin ⁡ ( ϕ ) sin ⁡ ( θ ) + cos ⁡ ( ϕ ) cos ⁡ ( θ ) 0 0 0 1 ] ⋅ [ S x 0 0 0 S y 0 0 0 1 ] = [ S x cos ⁡ ( ϕ ) − S y cos ⁡ ( ϕ ) sin ⁡ ( θ ) − S y sin ⁡ ( ϕ ) cos ⁡ ( θ ) t x S x sin ⁡ ( ϕ ) − S y sin ⁡ ( ϕ ) sin ⁡ ( θ ) + S y cos ⁡ ( ϕ ) cos ⁡ ( θ ) t y 0 0 1 ] \text{HomMat2D} =

[10tx01ty001]
\cdot
[cos(ϕ)sin(ϕ)0sin(ϕ)cos(ϕ)0001]
\cdot
[1sin(θ)00cos(θ)0001]
\cdot
[Sx000Sy0001]
=
[10tx01ty001]
\cdot
[cos(ϕ)cos(ϕ)sin(θ)sin(ϕ)cos(θ)0sin(ϕ)sin(ϕ)sin(θ)+cos(ϕ)cos(θ)0001]
\cdot
[Sx000Sy0001]
=
[Sxcos(ϕ)Sycos(ϕ)sin(θ)Sysin(ϕ)cos(θ)txSxsin(ϕ)Sysin(ϕ)sin(θ)+Sycos(ϕ)cos(θ)ty001]
HomMat2D=100010txty1cos(ϕ)sin(ϕ)0sin(ϕ)cos(ϕ)0001100sin(θ)cos(θ)0001Sx000Sy0001=100010txty1cos(ϕ)sin(ϕ)0cos(ϕ)sin(θ)sin(ϕ)cos(θ)sin(ϕ)sin(θ)+cos(ϕ)cos(θ)0001Sx000Sy0001=Sxcos(ϕ)Sxsin(ϕ)0Sycos(ϕ)sin(θ)Sysin(ϕ)cos(θ)Sysin(ϕ)sin(θ)+Sycos(ϕ)cos(θ)0txty1

已知仿射变换矩阵如下:求变换参数
( x ′ y ′ 1 ) = ( a b e c d f 0 0 1 ) ( x y 1 )

(xy1)
=
(abecdf001)
(xy1)
xy1=ac0bd0ef1xy1

可得
[ S x cos ⁡ ( ϕ ) − S y cos ⁡ ( ϕ ) sin ⁡ ( θ ) − S y sin ⁡ ( ϕ ) cos ⁡ ( θ ) t x S x sin ⁡ ( ϕ ) − S y sin ⁡ ( ϕ ) sin ⁡ ( θ ) + S y cos ⁡ ( ϕ ) cos ⁡ ( θ ) t y 0 0 1 ] = ( a b e c d f 0 0 1 )

[Sxcos(ϕ)Sycos(ϕ)sin(θ)Sysin(ϕ)cos(θ)txSxsin(ϕ)Sysin(ϕ)sin(θ)+Sycos(ϕ)cos(θ)ty001]
=
(abecdf001)
Sxcos(ϕ)Sxsin(ϕ)0Sycos(ϕ)sin(θ)Sysin(ϕ)cos(θ)Sysin(ϕ)sin(θ)+Sycos(ϕ)cos(θ)0txty1=ac0bd0ef1
经过比较矩阵的元素,我们可以得到以下关系:

  1. S x cos ⁡ ( ϕ ) = a S_x \cos(\phi) = aSxcos(ϕ)=a
  2. − S y cos ⁡ ( ϕ ) sin ⁡ ( θ ) − S y sin ⁡ ( ϕ ) cos ⁡ ( θ ) = b -S_y \cos(\phi)\sin(\theta) -S_y \sin(\phi)\cos(\theta) = bSycos(ϕ)sin(θ)Sysin(ϕ)cos(θ)=b
  3. S x sin ⁡ ( ϕ ) = c S_x \sin(\phi) = cSxsin(ϕ)=c
  4. − S y sin ⁡ ( ϕ ) sin ⁡ ( θ ) + S y cos ⁡ ( ϕ ) cos ⁡ ( θ ) = d -S_y \sin(\phi)\sin(\theta) +S_y \cos(\phi)\cos(\theta) = dSysin(ϕ)sin(θ)+Sycos(ϕ)cos(θ)=d
  5. t x = e t_x = etx=e
  6. t y = f t_y = fty=f

通过这些方程,我们许可解出S x , S y , ϕ , θ , t x , t y S_x, S_y, \phi, \theta, t_x, t_ySx,Sy,ϕ,θ,tx,ty

  1. S x = a 2 + c 2 S_x = \sqrt{a^2+c^2}Sx=a2+c2
  2. ϕ = arctan ⁡ c a \phi = \arctan{\frac{c}{a}}ϕ=arctanac
  3. θ = arctan ⁡ d c + a b b c − d a \theta = \arctan{\frac{dc+ab}{bc-da}}θ=arctanbcdadc+ab
  4. S y = b 2 + d 2 S_y= \sqrt{b^2 +d^2}Sy=b2+d2
  5. t x = e t_x = etx=e
  6. t y = f t_y = fty=f

其中过程如下:
从方程 (1) 和 (3) 中解出:
S x 2 cos ⁡ 2 ( ϕ ) + S x 2 sin ⁡ 2 ( ϕ ) = S x 2 = a 2 + b 2 S_x^2 \cos^2(\phi) + S_x^2 \sin^2(\phi)=S_x^2= a^2+b^2Sx2cos2(ϕ)+Sx2sin2(ϕ)=Sx2=a2+b2
cos ⁡ ( ϕ ) sin ⁡ ( ϕ ) = tan ⁡ ϕ = c a \frac{\cos(\phi)}{\sin(\phi)}=\tan{\phi}=\frac{c}{a}sin(ϕ)cos(ϕ)=tanϕ=ac

从方程 (2) 和 (4) 中解出:
− S y sin ⁡ ( θ + ϕ ) = b -S_y \sin(\theta+\phi) = bSysin(θ+ϕ)=b
S y cos ⁡ ( θ + ϕ ) = d S_y \cos(\theta+\phi) = dSycos(θ+ϕ)=d
S y 2 cos ⁡ 2 ( θ + ϕ ) + S y 2 sin ⁡ 2 ( θ + ϕ ) = S y 2 = b 2 + d 2 S_y^2 \cos^2(\theta+\phi) + S_y^2 \sin^2(\theta+\phi)=S_y^2= b^2+d^2Sy2cos2(θ+ϕ)+Sy2sin2(θ+ϕ)=Sy2=b2+d2
tan ⁡ θ + tan ⁡ ϕ 1 − tan ⁡ θ tan ⁡ ϕ = − b d = tan ⁡ θ + c a 1 − tan ⁡ θ c a \frac{\tan{\theta}+\tan{\phi}}{1-\tan{\theta}\tan{\phi}}=\frac{-b}{d}=\frac{\tan{\theta}+\frac{c}{a}}{1-\tan{\theta}\frac{c}{a}}1tanθtanϕtanθ+tanϕ=db=1tanθactanθ+ac

posted @ 2025-10-06 08:59  ycfenxi  阅读(8)  评论(0)    收藏  举报