realsense d435i标定python教程
时间: 2025-06-03 15:25:29 浏览: 14
### Realsense D435i 相机标定使用 Python 教程
#### 准备工作
为了对标定 Intel RealSense D435i 相机,需先确保已正确安装 `pyrealsense2` 库以及 OpenCV。对于解决可能遇到的依赖项问题,如 Git 克隆过程中出现问题,建议按照官方指南来配置开发环境[^1]。
#### 获取必要的库和支持文件
如果在 Anaconda 环境下操作,则可考虑将特定版本的 `.pyd` 文件放置于指定位置以兼容当前使用的解释器版本,这有助于避免一些常见的导入错误[^2]。
#### 开始标定过程
OpenCV 提供了一套完整的工具集用于相机内参和外参的计算。下面是一个简单的例子展示如何利用棋盘格图案来进行相机内部参数估计:
```python
import numpy as np
import cv2
import pyrealsense2 as rs
# 设置管道并启动流
pipeline = rs.pipeline()
config = rs.config()
# 启动设备上的彩色传感器
config.enable_stream(rs.stream.color, 640, 480, rs.format.bgr8, 30)
pipeline.start(config)
criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 30, 0.001)
objp = np.zeros((9*7, 3), np.float32)
objp[:, :2] = np.mgrid[0:9, 0:7].T.reshape(-1, 2)
# 存储对象点和图像点的位置数组
objpoints = [] # 世界坐标系中的三维点
imgpoints = [] # 图像平面内的二维点
frames_count = 0
while frames_count < 20:
frames = pipeline.wait_for_frames()
color_frame = frames.get_color_frame()
if not color_frame:
continue
img = np.asanyarray(color_frame.get_data())
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
ret, corners = cv2.findChessboardCorners(gray, (9, 7), None)
if ret == True:
objpoints.append(objp)
refined_corners = cv2.cornerSubPix(
gray, corners, (11, 11), (-1, -1), criteria)
imgpoints.append(refined_corners)
cv2.drawChessboardCorners(img, (9, 7), refined_corners, ret)
cv2.imshow('Calibration', img)
frames_count += 1
key = cv2.waitKey(1) & 0xFF
if key == ord('q'):
break
ret, mtx, dist, rvecs, tvecs = cv2.calibrateCamera(
objpoints, imgpoints, gray.shape[::-1], None, None)
print("Camera matrix:")
print(mtx)
print("\nDistortion coefficients:")
print(dist)
pipeline.stop()
cv2.destroyAllWindows()
```
此脚本通过捕获一系列包含校准板(通常是黑白相间的方格阵列)的画面,并从中提取角点信息完成标定流程。最终输出的是摄像机矩阵 (`mtx`) 和畸变系数 (`dist`),它们可用于后续处理中纠正镜头失真等问题[^3]。
阅读全文
相关推荐

















