相机标定步骤
时间: 2025-06-30 14:39:38 浏览: 12
### 相机标定的详细步骤及实现方法
相机标定是确定相机内参和外参的过程,通常包括单目相机标定和双目相机标定。以下是相机标定的具体步骤及其实现方法:
#### 1. 标定板的选择与准备
在相机标定过程中,需要使用标定板作为参考物。常用的标定板有棋盘格、圆点阵列等。棋盘格标定板因其易于检测和高精度而被广泛使用[^1]。
#### 2. 图像采集
将标定板放置在相机视野的不同位置和角度下,拍摄多张包含标定板的图像。确保标定板覆盖相机视野的各个区域,以提高标定结果的准确性[^1]。
#### 3. 特征点检测
通过图像处理技术提取标定板上的特征点坐标。对于棋盘格标定板,可以使用 OpenCV 的 `findChessboardCorners` 函数来检测角点。代码示例如下:
```python
import cv2
import numpy as np
# 棋盘格尺寸
chessboard_size = (9, 6)
# 存储世界坐标系中的三维点和图像坐标系中的二维点
obj_points = [] # 3D 点
img_points = [] # 2D 点
# 创建世界坐标系中的三维点
objp = np.zeros((chessboard_size[0] * chessboard_size[1], 3), np.float32)
objp[:, :2] = np.mgrid[0:chessboard_size[0], 0:chessboard_size[1]].T.reshape(-1, 2)
# 加载图像并检测角点
for fname in image_files:
img = cv2.imread(fname)
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
ret, corners = cv2.findChessboardCorners(gray, chessboard_size, None)
if ret:
obj_points.append(objp)
img_points.append(corners)
```
#### 4. 内参标定
利用检测到的特征点,通过 OpenCV 的 `calibrateCamera` 函数计算相机的内参矩阵和畸变系数。代码示例如下:
```python
ret, camera_matrix, dist_coeffs, rvecs, tvecs = cv2.calibrateCamera(
obj_points, img_points, gray.shape[::-1], None, None
)
```
其中,`camera_matrix` 是相机的内参矩阵,`dist_coeffs` 是畸变系数。
#### 5. 外参标定
对于每一张标定图像,`rvecs` 和 `tvecs` 分别表示相机相对于标定板的姿态(旋转和平移)。这些参数可以通过 `cv2.calibrateCamera` 函数直接获得。
#### 6. 双目相机标定
对于双目视觉系统,除了标定每个相机的内参和外参外,还需要标定两相机之间的相对位置关系,即旋转矩阵和平移向量。可以使用 OpenCV 的 `stereoCalibrate` 函数完成这一任务。代码示例如下:
```python
ret, camera_matrix1, dist_coeffs1, camera_matrix2, dist_coeffs2, R, T, E, F = cv2.stereoCalibrate(
obj_points, img_points1, img_points2, camera_matrix1, dist_coeffs1, camera_matrix2, dist_coeffs2, gray.shape[::-1]
)
```
其中,`R` 和 `T` 分别表示两个相机之间的旋转矩阵和平移向量[^1]。
#### 7. 结果评估
为了验证标定结果的准确性,可以使用重投影误差进行评估。重投影误差越小,标定结果越准确。代码示例如下:
```python
mean_error = 0
for i in range(len(obj_points)):
img_points_reprojected, _ = cv2.projectPoints(obj_points[i], rvecs[i], tvecs[i], camera_matrix, dist_coeffs)
error = cv2.norm(img_points[i], img_points_reprojected, cv2.NORM_L2) / len(img_points_reprojected)
mean_error += error
mean_error /= len(obj_points)
print("Mean reprojection error:", mean_error)
```
---
阅读全文
相关推荐

















