当前位置: 首页 > news >正文

齐次坐标系下的变换矩阵

理解齐次坐标系下的变换矩阵

文章目录

  • 理解齐次坐标系下的变换矩阵
    • 1 引言
    • 2 齐次坐标系的简要介绍
      • 2.1 齐次坐标系的定义
      • 2.2 为什么需要齐次坐标系?
      • 2.3 齐次坐标系的特殊性质
        • 2.3.1 点和向量的区分
        • 2.3.2 投影变换
    • 3 齐次坐标系下的变换矩阵
      • 3.1 二维变换矩阵
        • 平移变换
        • 缩放变换
        • 旋转变换(逆时针旋转θ角度)
        • 复合变换
      • 3.2 三维变换矩阵
        • 平移变换
        • 缩放变换
        • 绕X轴旋转
        • 绕Y轴旋转
        • 绕Z轴旋转
    • 4 齐次坐标系下的变换矩阵的实际应用示例
    • 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} 100001000011/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(θ)0sin(θ)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(θ)0sin(θ)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=PR(θ)=(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(θ)0sin(θ)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(θ)0sin(θ)cos(θ)0001 = cos(θ)sin(θ)0sin(θ)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(θ)00sin(θ)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(θ)0sin(θ)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(θ)00sin(θ)cos(θ)0000100001

4 齐次坐标系下的变换矩阵的实际应用示例

下面是一个使用Python和NumPy实现齐次坐标变换的简单示例:

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 总结

齐次坐标系是计算机图形学中的基础工具,它通过增加一个维度,使得各种变换(包括平移、旋转、缩放和投影)都可以用矩阵乘法统一表示。这种表示方法不仅数学上优雅,而且在计算上高效,特别是在需要连续应用多种变换的场景中。

理解齐次坐标系及其变换矩阵,对于从事计算机图形学、计算机视觉、机器人学等领域的开发和研究工作至关重要。通过本文的介绍,希望读者能够掌握这一强大工具的基本原理和应用方法。

参考文献

  1. Foley, J. D., Van Dam, A., Feiner, S. K., & Hughes, J. F. (1995). Computer Graphics: Principles and Practice. Addison-Wesley.
  2. Shirley, P., & Marschner, S. (2009). Fundamentals of Computer Graphics. A K Peters/CRC Press.
  3. Craig, J. J. (2004). Introduction to Robotics: Mechanics and Control. Pearson Education.

如有问题或建议,欢迎在评论区留言交流!

http://www.xdnf.cn/news/66745.html

相关文章:

  • 【图论 DFS BFS】P10725 [GESP202406 八级] 最远点对|普及+
  • LangChain实现PDF中图表文本多模态数据向量化及RAG应用实战指南
  • LeetCode算法题(Go语言实现)_54
  • ubuntu--汉字、中文输入
  • iso文件在麒麟V10系统上安装达梦数据库
  • 基础服务系列-Jupyter Notebook 支持JavaScript
  • 【技术派后端篇】基于 Redis 实现网站 PV/UV 数据统计
  • 前端笔记-Vue3(上)
  • Spark-SQL 四(实验)
  • 显卡及相关大模型部署需求概述
  • 靠华为脱胎换骨,但赛力斯仍需要Plan B
  • 【Linux网络编程十】网络原理之IP协议【网络层】
  • 悬空引用和之道、之禅-《分析模式》漫谈57
  • SystemWeaver详解:从入门到精通的深度实战指南
  • css3新特性第五章(web字体)
  • 极狐GitLab Git LFS 速率限制如何设置?
  • mysql的binlog,redolog,undolog的区别
  • 安卓垂直进度条
  • 学习深度学习是否要先学习机器学习?工程师的路径选择策略
  • 部署Kimi-VL-A3B-Instruct视频推理
  • AgentGPT开源程序可以在浏览器中组装、配置和部署自主人工智能代理
  • FramePack:让视频生成更高效、更实用
  • 从0到1学习X-File-Storage:一站式文件存储解决方案
  • spark基础介绍
  • C++中函数的实现写在头文件内
  • Linux系统的介绍及操作系统的基本概念
  • 赛灵思Xilinx FPGa XCKU15P‑2FFVA1156I AMD Kintex UltraScale+
  • Qt6文档阅读笔记-RESTful API Server解析
  • 从C语言变量看内存
  • BR_调制特性(RF/TRM/CA/BV-07-C [Modulation Characteristics])