从仿射矩阵得到旋转量平移量缩放量
仿射变换原理
仿射变换是一种线性变换,可以包括平移、旋转、缩放和剪切等操作。其一般公式可以表示为:
$$\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 ) \begin{pmatrix} x' \\ y' \end{pmatrix} = \begin{pmatrix} a & b \\ c & d \end{pmatrix} \begin{pmatrix} x \\ y \end{pmatrix} + \begin{pmatrix} e \\ f \end{pmatrix} (x′y′)=(acbd)(xy)+(ef)
其中:
- (a, b, c, d) 是仿射变换矩阵 (A) 的元素。
- (e, f) 是平移向量 (\mathbf{b}) 的元素。
齐次坐标下:
( x ′ y ′ 1 ) = ( a b e c d f 0 0 1 ) ( x y 1 ) \begin{pmatrix} x' \\ y' \\ 1 \end{pmatrix} = \begin{pmatrix} a & b & e \\ c & d & f \\ 0 & 0 & 1 \end{pmatrix} \begin{pmatrix} x \\ y \\ 1 \end{pmatrix} x′y′1 = ac0bd0ef1 xy1
仿射变换是一种在二维或三维空间中保持点之间相对位置的几何变换。它包括平移、旋转、缩放和剪切等基本变换。仿射变换可以用矩阵表示,并且可以通过矩阵乘法进行组合。在计算机图形学和图像处理中,仿射变换是常用的技术。
齐次坐标
齐次坐标是一种将二维或三维坐标扩展到更高维度的方法,以便于表示平移变换。在二维空间中,齐次坐标是将 ( 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 ] \begin{bmatrix} x' \\ y' \\ 1 \end{bmatrix}=\begin{bmatrix} 1 & 0 & t_x \\ 0 & 1 & t_y \\ 0 & 0 & 1 \end{bmatrix}\begin{bmatrix} x \\ y \\ 1 \end{bmatrix} x′y′1 = 100010txty1 xy1
通过使用齐次坐标,我们可以将所有的仿射变换统一表示为矩阵乘法,从而简化了变换的组合和计算。
基本变换
仿射变换的公式可以通过线性代数的基本原理推导出来。例如,旋转矩阵的推导基于三角函数和线性组合。
组合所有基本仿射变换(平移、旋转、缩放和剪切)的总变换可以通过矩阵乘法来实现。为了简化计算,我们使用齐次坐标。在二维空间中,每个变换都可以表示为一个 3x3 矩阵。组合这些变换时,我们需要按照特定的顺序应用这些矩阵。
假设我们有一个点 P ( x , y ) P(x, y) P(x,y),我们想要先对其进行缩放,然后旋转,接着平移,最后进行剪切。变换的顺序很重要,因为它会影响最终结果。
-
缩放矩阵 S S S:
S = [ s x 0 0 0 s y 0 0 0 1 ] S = \begin{bmatrix} s_x & 0 & 0 \\ 0 & s_y & 0 \\ 0 & 0 & 1 \end{bmatrix} S= sx000sy0001 -
旋转矩阵 R R R:
R = [ cos θ − sin θ 0 sin θ cos θ 0 0 0 1 ] R = \begin{bmatrix} \cos\theta & -\sin\theta & 0 \\ \sin\theta & \cos\theta & 0 \\ 0 & 0 & 1 \end{bmatrix} R= cosθsinθ0−sinθcosθ0001 -
平移矩阵 T T T:
T = [ 1 0 t x 0 1 t y 0 0 1 ] T = \begin{bmatrix} 1 & 0 & t_x \\ 0 & 1 & t_y \\ 0 & 0 & 1 \end{bmatrix} T= 100010txty1 -
斜切矩阵 H H H:
H = [ 1 − sin ( Theta ) 0 0 cos ( Theta ) 0 0 0 1 ] H = \begin{bmatrix} 1 & -\sin(\text{Theta}) & 0 \\ 0 & \cos(\text{Theta}) & 0 \\ 0 & 0 & 1 \end{bmatrix} H= 100−sin(Theta)cos(Theta)0001
组合这些变换的矩阵乘法顺序是从右到左,即最后一个变换矩阵在最右边。因此,总的变换矩阵 M M M 为:
M = H × T × R × S M = H \times T \times R \times S M=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 = \begin{bmatrix} 1 & -\sin(\text{Theta}) & 0 \\ 0 & \cos(\text{Theta}) & 0 \\ 0 & 0 & 1 \end{bmatrix} \times \begin{bmatrix} 1 & 0 & t_x \\ 0 & 1 & t_y \\ 0 & 0 & 1 \end{bmatrix} \times \begin{bmatrix} \cos\theta & -\sin\theta & 0 \\ \sin\theta & \cos\theta & 0 \\ 0 & 0 & 1 \end{bmatrix} \times \begin{bmatrix} s_x & 0 & 0 \\ 0 & s_y & 0 \\ 0 & 0 & 1 \end{bmatrix} M= 100−sin(Theta)cos(Theta)0001 × 100010txty1 × cosθsinθ0−sinθcosθ0001 × sx000sy0001
hom_mat2d_to_affine_par
computes the affine transformation parameters corresponding to the homogeneous 2D transformation matrix HomMat2D. The parameters S_x and S_y determine how the transformation scales the original x- and y-axes, respectively. The two scaling factors are always positive. The angle Theta describes whether the transformed coordinate axes are orthogonal (Theta = 0) or slanted. If (|\Theta| > \pi/2), the transformation contains a reflection. The angle Phi determines the rotation of the transformed x-axis with respect to the original x-axis. The parameters t_x and t_y determine the translation of the two coordinate systems. The matrix HomMat2D can 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} = \begin{bmatrix} 1 & 0 & t_x \\ 0 & 1 & t_y \\ 0 & 0 & 1 \end{bmatrix} \cdot \begin{bmatrix} \cos(\text{Phi}) & -\sin(\text{Phi}) & 0 \\ \sin(\text{Phi}) & \cos(\text{Phi}) & 0 \\ 0 & 0 & 1 \end{bmatrix} \cdot \begin{bmatrix} 1 & -\sin(\text{Theta}) & 0 \\ 0 & \cos(\text{Theta}) & 0 \\ 0 & 0 & 1 \end{bmatrix} \cdot \begin{bmatrix} S_x & 0 & 0 \\ 0 & S_y & 0 \\ 0 & 0 & 1 \end{bmatrix} \\ = \begin{bmatrix} S_x \cos(\text{Phi}) & -S_x \sin(\text{Phi}) \sin(\text{Theta}) + S_y \cos(\text{Phi}) \cos(\text{Theta}) & S_x \sin(\text{Phi}) \cos(\text{Theta}) + S_y \cos(\text{Phi}) \sin(\text{Theta}) + t_x \\ S_y \sin(\text{Phi}) & S_x \cos(\text{Phi}) \cos(\text{Theta}) + S_y \sin(\text{Phi}) \sin(\text{Theta}) & -S_x \cos(\text{Phi}) \sin(\text{Theta}) + S_y \sin(\text{Phi}) \cos(\text{Theta}) + t_y \\ 0 & 0 & 1 \end{bmatrix} HomMat2D= 100010txty1 ⋅ cos(Phi)sin(Phi)0−sin(Phi)cos(Phi)0001 ⋅ 100−sin(Theta)cos(Theta)0001 ⋅ Sx000Sy0001 = Sxcos(Phi)Sysin(Phi)0−Sxsin(Phi)sin(Theta)+Sycos(Phi)cos(Theta)Sxcos(Phi)cos(Theta)+Sysin(Phi)sin(Theta)0Sxsin(Phi)cos(Theta)+Sycos(Phi)sin(Theta)+tx−Sxcos(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} = \begin{bmatrix} 1 & 0 & t_x \\ 0 & 1 & t_y \\ 0 & 0 & 1 \end{bmatrix} \cdot \begin{bmatrix} \cos(\phi) & -\sin(\phi) & 0 \\ \sin(\phi) & \cos(\phi) & 0 \\ 0 & 0 & 1 \end{bmatrix} \cdot \begin{bmatrix} 1 & -\sin(\theta) & 0 \\ 0 & \cos(\theta) & 0 \\ 0 & 0 & 1 \end{bmatrix} \cdot \begin{bmatrix} S_x & 0 & 0 \\ 0 & S_y & 0 \\ 0 & 0 & 1 \end{bmatrix} =\begin{bmatrix} 1 & 0 & t_x \\ 0 & 1 & t_y \\ 0 & 0 & 1 \end{bmatrix} \cdot \begin{bmatrix} \cos(\phi) & -\cos(\phi)\sin(\theta)-\sin(\phi)\cos(\theta) & 0 \\ \sin(\phi) & -\sin(\phi)\sin(\theta)+\cos(\phi)\cos(\theta) & 0 \\ 0 & 0 & 1 \end{bmatrix} \cdot \begin{bmatrix} S_x & 0 & 0 \\ 0 & S_y & 0 \\ 0 & 0 & 1 \end{bmatrix} = \begin{bmatrix} S_x \cos(\phi) & -S_y \cos(\phi)\sin(\theta) -S_y \sin(\phi)\cos(\theta) & t_x \\ S_x \sin(\phi) & -S_y \sin(\phi)\sin(\theta) +S_y \cos(\phi)\cos(\theta) & t_y \\ 0 & 0 & 1 \end{bmatrix} HomMat2D= 100010txty1 ⋅ cos(ϕ)sin(ϕ)0−sin(ϕ)cos(ϕ)0001 ⋅ 100−sin(θ)cos(θ)0001 ⋅ Sx000Sy0001 = 100010txty1 ⋅ cos(ϕ)sin(ϕ)0−cos(ϕ)sin(θ)−sin(ϕ)cos(θ)−sin(ϕ)sin(θ)+cos(ϕ)cos(θ)0001 ⋅ Sx000Sy0001 = Sxcos(ϕ)Sxsin(ϕ)0−Sycos(ϕ)sin(θ)−Sysin(ϕ)cos(θ)−Sysin(ϕ)sin(θ)+Sycos(ϕ)cos(θ)0txty1
已知仿射变换矩阵如下:求变换参数
( x ′ y ′ 1 ) = ( a b e c d f 0 0 1 ) ( x y 1 ) \begin{pmatrix} x' \\ y' \\ 1 \end{pmatrix} = \begin{pmatrix} a & b & e \\ c & d & f \\ 0 & 0 & 1 \end{pmatrix} \begin{pmatrix} x \\ y \\ 1 \end{pmatrix} x′y′1 = ac0bd0ef1 xy1
可得
[ 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 ) \begin{bmatrix} S_x \cos(\phi) & -S_y \cos(\phi)\sin(\theta) -S_y \sin(\phi)\cos(\theta) & t_x \\ S_x \sin(\phi) & -S_y \sin(\phi)\sin(\theta) +S_y \cos(\phi)\cos(\theta) & t_y \\ 0 & 0 & 1 \end{bmatrix} = \begin{pmatrix} a & b & e \\ c & d & f \\ 0 & 0 & 1 \end{pmatrix} Sxcos(ϕ)Sxsin(ϕ)0−Sycos(ϕ)sin(θ)−Sysin(ϕ)cos(θ)−Sysin(ϕ)sin(θ)+Sycos(ϕ)cos(θ)0txty1 = ac0bd0ef1
通过比较矩阵的元素,我们可以得到以下关系:
- S x cos ( ϕ ) = a S_x \cos(\phi) = a Sxcos(ϕ)=a
- − S y cos ( ϕ ) sin ( θ ) − S y sin ( ϕ ) cos ( θ ) = b -S_y \cos(\phi)\sin(\theta) -S_y \sin(\phi)\cos(\theta) = b −Sycos(ϕ)sin(θ)−Sysin(ϕ)cos(θ)=b
- S x sin ( ϕ ) = c S_x \sin(\phi) = c Sxsin(ϕ)=c
- − S y sin ( ϕ ) sin ( θ ) + S y cos ( ϕ ) cos ( θ ) = d -S_y \sin(\phi)\sin(\theta) +S_y \cos(\phi)\cos(\theta) = d −Sysin(ϕ)sin(θ)+Sycos(ϕ)cos(θ)=d
- t x = e t_x = e tx=e
- t y = f t_y = f ty=f
通过这些方程,我们可以解出 S x , S y , ϕ , θ , t x , t y S_x, S_y, \phi, \theta, t_x, t_y Sx,Sy,ϕ,θ,tx,ty。
- S x = a 2 + c 2 S_x = \sqrt{a^2+c^2} Sx=a2+c2
- ϕ = arctan c a \phi = \arctan{\frac{c}{a}} ϕ=arctanac
- θ = arctan d c + a b b c − d a \theta = \arctan{\frac{dc+ab}{bc-da}} θ=arctanbc−dadc+ab
- S y = b 2 + d 2 S_y= \sqrt{b^2 +d^2} Sy=b2+d2
- t x = e t_x = e tx=e
- t y = f t_y = f ty=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^2 Sx2cos2(ϕ)+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) = b −Sysin(θ+ϕ)=b
S y cos ( θ + ϕ ) = d S_y \cos(\theta+\phi) = d Sycos(θ+ϕ)=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^2 Sy2cos2(θ+ϕ)+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}} 1−tanθtanϕtanθ+tanϕ=d−b=1−tanθactanθ+ac