坐标系的变换矩阵推导
1.平移变换
假设存在点(x,y,z),将x移动a,y移动b,z移动c,到新的点
(
x
′
,
y
′
,
z
′
)
(x^{'},y^{'},z^{'})
(x′,y′,z′),则:
x
′
=
x
+
a
x^{'}=x+a
x′=x+a
y
′
=
y
+
b
y^{'}=y+b
y′=y+b
z
′
=
y
+
c
z^{'}=y+c
z′=y+c
写成矩阵形式:
[
x
′
y
′
z
′
1
]
=
[
1
0
0
a
0
1
0
b
0
0
1
c
0
0
0
1
]
[
x
y
z
1
]
\left[\begin{matrix}x^{'}\\y^{'}\\z^{'}\\1 \end{matrix}\right]= \left[\begin{matrix}1&0&0&a\\0&1&0&b\\0&0&1&c\\0&0&0&1 \end{matrix}\right] \left[\begin{matrix}x\\y\\z\\1 \end{matrix}\right]
⎣⎢⎢⎡x′y′z′1⎦⎥⎥⎤=⎣⎢⎢⎡100001000010abc1⎦⎥⎥⎤⎣⎢⎢⎡xyz1⎦⎥⎥⎤
中间4x4的矩阵叫变换矩阵。可见,如果要平移坐标,要将坐标维度增加1,变成齐次坐标(齐次坐标(homogeneous coordinates)就是将一个原本是n维的向量用一个n+1维向量来表示,常用于投影几何)。
在计算机图形学中,为了实现平移、旋转、缩放等图像操作,需要用到齐次坐标。
例1:世界坐标系wor相对相机坐标系cam的x、y、z分别平移了10,20,30,求次变换齐次矩阵。
[ x ′ y ′ z ′ 1 ] = [ 1 0 0 10 0 1 0 0 0 0 1 0 0 0 0 1 ] × [ 1 0 0 0 0 1 0 20 0 0 1 0 0 0 0 1 ] × [ 1 0 0 0 0 1 0 0 0 0 1 30 0 0 0 1 ] × [ x y z 1 ] = [ 1 0 0 10 0 1 0 20 0 0 1 30 0 0 0 1 ] × [ x y z 1 ] \left[\begin{matrix}x^{'}\\y^{'}\\z^{'}\\1 \end{matrix}\right]=\left[\begin{matrix}1&0&0&10\\0&1&0&0\\0&0&1&0\\0&0&0&1 \end{matrix}\right] \times \left[\begin{matrix}1&0&0&0\\0&1&0&20\\0&0&1&0\\0&0&0&1 \end{matrix}\right]\times \left[\begin{matrix}1&0&0&0\\0&1&0&0\\0&0&1&30\\0&0&0&1 \end{matrix}\right]\times \left[\begin{matrix}x\\y\\z\\1 \end{matrix}\right]= \left[\begin{matrix}1&0&0&10\\0&1&0&20\\0&0&1&30\\0&0&0&1 \end{matrix}\right]\times \left[\begin{matrix}x\\y\\z\\1 \end{matrix}\right] ⎣⎢⎢⎡x′y′z′1⎦⎥⎥⎤=⎣⎢⎢⎡10000100001010001⎦⎥⎥⎤×⎣⎢⎢⎡10000100001002001⎦⎥⎥⎤×⎣⎢⎢⎡10000100001000301⎦⎥⎥⎤×⎣⎢⎢⎡xyz1⎦⎥⎥⎤=⎣⎢⎢⎡1000010000101020301⎦⎥⎥⎤×⎣⎢⎢⎡xyz1⎦⎥⎥⎤
三个分量矩阵位置可以交换,因为是独立变量,互不影响。
所以,平移齐次矩阵为:
[
1
0
0
10
0
1
0
20
0
0
1
30
0
0
0
1
]
\left[\begin{matrix}1&0&0&10\\0&1&0&20\\0&0&1&30\\0&0&0&1 \end{matrix}\right]
⎣⎢⎢⎡1000010000101020301⎦⎥⎥⎤
旋转变换有两种,一种是向量在当前坐标系内的旋转,一种是坐标系的旋转。
2. 坐标系旋转变换:由固定坐标系旋转到另一个坐标系。
旋转变换有两种,一种是向量在当前坐标系内的旋转,一种是坐标系的旋转。这里推导坐标系旋转矩阵。
(1) 绕X轴旋转(逆时针) α \alpha α:
方程为:
{
x
′
=
x
,
y
′
=
y
c
o
s
α
+
z
s
i
n
α
,
z
′
=
−
y
s
i
n
α
+
z
c
o
s
α
\begin{cases} x'=x,\\ y'=ycos{\alpha}+zsin{\alpha},\\ z'=-ysin{\alpha}+zcos{\alpha} \end{cases}
⎩⎪⎨⎪⎧x′=x,y′=ycosα+zsinα,z′=−ysinα+zcosα
写成矩阵形式:
[
x
′
y
′
z
′
1
]
=
[
1
0
0
0
0
c
o
s
α
s
i
n
α
0
0
−
s
i
n
α
c
o
s
α
0
0
0
0
1
]
×
[
x
y
z
1
]
\left[\begin{matrix}x^{'}\\y^{'}\\z^{'}\\1 \end{matrix}\right]= \left[\begin{matrix}1&0&0&0\\0&cos{\alpha}&sin{\alpha}&0\\0&-sin{\alpha}&cos{\alpha}&0\\0&0&0&1 \end{matrix}\right] \times \left[\begin{matrix}x\\y\\z\\1 \end{matrix}\right]
⎣⎢⎢⎡x′y′z′1⎦⎥⎥⎤=⎣⎢⎢⎡10000cosα−sinα00sinαcosα00001⎦⎥⎥⎤×⎣⎢⎢⎡xyz1⎦⎥⎥⎤
(2) 绕Y轴旋转(逆时针) β \beta β:
方程为:
{
x
′
′
=
x
′
c
o
s
β
+
z
′
s
i
n
β
,
y
′
′
=
y
′
,
z
′
′
=
−
x
′
s
i
n
β
+
z
′
c
o
s
β
\begin{cases} x''=x'cos{\beta}+z'sin{\beta},\\ y''=y',\\ z''=-x'sin{\beta}+z'cos{\beta} \end{cases}
⎩⎪⎨⎪⎧x′′=x′cosβ+z′sinβ,y′′=y′,z′′=−x′sinβ+z′cosβ
写成矩阵形式:
[
x
′
′
y
′
′
z
′
′
1
]
=
[
c
o
s
β
0
s
i
n
β
0
0
1
0
0
−
s
i
n
α
0
c
o
s
β
0
0
0
0
1
]
×
[
x
′
y
′
z
′
1
]
\left[\begin{matrix}x^{''}\\y^{''}\\z^{''}\\1 \end{matrix}\right]= \left[\begin{matrix}cos{\beta}&0&sin{\beta}&0\\0&1&0&0\\-sin{\alpha}&0&cos{\beta}&0\\0&0&0&1 \end{matrix}\right] \times \left[\begin{matrix}x'\\y'\\z'\\1 \end{matrix}\right]
⎣⎢⎢⎡x′′y′′z′′1⎦⎥⎥⎤=⎣⎢⎢⎡cosβ0−sinα00100sinβ0cosβ00001⎦⎥⎥⎤×⎣⎢⎢⎡x′y′z′1⎦⎥⎥⎤
(3)绕Z轴旋转(逆时针) γ \gamma γ:
方程为:
{
x
′
′
′
=
x
′
′
c
o
s
γ
+
y
′
′
s
i
n
γ
,
y
′
′
′
=
−
x
′
′
s
i
n
γ
+
y
′
′
c
o
s
β
,
z
′
′
′
=
z
′
′
\begin{cases} x'''=x''cos{\gamma}+y''sin{\gamma},\\ y'''=-x''sin{\gamma}+y''cos{\beta},\\ z'''=z'' \end{cases}
⎩⎪⎨⎪⎧x′′′=x′′cosγ+y′′sinγ,y′′′=−x′′sinγ+y′′cosβ,z′′′=z′′
写成矩阵形式:
[
x
′
′
′
y
′
′
′
z
′
′
′
1
]
=
[
c
o
s
γ
s
i
n
γ
0
0
−
s
i
n
γ
c
o
s
γ
0
0
0
0
1
0
0
0
0
1
]
×
[
x
′
′
y
′
′
z
′
′
1
]
\left[\begin{matrix}x^{'''}\\y^{'''}\\z^{'''}\\1 \end{matrix}\right]= \left[\begin{matrix}cos{\gamma}&sin{\gamma}&0&0\\-sin{\gamma}&cos{\gamma}&0&0\\0&0&1&0\\0&0&0&1 \end{matrix}\right] \times \left[\begin{matrix}x''\\y''\\z''\\1 \end{matrix}\right]
⎣⎢⎢⎡x′′′y′′′z′′′1⎦⎥⎥⎤=⎣⎢⎢⎡cosγ−sinγ00sinγcosγ0000100001⎦⎥⎥⎤×⎣⎢⎢⎡x′′y′′z′′1⎦⎥⎥⎤
所以,坐标轴分别依次绕x,y,z轴旋转 α \alpha α, β \beta β, γ \gamma γ的变换矩阵(前后用左乘来连接):
R = R z ( γ ) R y ( β ) R x ( α ) = [ c o s γ s i n γ 0 0 − s i n γ c o s γ 0 0 0 0 1 0 0 0 0 1 ] × [ c o s β 0 s i n β 0 0 1 0 0 − s i n α 0 c o s β 0 0 0 0 1 ] × [ 1 0 0 0 0 c o s α s i n α 0 0 − s i n α c o s α 0 0 0 0 1 ] × [ x y z 1 ] R=R_z(\gamma)R_y(\beta)R_x(\alpha)=\left[\begin{matrix}cos{\gamma}&sin{\gamma}&0&0\\-sin{\gamma}&cos{\gamma}&0&0\\0&0&1&0\\0&0&0&1 \end{matrix}\right]\times \left[\begin{matrix}cos{\beta}&0&sin{\beta}&0\\0&1&0&0\\-sin{\alpha}&0&cos{\beta}&0\\0&0&0&1 \end{matrix}\right] \times \left[\begin{matrix}1&0&0&0\\0&cos{\alpha}&sin{\alpha}&0\\0&-sin{\alpha}&cos{\alpha}&0\\0&0&0&1 \end{matrix}\right] \times \left[\begin{matrix}x\\y\\z\\1 \end{matrix}\right] R=Rz(γ)Ry(β)Rx(α)=⎣⎢⎢⎡cosγ−sinγ00sinγcosγ0000100001⎦⎥⎥⎤×⎣⎢⎢⎡cosβ0−sinα00100sinβ0cosβ00001⎦⎥⎥⎤×⎣⎢⎢⎡10000cosα−sinα00sinαcosα00001⎦⎥⎥⎤×⎣⎢⎢⎡xyz1⎦⎥⎥⎤