PRCV 期末复习笔记
章节目录:
2D-图像变换
对于图像的变换操作一共可以分成两类:Filtering 和 Warping,前者修改图像像素值,后者改变图像的空间结构,本节内容主要围绕 2D geometric transformation 展开
Warping 定义如下:
$$(x,y) \to (x',y')$$齐次坐标(heterogeneous coordinate)
齐次坐标的定义很简单:
$$ \begin{bmatrix}x\\ y\\ w\end{bmatrix} = \begin{bmatrix} \frac{x}{w}\\ \frac{y}{w} \end{bmatrix} $$所有满足齐次关系的点都表示二维坐标中的同一个点,在引入了第三维度后,我们可以将所有常见的图像变换方式表示为矩阵乘法
仿射变换(affine transform)
$$ \begin{bmatrix}x'\\ y'\\ 1\end{bmatrix} = \begin{bmatrix}a & b & t_x\\ c & d & t_y\\ 0 & 0 & 1\end{bmatrix} \begin{bmatrix}x\\ y\\ 1\end{bmatrix} $$展开为
$$ \begin{aligned} x' &= ax + by + t_x \\ y' &= cx + dy + t_y \end{aligned} $$仿射变换可以包含:非均匀缩放、旋转、平移、剪切
自由度:6 DOF(degree of freedom)
affine transform 的性质
1.原点不一定映射到原点
2.直线依然为直线
3.平行关系保留
4.比例关系保留(同一条直线上)
5.仿射变换的复合依然是仿射变换
6.Does the last coordinate w ever change?
NO!所以仿射变换并不会引入透视效果
仿射变换的矩阵分解
可以使用仿射变换来表示图像的旋转、缩放、平移、剪切等操作
旋转
$$ \begin{bmatrix}x'\\ y'\\ 1\end{bmatrix} = \begin{bmatrix}\cos\theta & -\sin\theta & 0\\ \sin\theta & \cos\theta & 0\\ 0 & 0 & 1\end{bmatrix} \begin{bmatrix}x\\ y\\ 1\end{bmatrix} $$自由度:1 DOF
Flip
$$ \begin{bmatrix}x'\\ y'\\ 1\end{bmatrix} = \begin{bmatrix}-1 & 0 & 0\\ 0 & 1 & 0\\ 0 & 0 & 1\end{bmatrix} \begin{bmatrix}x\\ y\\ 1\end{bmatrix} $$缩放
$$ \begin{bmatrix}x'\\ y'\\ 1\end{bmatrix} = \begin{bmatrix}s_x & 0 & 0\\ 0 & s_y & 0\\ 0 & 0 & 1\end{bmatrix} \begin{bmatrix}x\\ y\\ 1\end{bmatrix} $$自由度:2 DOF
当 $s_x = s_y$ 时为均匀缩放,否则为非均匀缩放
平移
$$ \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} $$自由度:2 DOF
剪切
$$ \begin{bmatrix}x'\\ y'\\ 1\end{bmatrix} = \begin{bmatrix}1 & k_x & 0\\ k_y & 1 & 0\\ 0 & 0 & 1\end{bmatrix} \begin{bmatrix}x\\ y\\ 1\end{bmatrix} $$自由度:2 DOF
注意:当多个矩阵变换叠加时,不可以随便交换顺序(矩阵乘法不满足交换律),不同的顺序会得到不同的结果
其他变换
Euclidean / Rigid Transform
欧式变换/刚体变换,包含旋转和平移两种操作
$$ \begin{bmatrix}x'\\ y'\\ 1\end{bmatrix} = \begin{bmatrix}\cos\theta & -\sin\theta & t_x\\ \sin\theta & \cos\theta & t_y\\ 0 & 0 & 1\end{bmatrix} \begin{bmatrix}x\\ y\\ 1\end{bmatrix} $$自由度:3 DOF
Similarity Transform
相似变换,包含旋转、平移、缩放三种操作
$$ \begin{bmatrix}x'\\ y'\\ 1\end{bmatrix} = \begin{bmatrix}s\cos\theta & -s\sin\theta & t_x\\ s\sin\theta & s\cos\theta & t_y\\ 0 & 0 & 1\end{bmatrix} \begin{bmatrix}x\\ y\\ 1\end{bmatrix} $$自由度:4 DOF
Affine Transform 求解
问题的定义:已知两组点之间的对应关系,如何求解仿射变换矩阵 $A$?
从自由度角度分析,因为放射变换是 6 DOF 的变换,所以我们至少需要 3 个不共线的点来求解这个变换矩阵,一个点(x,y)可以干掉两个自由度。
三点情形
$$ \begin{bmatrix}x_1'\\ y_1'\\ 1\end{bmatrix} = \begin{bmatrix}a & b & t_x\\ c & d & t_y\\ 0 & 0 & 1\end{bmatrix} \begin{bmatrix}x_1\\ y_1\\ 1\end{bmatrix} $$由于最后一行始终表示 1 = 1,我们可以丢弃第三行然后将上面的方程重新表示成如下形式
$$ \begin{bmatrix} x_1 & y_1 & 1 & 0 & 0 & 0\\ 0 & 0 & 0 & x_1 & y_1 & 1\\ x_2 & y_2 & 1 & 0 & 0 & 0\\ 0 & 0 & 0 & x_2 & y_2 & 1\\ x_3 & y_3 & 1 & 0 & 0 & 0\\ 0 & 0 & 0 & x_3 & y_3 & 1 \end{bmatrix} \begin{bmatrix}a\\ b\\ t_x\\ c\\ d\\ t_y\end{bmatrix} = \begin{bmatrix}x_1'\\ y_1'\\ x_2'\\ y_2'\\ x_3'\\ y_3'\end{bmatrix} $$$$ A = \begin{bmatrix} x_1 & y_1 & 1 & 0 & 0 & 0\\ 0 & 0 & 0 & x_1 & y_1 & 1\\ x_2 & y_2 & 1 & 0 & 0 & 0\\ 0 & 0 & 0 & x_2 & y_2 & 1\\ x_3 & y_3 & 1 & 0 & 0 & 0\\ 0 & 0 & 0 & x_3 & y_3 & 1 \end{bmatrix} m = \begin{bmatrix}a\\ b\\ t_x\\ c\\ d\\ t_y\end{bmatrix} b = \begin{bmatrix}x_1'\\ y_1'\\ x_2'\\ y_2'\\ x_3'\\ y_3'\end{bmatrix} $$原始问题可以表示为 $Am = b$
多于三个点(存在噪声)
此时使用最小二乘估计来求解,因为不存在一个满足所有坐标的仿射变换
目标:
$$ \min_m \sum_{i=1} \left\| \begin{bmatrix} x_i' \\ y_i' \end{bmatrix} - \begin{bmatrix} \hat{x}_i' \\ \hat{y}_i' \end{bmatrix} \right\|^2 $$即 $\min_m \|Am - b\|^2$
这个时候则可以引入正规方程来对问题求解
$$A^TA m = A^Tb$$正规方程的推导
$$ \begin{aligned} \mathcal{L} &= \|Am - b\|^2 \\ &= (Am - b)^T(Am - b) \\ &= (m^TA^T - b^T)(Am - b) \\ &= m^TA^TA m - 2b^TA m + b^Tb \end{aligned} $$ 对 $m$ 求导并令其为 0 $$ \begin{aligned} \frac{\partial \mathcal{L}}{\partial m} &= 2A^TA m - 2A^Tb \\ &= 0 \end{aligned} $$ 得到正规方程 $A^TA m = A^Tb$对于最后表达式的理解也很有趣,精确求解的情况是 b 恰好在 A 张成的列空间中,那么最有解应该满足残差与 $Col(A)$ 正交,即 $A^T(b - Am) = 0$,从而得到正规方程
单应性变换 Homography
定义 & 性质
Homography 是从一个二维平面到另一个二维平面的映射,通常用于图像的透视变换。它可以表示为一个 3x3 的矩阵
$$ x' \sim Hx \quad (在齐次坐标下相等) $$其中
$$ x = \begin{bmatrix}x\\ y\\ 1\end{bmatrix}, \quad x' = \begin{bmatrix}x'\\ y'\\ 1\end{bmatrix}, \quad H = \begin{bmatrix}h_{1} & h_{2} & h_{3}\\ h_{4} & h_{5} & h_{6}\\ h_{7} & h_{8} & h_{9}\end{bmatrix} $$自由度:虽然 H 有 9 个参数,但实际自由度是 8,因为 $H$ 和 $kH$(k 为非零常数)表示同一个单应性变换
坐标变换过程:
单应性变换相比于 affine transform,最后一维的 w 不再恒定不变,所以它可以模拟透视效果,平行关系不再保留
$$ w' = h_{7}x + h_{8}y + h_{9} $$所以最后归一化得到最终的坐标为
$$ \begin{aligned} x' &= \frac{h_{1}x + h_{2}y + h_{3}}{h_{7}x + h_{8}y + h_{9}} \\ y' &= \frac{h_{4}x + h_{5}y + h_{6}}{h_{7}x + h_{8}y + h_{9}} \end{aligned} $$性质:
1. 直线依然为直线 2. 平行关系不保留 3. 比例关系不保留应用
单应性变换有三种典型的使用情景:
- 拍摄对象本身处于同一个平面上
- 场景很远,且深度变化小
- 相机只有旋转没有平移(Insta360 的 X 系列相机)