立体视觉 世界坐标系、相机坐标系
时间: 2025-03-27 09:14:32 浏览: 23
### 立体视觉中世界坐标系到相机坐标系的转换
在立体视觉应用里,为了精确描述场景内各点相对于摄像机的位置,需建立从世界坐标系至相机坐标系的映射关系。通常情况下,会选取其中一个摄像头作为参考标准来定义整个系统的空间定位框架。
#### 定义两个主要坐标体系
- **世界坐标系 (World Coordinate System)**: 此坐标系用于全局范围内表示物体位置,其原点可以根据具体应用场景灵活设定;对于双目视觉而言,往往设于左侧或右侧摄像头上,或是两台设备沿X轴方向连线中心处[^1]。
- **相机坐标系 (Camera Coordinate System)**: 这是一个局部参照系,以选定的具体摄像装置光心为起点构建而成,Z轴正向指向景物前方,XY平面平行于传感器表面并垂直于镜头主光线传播路径。
#### 转换矩阵及其构成要素
要完成由世界坐标系到相机坐标系的变化操作,则依赖于旋转和平移两种变换组合而成的整体刚体运动模型:
\[ \begin{bmatrix} X_c \\ Y_c \\ Z_c \\ 1 \end{bmatrix}
= [\mathbf{R}| \mathbf{T}]
\cdot
\begin{bmatrix} X_w \\ Y_w \\ Z_w \\ 1 \end{bmatrix},
\]
其中 \( [\mathbf{R}|\mathbf{T}] \) 表示齐次变换矩阵,包含了3×3大小的姿态角旋转变换子阵\( \mathbf{R}\),以及长度为3的一维平移矢量\( \mathbf{T}\)[^2]。
该公式说明了如何通过给定的世界坐标计算对应的相机坐标下的三维点位信息。实际编程实践中可通过如下Python代码片段展示这一过程:
```python
import numpy as np
def world_to_camera(world_point, R, T):
"""
Convert a point from the world coordinate system to camera coordinates.
Parameters:
world_point : array_like
A single or multiple points represented in homogeneous form [Xw;Yw; Zw; 1].
R : ndarray of shape (3, 3)
Rotation matrix that transforms vectors from the world frame into the camera frame.
T : ndarray of shape (3,)
Translation vector between origins of two frames.
Returns:
cam_points : ndarray with same number of rows as input but four columns representing transformed points.
"""
# Ensure inputs are arrays and reshape them appropriately
wp = np.atleast_2d(np.array(world_point))
npts = wp.shape[0]
if wp.shape[-1]==3:
ones_col=np.ones((npts,1))
wp=np.hstack([wp,ones_col])
elif wp.shape[-1]!=4:
raise ValueError('Input must be either Nx3 Cartesian coords or Nx4 Homogeneous.')
RT_matrix = np.column_stack((np.row_stack((R,T)), [[0],[0],[0],[1]]))
cam_homog = RT_matrix @ wp.T
return cam_homog[:3].T / cam_homog[3].reshape(-1,1)
# Example usage demonstrating conversion using random rotation & translation matrices
if __name__ == "__main__":
rng = np.random.default_rng()
example_world_pts = rng.uniform(size=(5, 3)) * 100
rot_mat_example = rng.standard_normal((3, 3))
trans_vec_example = rng.uniform(low=-50., high=50., size=(3,))
converted_cam_coords = world_to_camera(example_world_pts, rot_mat_example, trans_vec_example)
print(converted_cam_coords)
```
阅读全文
相关推荐

















