以下为个人学习笔记整理,涉及坐标内容统一用右手坐标系,课程官网

# Transformation

# 常见的 2D 矩阵变换(2D Transforms)

# 大小变换(Scale)

各个坐标放大或者缩小

image-20201228111755203

缩放矩阵:[s00s][s00s][xy]=[xy]\text{缩放矩阵:} \begin{bmatrix} s & 0 \\ 0 & s \end{bmatrix} \quad \to \quad \begin{bmatrix} s & 0 \\ 0 & s \end{bmatrix} \begin{bmatrix} x \\ y \end{bmatrix} = \begin{bmatrix} x^{\prime} \\ y^{\prime} \end{bmatrix}

# 翻转(Reflection)

各个坐标取反

image-20201228112301843

翻转矩阵:[1001][1001][xy]=[xy]\text{翻转矩阵:} \begin{bmatrix} -1 & 0 \\ 0 & 1 \end{bmatrix} \quad \to \quad \begin{bmatrix} -1 & 0 \\ 0 & 1 \end{bmatrix} \begin{bmatrix} x \\ y \end{bmatrix} = \begin{bmatrix} x^{\prime} \\ y^{\prime} \end{bmatrix}

# 切变(Shear)

y 坐标不变,x 坐标水平移动,移动距离取决于矩阵右上角的值(ay)

image-20201228112706339

切变矩阵:[1a01][1a01][xy]=[x+ayy]=[xy]\text{切变矩阵:} \begin{bmatrix} 1 & a \\ 0 & 1 \end{bmatrix} \quad \to \quad \begin{bmatrix} 1 & a \\ 0 & 1 \end{bmatrix} \begin{bmatrix} x \\ y \end{bmatrix} = \begin{bmatrix} x + ay \\ y \end{bmatrix} = \begin{bmatrix} x^{\prime} \\ y^{\prime} \end{bmatrix}

# 旋转(Rotate)

默认情况下,绕着原点(0,0)逆时针进行旋转

image-20201228113715108

[abcd][01]=[sinθcosθ]b=sinθ,d=cosθ\begin{bmatrix} a & b \\ c & d \end{bmatrix} \begin{bmatrix} 0 \\ 1 \end{bmatrix} = \begin{bmatrix} - \sin \theta \\ \cos \theta \end{bmatrix} \quad \to \quad b = - \sin \theta \quad, d = \cos \theta

[abcd][10]=[cosθsinθ]a=cosθ,c=sinθ\begin{bmatrix} a & b \\ c & d \end{bmatrix} \begin{bmatrix} 1 \\ 0 \end{bmatrix} = \begin{bmatrix} \cos \theta \\ \sin \theta \end{bmatrix} \quad \to \quad a = \cos \theta \quad, c = \sin \theta

旋转矩阵:[cosθsinθsinθcosθ]\text{旋转矩阵:} \begin{bmatrix} \cos \theta & -\sin \theta \\ \sin \theta & \cos \theta \end{bmatrix}

# 线性变换规则(Linear Transforms)

任何一个坐标,如果可以通过一个矩阵进行变换得到,那么称之为「线性变换」

x=Mx[xy]=[abcd][xy]x^{\prime} = Mx \quad \to \quad \begin{bmatrix} x^{\prime} \\ y^{\prime} \end{bmatrix} = \begin{bmatrix} a & b \\ c & d \end{bmatrix} \begin{bmatrix} x \\ y \end{bmatrix}

x=ax+byx^{\prime} = ax + by

y=cx+dyy^{\prime} = cx + dy

# 齐次坐标(Homogeneous coordinates)

引入「齐次坐标」的目的,是为了解决「平移矩阵」,常规的平移,没用办法通过「线性变换」得到

image-20201228120339767

x=x+txx^{\prime} = x + t_x

y=y+tyy^{\prime} = y + t_y

[xy]=[abcd][xy]+[txty]\begin{bmatrix} x^{\prime} \\ y^{\prime} \end{bmatrix} = \begin{bmatrix} a & b \\ c & d \end{bmatrix} \begin{bmatrix} x \\ y \end{bmatrix} + \begin{bmatrix} t_{x} \\ t_{y} \end{bmatrix}

txt_xtyt_y 没办法通过 ax+byax + by 的形式得到,它是一个固定值

# 新增一个 w 坐标维度

为了能够实现平移的效果,又可以让整体计算变得统一,所以引入了一个新的 w 坐标维度。

  • 2D 平面内的向量: (x,y,0)T(x,y,0)^T
  • 2D 平面内的点:(x,y,1)T(x,y,1)^T
