变换矩阵是数学线性代数中的一个概念。
在线性代数中,线性变换能够用矩阵表示。如果T是一个把Rn映射到Rm的线性变换,且x是一个具有n个元素的列向量,那么
![T({vec x})={mathbf {A}}{vec x}](https://img2018.cnblogs.com/blog/1470684/201810/1470684-20181019100542494-824537732.png)
我们把m×n的矩阵A,称为T的变换矩阵。
在单位方块上应用各种二维仿射变换矩阵的效果。
最为常用的几何变换都是线性变换,这包括旋转、缩放、切变、反射以及正投影。在二维空间中,线性变换可以用2×2的变换矩阵表示。
旋转
绕原点逆时针旋转 θ 度角的变换公式是
与
,用矩阵表示为:
![{displaystyle {egin{pmatrix}x'\y'end{pmatrix}}={egin{pmatrix}cos heta &-sin heta \sin heta &cos heta end{pmatrix}}{egin{pmatrix}x\yend{pmatrix}}}](https://img2018.cnblogs.com/blog/1470684/201810/1470684-20181019100547481-1793556153.png)
缩放
缩放(反矩阵)公式为
与
,用矩阵表示为:
![{egin{pmatrix}x'\y'end{pmatrix}}={egin{pmatrix}s_{x}&0\0&s_{y}end{pmatrix}}{egin{pmatrix}x\yend{pmatrix}}](https://img2018.cnblogs.com/blog/1470684/201810/1470684-20181019100548707-339450042.png)
切变
切变有两种可能的形式:
平行于 x 轴的切变为
与
,矩阵表示为:
![{egin{pmatrix}x'\y'end{pmatrix}}={egin{pmatrix}1&k\0&1end{pmatrix}}{egin{pmatrix}x\yend{pmatrix}}](https://img2018.cnblogs.com/blog/1470684/201810/1470684-20181019100551490-1547061845.png)
平行于 y 轴的切变为
与
,矩阵表示为:
![{egin{pmatrix}x'\y'end{pmatrix}}={egin{pmatrix}1&0\k&1end{pmatrix}}{egin{pmatrix}x\yend{pmatrix}}](https://img2018.cnblogs.com/blog/1470684/201810/1470684-20181019100553795-1495378281.png)
反射
为了沿经过原点的直线反射向量,假设(ux, uy)为直线方向的单位向量。变换矩阵为:
![{egin{pmatrix}x'\y'end{pmatrix}}={egin{pmatrix}2u_{x}^{2}-1&2u_{x}u_{y}\2u_{x}u_{y}&2u_{y}^{2}-1end{pmatrix}}{egin{pmatrix}x\yend{pmatrix}}](https://img2018.cnblogs.com/blog/1470684/201810/1470684-20181019100554462-839930285.png)
不经过原点的直线的反射是仿射变换,而不是线性变换。
若一座标(x, y)沿直线
进行反射,则其影像(x', y')可用以下公式求得:
![{displaystyle {egin{pmatrix}x'\y'end{pmatrix}}={egin{pmatrix}cos 2 heta &sin 2 heta \sin 2 heta &-cos 2 heta end{pmatrix}}{egin{pmatrix}x\yend{pmatrix}}}](https://img2018.cnblogs.com/blog/1470684/201810/1470684-20181019100555905-525639289.png)
正投影
为了将向量正投影到一条经过原点的直线,假设(ux, uy)是直线方向的单位向量,变换矩阵为:
![{egin{pmatrix}x'\y'end{pmatrix}}={egin{pmatrix}u_{x}^{2}&u_{x}u_{y}\u_{x}u_{y}&u_{y}^{2}end{pmatrix}}{egin{pmatrix}x\yend{pmatrix}}](https://img2018.cnblogs.com/blog/1470684/201810/1470684-20181019100556096-1210544857.png)
跟反射一样,正投影到一条不经过原点的直线的变换是仿射变换,而不是线性变换。
平行投影也是线性变换,也可以用矩阵表示。但是透视投影不是线性变换,必须用齐次坐标表示。
仿射变换
为了表示仿射变换,需要使用齐次坐标,即用三维向量(x, y, 1)表示二维向量,对于高维来说也是如此。按照这种方法,就可以用矩阵乘法表示变换。
;
变为
![{egin{pmatrix}x'\y'\1end{pmatrix}}={egin{pmatrix}1&0&t_{x}\0&1&t_{y}\0&0&1end{pmatrix}}{egin{pmatrix}x\y\1end{pmatrix}}](https://img2018.cnblogs.com/blog/1470684/201810/1470684-20181019100557442-278269946.png)
在矩阵中增加一列与一行,除右下角的元素为1外其它部分填充为0,通过这种方法,所有的线性变换都可以转换为仿射变换。例如,上面的旋转矩阵变为
![{egin{pmatrix}cos heta &-sin heta &0\sin heta &cos heta &0\0&0&1end{pmatrix}}](https://img2018.cnblogs.com/blog/1470684/201810/1470684-20181019100557610-1330730480.png)
通过这种方法,使用与前面一样的矩阵乘积可以将各种变换无缝地集成到一起。
当使用仿射变换时,齐次坐标向量w从来不变,这样可以把它当作为1。但是,透视投影中并不是这样。
透视投影
三维计算机图形学中另外一种重要的变换是透视投影。与平行投影沿着平行线将物体投影到图像平面上不同,透视投影按照从投影中心这一点发出的直线将物体投影到图像平面。这就意味着距离投影中心越远投影越小,距离越近投影越大。
最简单的透视投影将投影中心作为坐标原点,z = 1作为图像平面,这样投影变换为
;
,用齐次坐标表示为:
![{egin{pmatrix}x_{c}\y_{c}\z_{c}\w_{c}end{pmatrix}}={egin{pmatrix}1&0&0&0\0&1&0&0\0&0&1&0\0&0&1&0end{pmatrix}}{egin{pmatrix}x\y\z\1end{pmatrix}}](https://img2018.cnblogs.com/blog/1470684/201810/1470684-20181019100558500-1266500328.png)
(这个乘法的计算结果是
=
。)
在进行乘法计算之后,通常齐次元素wc并不为1,所以为了映射回真实平面需要进行齐次除法,即每个元素都除以wc:
![{egin{pmatrix}x'\y'\z'end{pmatrix}}={egin{pmatrix}x_{c}/w_{c}\y_{c}/w_{c}\z_{c}/w_{c}end{pmatrix}}](https://img2018.cnblogs.com/blog/1470684/201810/1470684-20181019100600221-1105484329.png)
更加复杂的透视投影可以是与旋转、缩放、平移、切变等组合在一起对图像进行变换。