坐标系变换矩阵推导

坐标系的变换矩阵推导

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] xyz1=100001000010abc1xyz1

  中间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] xyz1=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] xyz1=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=xcosβ+zsinβ,y=y,z=xsinβ+zcosβ

写成矩阵形式:
[ 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] xyz1=cosβ0sinα00100sinβ0cosβ00001×xyz1

(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=xcosγ+ysinγ,y=xsinγ+ycosβ,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] xyz1=cosγsinγ00sinγcosγ0000100001×xyz1

所以,坐标轴分别依次绕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β0sinα00100sinβ0cosβ00001×10000cosαsinα00sinαcosα00001×xyz1

  • 19
    点赞
  • 64
    收藏
    觉得还不错? 一键收藏
  • 5
    评论
透视投影变换矩阵推导过程如下: 假设有一个三维点 $(X,Y,Z)$,它在相机坐标系中的坐标为 $(X_c,Y_c,Z_c)$。相机坐标系的原点为相机位置,$Z_c$ 轴指向相机朝向的反方向,$X_c$ 和 $Y_c$ 轴分别与相机的右方向和下方向对齐。 为了把相机坐标系中的点映射到图像平面上,我们需要进行透视投影变换。首先,我们将相机坐标系中的点转换为齐次坐标 $(X_c,Y_c,Z_c,1)$。然后,我们将它乘以一个投影矩阵 $P$,得到一个新的齐次坐标 $(u,v,w,1)$: $$ \begin{bmatrix} u \\ v \\ w \\ 1 \\ \end{bmatrix} = P \cdot \begin{bmatrix} X_c \\ Y_c \\ Z_c \\ 1 \\ \end{bmatrix} $$ 其中,$u$ 和 $v$ 分别表示图像平面上的坐标,$w$ 用来进行透视除法,保证 $u$ 和 $v$ 的值在图像平面上。 投影矩阵 $P$ 可以分解为相机内参矩阵 $K$ 和相机外参矩阵 $[R|t]$ 的乘积: $$ P = K [R|t] $$ 其中,$K$ 是一个 $3 \times 3$ 的矩阵,包含了相机的内部参数,如焦距、主点等。$[R|t]$ 是一个 $3 \times 4$ 的矩阵,包含了相机的外部参数,如相机的旋转和平移。 为了推导 $P$ 的具体形式,我们可以先考虑一个简单的情况:相机坐标系的原点与图像平面重合,且相机的朝向与图像平面平行。这种情况下,投影矩阵可以表示为: $$ P = \begin{bmatrix} f & 0 & 0 & 0 \\ 0 & f & 0 & 0 \\ 0 & 0 & 1 & 0 \\ 0 & 0 & 0 & 1 \\ \end{bmatrix} $$ 其中,$f$ 是焦距,表示相机到图像平面的距离。 当相机坐标系的原点和图像平面不重合时,我们可以使用相机外参矩阵 $[R|t]$ 来把相机坐标系的原点变换到图像平面上。具体来说,我们可以将相机坐标系的原点变换为 $(X_c',Y_c',Z_c')$,其中 $(X_c',Y_c',0)$ 是图像平面上的点。这个变换可以表示为: $$ \begin{bmatrix} X_c' \\ Y_c' \\ Z_c' \\ 1 \\ \end{bmatrix} = [R|t] \cdot \begin{bmatrix} 0 \\ 0 \\ 0 \\ 1 \\ \end{bmatrix} $$ 然后,我们可以把 $(X,Y,Z)$ 变换为 $(X',Y',Z')$,其中 $(X',Y')$ 是图像平面上的坐标。这个变换可以表示为: $$ \begin{bmatrix} X' \\ Y' \\ Z' \\ 1 \\ \end{bmatrix} = [R|t] \cdot \begin{bmatrix} X \\ Y \\ Z \\ 1 \\ \end{bmatrix} $$ 最后,我们可以将 $(X',Y',Z')$ 投影到图像平面上,得到一个新的齐次坐标 $(u,v,w,1)$。这个投影可以表示为: $$ \begin{bmatrix} u \\ v \\ w \\ 1 \\ \end{bmatrix} = K \cdot \begin{bmatrix} X'/Z' \\ Y'/Z' \\ 1 \\ \end{bmatrix} $$ 将以上三个变换组合起来,我们可以得到透视投影变换矩阵的形式: $$ P = K [R|t] = \begin{bmatrix} f_x & 0 & c_x & 0 \\ 0 & f_y & c_y & 0 \\ 0 & 0 & 1 & 0 \\ \end{bmatrix} \begin{bmatrix} r_{11} & r_{12} & r_{13} & t_1 \\ r_{21} & r_{22} & r_{23} & t_2 \\ r_{31} & r_{32} & r_{33} & t_3 \\ \end{bmatrix} $$ 其中,$f_x$ 和 $f_y$ 是 $K$ 矩阵的对角线元素,分别表示 $x$ 和 $y$ 方向上的焦距;$c_x$ 和 $c_y$ 是 $K$ 矩阵的中心点,表示图像平面上的主点;$r_{ij}$ 和 $t_i$ 是 $[R|t]$ 矩阵的元素,表示相机的旋转和平移。
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值