# 为什么向量的齐次坐标是 (x,y,0)T(x,y,0)^T
  • vector + vector = vector
  • point - point = vector
  • point + vector = point
  • point + point = point(这两个点的中点)

向量的第三维是 0,是为了保证平移操作不会影响向量本身的方向和值。

# 为什么点的其次坐标是(x,y,1)T(x,y,1)^T

其实也可以非 1,但是最终都可以转换成为 1 的情况。只要 w 不为 0(为 0 的情况下视作向量)

# 齐次坐标下的平移操作

(xyw)=(10tx01ty001)(xy1)=(x+txy+ty1)\begin{pmatrix} x^{\prime} \\ y^{\prime} \\ w^{\prime} \end{pmatrix} = \begin{pmatrix} 1 & 0 & t_x \\ 0 & 1 & t_y \\ 0 & 0 & 1 \end{pmatrix} \begin{pmatrix} x \\ y \\ 1 \end{pmatrix} = \begin{pmatrix} x + t_x \\ y + t_y \\ 1 \end{pmatrix}

# 齐次坐标下的其他操作:

任何仿射变换(Affine Transformations)都可以转换成 齐次坐标变换

仿射变换:(xy)=(abcd)(xy)+(txty)\text{仿射变换:} \begin{pmatrix} x^{\prime} \\ y^{\prime} \end{pmatrix} = \begin{pmatrix} a & b \\ c & d \end{pmatrix} \begin{pmatrix} x \\ y \end{pmatrix} + \begin{pmatrix} t_x \\ t_y \end{pmatrix}

齐次坐标变换:(xy1)=(abtxcdty001)(xy1)\text{齐次坐标变换:} \begin{pmatrix} x^{\prime} \\ y^{\prime} \\ 1 \end{pmatrix} = \begin{pmatrix} a & b & t_x \\ c & d & t_y \\ 0 & 0 & 1 \end{pmatrix} \begin{pmatrix} x \\ y \\ 1 \end{pmatrix}

注意,所有的变换都是先处理线性变换 (x,y)T\cdot (x,y)^T,再处理平移 +(tx,ty)T+ (t_x,t_y)^T

# Scale

缩放:S(sx,sy)=(sx000sy0001)\text{缩放:} \quad S_{(s_x,s_y)} = \begin{pmatrix} s_x & 0 & 0 \\ 0 & s_y & 0 \\ 0 & 0 & 1 \end{pmatrix}

# Rotation

旋转:R(α)=(cosαsinα0sinαcosα0001)\text{旋转:} \quad R_{(\alpha)} = \begin{pmatrix} \cos \alpha & -\sin \alpha & 0 \\ \sin \alpha & \cos \alpha & 0 \\ 0 & 0 & 1 \end{pmatrix}

旋转:R(α)=(cosαsinα0sinαcosα0001)\text{旋转:} \quad R_{(-\alpha)} = \begin{pmatrix} \cos \alpha & \sin \alpha & 0 \\ -\sin \alpha & \cos \alpha & 0 \\ 0 & 0 & 1 \end{pmatrix}

R(α)T=R(α){R_{(\alpha)}}^T = R_{(-\alpha)}

# Translation

平移:T(tx,ty)=(10tx01ty001)\text{平移:} \quad T_{(t_x, t_y)} = \begin{pmatrix} 1 & 0 & t_x \\ 0 & 1 & t_y \\ 0 & 0 & 1 \end{pmatrix}

# 多次变换的情况下:

操作顺序从右到左依次进行。

先旋转45度后,再平移1个单位:T(1,0)R45[xy1]=[101010001][cos45sin450sin45cos450001][xy1]\text{先旋转45度后,再平移1个单位:} \quad T_{(1,0)} \cdot R_{45} \begin{bmatrix} x \\ y \\ 1 \end{bmatrix} = \begin{bmatrix} 1 & 0 & 1 \\ 0 & 1 & 0 \\ 0 & 0 & 1 \end{bmatrix} \begin{bmatrix} cos45^{\circ} & -sin45^{\circ} & 0 \\ sin45^{\circ} & cos45^{\circ} & 0 \\ 0 & 0 & 1 \end{bmatrix} \begin{bmatrix} x \\ y \\ 1 \end{bmatrix}

# 如何围绕某个点进行旋转?

可以看成是先对图像进行平移,在进行旋转,再进行平移

image-20201228155148332

T(c)R(α)T(c)T_{(c)} \cdot R_{(\alpha)} \cdot T_{(-c)}

# 关键字

  • 仿射变换(Affine Transformations):线性变换 (缩放,旋转,翻转、切变)+ 平移

  • 正交矩阵:矩阵 = 矩阵的逆 R_{- \theta} = {R_{\theta}}^