理解齐次坐标系下的变换矩阵
文章目录
- 理解齐次坐标系下的变换矩阵
-
- [1 引言](#1 引言)
- [2 齐次坐标系的简要介绍](#2 齐次坐标系的简要介绍)
-
- [2.1 齐次坐标系的定义](#2.1 齐次坐标系的定义)
- [2.2 为什么需要齐次坐标系?](#2.2 为什么需要齐次坐标系?)
- [2.3 齐次坐标系的特殊性质](#2.3 齐次坐标系的特殊性质)
-
- [2.3.1 点和向量的区分](#2.3.1 点和向量的区分)
- [2.3.2 投影变换](#2.3.2 投影变换)
- [3 齐次坐标系下的变换矩阵](#3 齐次坐标系下的变换矩阵)
- [4 齐次坐标系下的变换矩阵的实际应用示例](#4 齐次坐标系下的变换矩阵的实际应用示例)
- [5 总结](#5 总结)
- 参考文献
1 引言
在计算机图形学、计算机视觉和机器人学等领域,齐次坐标系是一个极其重要的数学工具。它不仅能够统一表示平移、旋转、缩放等变换,还能够处理投影变换,使得各种几何变换可以通过矩阵乘法优雅地表示和计算。本文将深入介绍齐次坐标系的概念以及在此基础上的变换矩阵。
2 齐次坐标系的简要介绍
关于齐次坐标系及其作用的介绍,可以参考我的这一篇文章:齐次坐标系统:什么是齐次坐标?为什么要引入齐次坐标?。
2.1 齐次坐标系的定义
齐次坐标系是将n维空间映射到n+1维空间的一种方法:
- 二维空间中的点 ( x , y ) (x, y) (x,y)在齐次坐标系中表示为 ( x , y , w ) (x, y, w) (x,y,w),其中 w ≠ 0 w \neq 0 w=0,对应的笛卡尔坐标为 ( x / w , y / w ) (x/w, y/w) (x/w,y/w)
- 三维空间中的点 ( x , y , z ) (x, y, z) (x,y,z)在齐次坐标系中表示为 ( x , y , z , w ) (x, y, z, w) (x,y,z,w),其中 w ≠ 0 w \neq 0 w=0,对应的笛卡尔坐标为 ( x / w , y / w , z / w ) (x/w, y/w, z/w) (x/w,y/w,z/w)
通常,我们令 w = 1 w=1 w=1,即二维点表示为 ( x , y , 1 ) (x, y, 1) (x,y,1),三维点表示为 ( x , y , z , 1 ) (x, y, z, 1) (x,y,z,1)。
2.2 为什么需要齐次坐标系?
在传统的笛卡尔坐标系中,我们用 ( x , y ) (x, y) (x,y)表示二维平面上的点,用 ( x , y , z ) (x, y, z) (x,y,z)表示三维空间中的点。然而,当我们需要表示平移变换时,会遇到一个问题:平移不能用线性变换(即矩阵乘法)表示。
例如,在二维空间中,点 ( x , y ) (x, y) (x,y)沿向量 ( t x , t y ) (t_x, t_y) (tx,ty)平移后的坐标为:
x ′ = x + t x y ′ = y + t y x' = x + t_x \\ y' = y + t_y x′=x+txy′=y+ty
这个变换包含加法操作,无法用矩阵乘法表示。为了解决这个问题,齐次坐标系应运而生。
2.3 齐次坐标系的特殊性质
2.3.1 点和向量的区分
在齐次坐标系中,点和向量可以明确区分:
- 点: ( x , y , z , 1 ) (x, y, z, 1) (x,y,z,1)
- 向量: ( x , y , z , 0 ) (x, y, z, 0) (x,y,z,0)
这种区分在几何计算中非常有用,因为点可以平移,而向量只能旋转和缩放。
2.3.2 投影变换
齐次坐标系最强大的特性之一是能够表示投影变换。通过适当设置 4 × 4 4 \times 4 4×4矩阵,我们可以实现透视投影、正交投影等。
例如,一个简单的透视投影矩阵可以表示为:
( 1 0 0 0 0 1 0 0 0 0 1 0 0 0 − 1 / d 0 ) \begin{pmatrix} 1 & 0 & 0 & 0 \\ 0 & 1 & 0 & 0 \\ 0 & 0 & 1 & 0 \\ 0 & 0 & -1/d & 0 \end{pmatrix} 10000100001−1/d0000
其中d是视点到投影平面的距离。
3 齐次坐标系下的变换矩阵
注:这些变换矩阵的使用方式(对应的矩阵乘法)如下:
以在二维空间中,逆时针旋转θ角度的旋转矩阵为例:
R ( θ ) = ( cos ( θ ) − sin ( θ ) 0 sin ( θ ) cos ( θ ) 0 0 0 1 ) R(\theta) = \begin{pmatrix} \cos(\theta) & -\sin(\theta) & 0 \\ \sin(\theta) & \cos(\theta) & 0 \\ 0 & 0 & 1 \end{pmatrix} R(θ)= cos(θ)sin(θ)0−sin(θ)cos(θ)0001
对于点 P = ( x , y , 1 ) T P = (x, y, 1)^T P=(x,y,1)T,旋转后的点 P ′ P' P′ 可以通过以下矩阵乘法 计算(注意此时的 P P P矩阵是列向量):
P ′ = R ( θ ) ⋅ P = ( cos ( θ ) − sin ( θ ) 0 sin ( θ ) cos ( θ ) 0 0 0 1 ) ( x y 1 ) = ( x cos ( θ ) − y sin ( θ ) x sin ( θ ) + y cos ( θ ) 1 ) P' = R(\theta) \cdot P = \begin{pmatrix} \cos(\theta) & -\sin(\theta) & 0 \\ \sin(\theta) & \cos(\theta) & 0 \\ 0 & 0 & 1 \end{pmatrix} \begin{pmatrix} x \\ y \\ 1 \end{pmatrix} = \begin{pmatrix} x\cos(\theta) - y\sin(\theta) \\ x\sin(\theta) + y\cos(\theta) \\ 1 \end{pmatrix} P′=R(θ)⋅P= cos(θ)sin(θ)0−sin(θ)cos(θ)0001 xy1 = xcos(θ)−ysin(θ)xsin(θ)+ycos(θ)1
也可以写成(注意此时的 P P P矩阵是列向量(x, y, 1)):
P ′ = P ⋅ R ( θ ) = ( x y 1 ) ( cos ( θ ) sin ( θ ) 0 − sin ( θ ) cos ( θ ) 0 0 0 1 ) = ( x cos ( θ ) − y sin ( θ ) x sin ( θ ) + y cos ( θ ) 1 ) P' = P \cdot R(\theta) = \begin{pmatrix} x & y & 1 \end{pmatrix} \begin{pmatrix} \cos(\theta) & \sin(\theta) & 0 \\ -\sin(\theta) & \cos(\theta) & 0 \\ 0 & 0 & 1 \end{pmatrix} = \begin{pmatrix} x\cos(\theta) - y\sin(\theta) & x\sin(\theta) + y\cos(\theta) & 1 \end{pmatrix} P′=P⋅R(θ)=(xy1) cos(θ)−sin(θ)0sin(θ)cos(θ)0001 =(xcos(θ)−ysin(θ)xsin(θ)+ycos(θ)1)
下文中的变换矩阵均以前一种 方法(即表示点的坐标的 P P P矩阵是行向量 ,与变换矩阵依次左乘 )为例。如果采用后一种 方法,即表示点的坐标的 P P P矩阵是列向量 ,那么变换矩阵应该转置,并且这个表示点的坐标的列向量 P P P矩阵与变换矩阵应依次右乘。
3.1 二维变换矩阵
在二维齐次坐标系下,变换矩阵是 3 × 3 3 \times 3 3×3的:
平移变换
( 1 0 t x 0 1 t y 0 0 1 ) \begin{pmatrix} 1 & 0 & t_x \\ 0 & 1 & t_y \\ 0 & 0 & 1 \end{pmatrix} 100010txty1
缩放变换
( s x 0 0 0 s y 0 0 0 1 ) \begin{pmatrix} s_x & 0 & 0 \\ 0 & s_y & 0 \\ 0 & 0 & 1 \end{pmatrix} sx000sy0001
旋转变换(逆时针旋转θ角度)
( cos ( θ ) − sin ( θ ) 0 sin ( θ ) cos ( θ ) 0 0 0 1 ) \begin{pmatrix} \cos(\theta) & -\sin(\theta) & 0 \\ \sin(\theta) & \cos(\theta) & 0 \\ 0 & 0 & 1 \end{pmatrix} cos(θ)sin(θ)0−sin(θ)cos(θ)0001
复合变换
多个变换可以通过矩阵乘法组合在一起。例如,先旋转后平移的变换矩阵为:
( 1 0 t x 0 1 t y 0 0 1 ) × ( cos ( θ ) − sin ( θ ) 0 sin ( θ ) cos ( θ ) 0 0 0 1 ) = ( cos ( θ ) − sin ( θ ) t x sin ( θ ) cos ( θ ) t y 0 0 1 ) \begin{pmatrix} 1 & 0 & t_x \\ 0 & 1 & t_y \\ 0 & 0 & 1 \end{pmatrix} \times \begin{pmatrix} \cos(\theta) & -\sin(\theta) & 0 \\ \sin(\theta) & \cos(\theta) & 0 \\ 0 & 0 & 1 \end{pmatrix} = \begin{pmatrix} \cos(\theta) & -\sin(\theta) & t_x \\ \sin(\theta) & \cos(\theta) & t_y \\ 0 & 0 & 1 \end{pmatrix} 100010txty1 × cos(θ)sin(θ)0−sin(θ)cos(θ)0001 = cos(θ)sin(θ)0−sin(θ)cos(θ)0txty1
3.2 三维变换矩阵
在三维齐次坐标系下,变换矩阵是 4 × 4 4 \times 4 4×4的:
平移变换
( 1 0 0 t x 0 1 0 t y 0 0 1 t z 0 0 0 1 ) \begin{pmatrix} 1 & 0 & 0 & t_x \\ 0 & 1 & 0 & t_y \\ 0 & 0 & 1 & t_z \\ 0 & 0 & 0 & 1 \end{pmatrix} 100001000010txtytz1
缩放变换
( s x 0 0 0 0 s y 0 0 0 0 s z 0 0 0 0 1 ) \begin{pmatrix} s_x & 0 & 0 & 0 \\ 0 & s_y & 0 & 0 \\ 0 & 0 & s_z & 0 \\ 0 & 0 & 0 & 1 \end{pmatrix} sx0000sy0000sz00001
绕X轴旋转
( 1 0 0 0 0 cos ( θ ) − sin ( θ ) 0 0 sin ( θ ) cos ( θ ) 0 0 0 0 1 ) \begin{pmatrix} 1 & 0 & 0 & 0 \\ 0 & \cos(\theta) & -\sin(\theta) & 0 \\ 0 & \sin(\theta) & \cos(\theta) & 0 \\ 0 & 0 & 0 & 1 \end{pmatrix} 10000cos(θ)sin(θ)00−sin(θ)cos(θ)00001
绕Y轴旋转
( cos ( θ ) 0 sin ( θ ) 0 0 1 0 0 − sin ( θ ) 0 cos ( θ ) 0 0 0 0 1 ) \begin{pmatrix} \cos(\theta) & 0 & \sin(\theta) & 0 \\ 0 & 1 & 0 & 0 \\ -\sin(\theta) & 0 & \cos(\theta) & 0 \\ 0 & 0 & 0 & 1 \end{pmatrix} cos(θ)0−sin(θ)00100sin(θ)0cos(θ)00001
绕Z轴旋转
( cos ( θ ) − sin ( θ ) 0 0 sin ( θ ) cos ( θ ) 0 0 0 0 1 0 0 0 0 1 ) \begin{pmatrix} \cos(\theta) & -\sin(\theta) & 0 & 0 \\ \sin(\theta) & \cos(\theta) & 0 & 0 \\ 0 & 0 & 1 & 0 \\ 0 & 0 & 0 & 1 \end{pmatrix} cos(θ)sin(θ)00−sin(θ)cos(θ)0000100001
4 齐次坐标系下的变换矩阵的实际应用示例
下面是一个使用Python和NumPy实现齐次坐标变换的简单示例:
python
import numpy as np
import matplotlib.pyplot as plt
# 定义一个2D点
point = np.array([1, 2, 1]) # 齐次坐标 (x, y, 1)
# 定义变换矩阵
# 1. 平移变换 (x+2, y+3)
translation = np.array([
[1, 0, 2],
[0, 1, 3],
[0, 0, 1]
])
# 2. 旋转变换 (45度)
theta = np.radians(45)
rotation = np.array([
[np.cos(theta), -np.sin(theta), 0],
[np.sin(theta), np.cos(theta), 0],
[0, 0, 1]
])
# 3. 缩放变换 (x*2, y*2)
scaling = np.array([
[2, 0, 0],
[0, 2, 0],
[0, 0, 1]
])
# 应用变换
translated_point = translation.dot(point)
rotated_point = rotation.dot(point)
scaled_point = scaling.dot(point)
# 组合变换 (先旋转,再平移)
combined = translation.dot(rotation)
combined_point = combined.dot(point)
# 打印结果
print(f"原始点: ({point[0]}, {point[1]})")
print(f"平移后: ({translated_point[0]}, {translated_point[1]})")
print(f"旋转后: ({rotated_point[0]:.2f}, {rotated_point[1]:.2f})")
print(f"缩放后: ({scaled_point[0]}, {scaled_point[1]})")
print(f"组合变换后: ({combined_point[0]:.2f}, {combined_point[1]:.2f})")
5 总结
齐次坐标系是计算机图形学中的基础工具,它通过增加一个维度,使得各种变换(包括平移、旋转、缩放和投影)都可以用矩阵乘法统一表示。这种表示方法不仅数学上优雅,而且在计算上高效,特别是在需要连续应用多种变换的场景中。
理解齐次坐标系及其变换矩阵,对于从事计算机图形学、计算机视觉、机器人学等领域的开发和研究工作至关重要。通过本文的介绍,希望读者能够掌握这一强大工具的基本原理和应用方法。
参考文献
- Foley, J. D., Van Dam, A., Feiner, S. K., & Hughes, J. F. (1995). Computer Graphics: Principles and Practice. Addison-Wesley.
- Shirley, P., & Marschner, S. (2009). Fundamentals of Computer Graphics. A K Peters/CRC Press.
- Craig, J. J. (2004). Introduction to Robotics: Mechanics and Control. Pearson Education.
如有问题或建议,欢迎在评论区留言交流!