问小白 wenxiaobai
资讯
历史
科技
环境与自然
成长
游戏
财经
文学与艺术
美食
健康
家居
文化
情感
汽车
三农
军事
旅行
运动
教育
生活
星座命理

机器视觉中的坐标系变换:旋转矩阵详解

创作时间:
作者:
@小白创作中心

机器视觉中的坐标系变换:旋转矩阵详解

引用
CSDN
1.
https://blog.csdn.net/weixin_73858308/article/details/144796042

在机器视觉领域,经常需要将物体的坐标从一个坐标系变换到另一个坐标系。旋转矩阵可以精确描述物体在三维空间中的旋转关系,常配合平移矢量一起完成刚体变换。

坐标系变换

当我们描述一个物体,从一个参考坐标系换算到另一个参考坐标系中时,我们需要对原坐标系进行仿射变换。旋转矩阵描述坐标系的旋转关系,平移矩阵描述两坐标系之间的位置关系。

公式:
$$
\mathbf{T} = \begin{bmatrix}
\mathbf{R} & \mathbf{t} \
\mathbf{0} & 1
\end{bmatrix}
$$
其中:

  • $\mathbf{p}$ 为物体的初始坐标,
  • $\mathbf{p'}$ 为变换后物体的坐标
  • $\mathbf{R}$ 为旋转矩阵
  • $\mathbf{t}$ 为平移矢量

平移矢量

在几何变换中,平移矢量是一种用于描述物体在空间中沿某一方向移动(平移)的方法。平移是一种刚体变换(即保持物体形状和大小不变的变换),但它不同于旋转矩阵,因为平移不涉及任何旋转或缩放操作。

一般来说,平移矢量是一个点的集合,直接相加即可。例如,将一个点 $\mathbf{p} = (x, y, z)$ 平移到新的位置 $\mathbf{p'}$,可以表示为:
$$
\mathbf{p'} = \mathbf{p} + \mathbf{t}
$$
其中 $\mathbf{t} = (t_x, t_y, t_z)$ 是平移矢量。

旋转矩阵

旋转矩阵用于描述物体在二维或三维空间中旋转变换,具有如下性质:

  • 旋转矩阵 $\mathbf{R}$ 为正交矩阵,即 $\mathbf{R}^T \mathbf{R} = \mathbf{I}$
  • 旋转矩阵行列式为 1,即 $\det(\mathbf{R}) = 1$
  • 旋转变换后,向量的长度和角度保持不变

二维旋转矩阵

假设给定一个二维向量 $\mathbf{v} = (x, y)$,让它绕原点逆时针旋转一个角度 $\theta$,旋转后的新向量记为 $\mathbf{v'}$,这个旋转的过程可以表示为:
$$
\mathbf{v'} = \mathbf{R} \mathbf{v}
$$
接下来,就是如何求解这个旋转矩阵 $\mathbf{R}$:

一:极坐标表示

一个二维向量 $\mathbf{v}$ 可以用极坐标表示为:
$$
\mathbf{v} = r (\cos \alpha, \sin \alpha)
$$
其中:

  • $r$ 为矢量长度,大小为 $\sqrt{x^2 + y^2}$
  • $\alpha$ 为矢量与 $x$ 轴的夹角

二:旋转后的极坐标表示

如果将矢量 $\mathbf{v}$ 绕原点逆时针旋转角度 $\theta$,旋转后的矢量 $\mathbf{v'}$ 表示为:
$$
\mathbf{v'} = r (\cos(\alpha + \theta), \sin(\alpha + \theta))
$$
利用和角公式展开:
$$
\mathbf{v'} = r (\cos \alpha \cos \theta - \sin \alpha \sin \theta, \sin \alpha \cos \theta + \cos \alpha \sin \theta)
$$
带入原式:
$$
\mathbf{v'} = (x \cos \theta - y \sin \theta, x \sin \theta + y \cos \theta)
$$
还原到直角坐标

三:矩阵形式表示

将上面两个方程用矩阵形式表示出来:
$$
\mathbf{v'} = \begin{bmatrix}
\cos \theta & -\sin \theta \
\sin \theta & \cos \theta
\end{bmatrix} \mathbf{v}
$$
因此二维旋转矩阵 $\mathbf{R}$ 为:
$$
\mathbf{R} = \begin{bmatrix}
\cos \theta & -\sin \theta \
\sin \theta & \cos \theta
\end{bmatrix}
$$

验证旋转矩阵的性质

一:正交性

是正交矩阵,$\mathbf{R}^T \mathbf{R} = \mathbf{I}$,保证了向量的长度和角度不发生变化。

证明:假设一个向量 $\mathbf{v}$ 旋转后变为 $\mathbf{v'}$,有:
$$
\mathbf{v'} = \mathbf{R} \mathbf{v}
$$
假设有一个向量 $\mathbf{v}$,则:
$$
\mathbf{v'}^T \mathbf{v'} = (\mathbf{R} \mathbf{v})^T (\mathbf{R} \mathbf{v}) = \mathbf{v}^T \mathbf{R}^T \mathbf{R} \mathbf{v} = \mathbf{v}^T \mathbf{v}
$$
上面1两条公式分别说明了旋转矩阵保存矢量长度和角度不变。

二:行列式

$\det(\mathbf{R}) = 1$,保证了矩阵变换不包括反射或其他非保向操作:如左手系坐标变为右手系坐标

三维旋转矩阵

目标:在三维空间中,找到一个旋转矩阵 $\mathbf{R}$,使得一个向量 $\mathbf{v}$ 绕三维空间中的某个轴旋转角度 $\theta$,得到新的向量 $\mathbf{v'}$。

绕x,y,z坐标轴旋转

考虑到一个向量 $\mathbf{v} = (x, y, z)$,绕 $x$ 轴旋转角度 $\theta$,几何上,绕 $x$ 轴的旋转只影响 $y$ 和 $z$ 轴的分量,而 $x$ 轴保存不变,参考二维旋转矩阵,有:

  • 原向量 $\mathbf{v}$ 和 $x$ 轴坐标 $(x, 0, 0)$
  • 旋转后坐标 $(x, y', z')$
  • $x$ 轴保持不变

将上述结果合并到三维旋转矩阵中:
$$
\mathbf{R}_x(\theta) = \begin{bmatrix}
1 & 0 & 0 \
0 & \cos \theta & -\sin \theta \
0 & \sin \theta & \cos \theta
\end{bmatrix}
$$

同理,我们可以得出绕 $y$ 轴旋转的旋转矩阵:
$$
\mathbf{R}_y(\theta) = \begin{bmatrix}
\cos \theta & 0 & \sin \theta \
0 & 1 & 0 \
-\sin \theta & 0 & \cos \theta
\end{bmatrix}
$$

绕 $z$ 轴旋转的旋转矩阵:
$$
\mathbf{R}_z(\theta) = \begin{bmatrix}
\cos \theta & -\sin \theta & 0 \
\sin \theta & \cos \theta & 0 \
0 & 0 & 1
\end{bmatrix}
$$

**需要注意的是:绕 $y$ 轴旋转时,由于定义的正方向与 $y$ 轴正方向不同,旋转矩阵略有不同。**在线性代数中可以用“逆序数”来描述

绕任意轴旋转

对于绕三维空间中任意轴旋转的情况,旋转矩阵可以使用罗德里格斯公式推导。假设旋转轴是一个单位向量 $\mathbf{u} = (u_x, u_y, u_z)$,绕该轴旋转了 $\theta$ 角度

公式表示

罗德里格斯公式描述任意轴的旋转矩阵:
$$
\mathbf{R}(\mathbf{u}, \theta) = \mathbf{I} + \sin \theta [\mathbf{u}]{\times} + (1 - \cos \theta) [\mathbf{u}]{\times}^2
$$
其中:

  • $\mathbf{I}$ 是单位矩阵
  • $[\mathbf{u}]_{\times}$ 是由旋转轴 $\mathbf{u}$ 构造的反对称矩阵
  • $[\mathbf{u}]{\times}^2$ 是矩阵 $[\mathbf{u}]{\times}$ 的平方

完整公式展开

将 $[\mathbf{u}]_{\times}$ 带入公式,得到旋转矩阵 $\mathbf{R}$:
$$
\mathbf{R}(\mathbf{u}, \theta) = \begin{bmatrix}
\cos \theta + u_x^2 (1 - \cos \theta) & u_x u_y (1 - \cos \theta) - u_z \sin \theta & u_x u_z (1 - \cos \theta) + u_y \sin \theta \
u_y u_x (1 - \cos \theta) + u_z \sin \theta & \cos \theta + u_y^2 (1 - \cos \theta) & u_y u_z (1 - \cos \theta) - u_x \sin \theta \
u_z u_x (1 - \cos \theta) - u_y \sin \theta & u_z u_y (1 - \cos \theta) + u_x \sin \theta & \cos \theta + u_z^2 (1 - \cos \theta)
\end{bmatrix}
$$

旋转矩阵代码(Python)

二维旋转矩阵

import numpy as np

def rotate_coordinates(x, y, theta):
    rotation_matrix = np.array([
        [np.cos(theta), -np.sin(theta)],
        [np.sin(theta),  np.cos(theta)]
    ])
    original_vector = np.array([x, y]) 
    rotated_vector = np.dot(rotation_matrix, original_vector)
    
    return rotated_vector[0], rotated_vector[1]  

三维绕x,y,z坐标轴旋转矩阵

import numpy as np

def rotation_matrix_x(theta):
    # 绕 X 轴旋转的旋转矩阵 R_x(theta)
    return np.array([
        [1, 0, 0],
        [0, np.cos(theta), -np.sin(theta)],
        [0, np.sin(theta),  np.cos(theta)]
    ])

def rotation_matrix_y(theta):
    # 绕 Y 轴旋转的旋转矩阵 R_y(theta)
    return np.array([
        [np.cos(theta), 0, np.sin(theta)],
        [0, 1, 0],
        [-np.sin(theta), 0, np.cos(theta)]
    ])

def rotation_matrix_z(theta):
    # 绕 Z 轴旋转的旋转矩阵 R_z(theta)
    return np.array([
        [np.cos(theta), -np.sin(theta), 0],
        [np.sin(theta),  np.cos(theta), 0],
        [0, 0, 1]
    ])

三维绕任意轴旋转

import numpy as np

def rotation_matrix_u(theta, u):
    # 任意轴 u 绕轴旋转的旋转矩阵 R(u, theta)
    u_x, u_y, u_z = u
    cos_theta = np.cos(theta)
    sin_theta = np.sin(theta)
    one_minus_cos = 1 - cos_theta
    R = np.array([
        [
            cos_theta + u_x**2 * one_minus_cos,
            u_x * u_y * one_minus_cos - u_z * sin_theta,
            u_x * u_z * one_minus_cos + u_y * sin_theta
        ],
        [
            u_y * u_x * one_minus_cos + u_z * sin_theta,
            cos_theta + u_y**2 * one_minus_cos,
            u_y * u_z * one_minus_cos - u_x * sin_theta
        ],
        [
            u_z * u_x * one_minus_cos - u_y * sin_theta,
            u_z * u_y * one_minus_cos + u_x * sin_theta,
            cos_theta + u_z**2 * one_minus_cos
        ]
    ])
    return R

本文介绍了机器视觉中坐标系变换的基础知识,重点讲解了旋转矩阵的原理和应用。从二维旋转矩阵到三维旋转矩阵,再到绕任意轴旋转的罗德里格斯公式,内容深入浅出,适合对机器视觉和线性代数感兴趣的读者。

© 2023 北京元石科技有限公司 ◎ 京公网安备 11010802042949号