相机控制

相机控制 API 参考文档

模块:camera_api — Intel RealSense D400 系列相机控制

1. 辅助函数

函数 list_realsense_devices()
list_realsense_devices() → list[dict]

枚举当前所有已连接的 RealSense 设备。

list[dict] — 设备信息列表
[ { "index": 0, "name": "Intel RealSense D435I", "serial": "123456789", "product_line": "D400", "usb_type": "3.2" } ]
函数 print_realsense_devices()
print_realsense_devices() → None

打印当前所有已连接相机信息。

Python
from camera_api.D435_rgb_depth import list_realsense_devices, print_realsense_devices

# 列出所有相机
devices = list_realsense_devices()
for dev in devices:
    print(f"序列号: {dev['serial']}, 名称: {dev['name']}")

# 打印设备信息
print_realsense_devices()

2. D435Camera 方法详解

构造 D435Camera()
D435Camera(serial, width, height, fps, enable_color, enable_depth, align_depth_to_color)
参数 类型 必需 默认值 说明
serial str 否 None 相机序列号
width int 否 640 图像宽度(像素)
height int 否 480 图像高度(像素)
fps int 否 30 帧率
enable_color bool 否 True 启用彩色图
enable_depth bool 否 True 启用深度图
align_depth_to_color bool 否 True 深度对齐到彩色图
D435Camera — 相机实例
Python
from camera_api.D435_rgb_depth import D435Camera

camera = D435Camera(
    serial="148522073368",  # 指定序列号
    width=640,
    height=480,
    fps=30,
    enable_color=True,
    enable_depth=True,
    align_depth_to_color=True
)
方法 camera.start()
camera.start() → None

启动相机。

Python
camera.start()
print(f"相机已启动,序列号: {camera.get_active_device_serial()}")
方法 camera.stop()
camera.stop() → None

停止相机。

Python
camera.stop()

3. 获取图像帧 — get_frames

方法 camera.get_frames()
camera.get_frames(timeout_ms) → dict
参数 类型 必需 默认值 说明
timeout_ms int 否 3000 等待超时时间(毫秒)
dict — 图像数据
{ "color": np.ndarray, # 彩色图(BGR格式) "depth": np.ndarray, # 深度图(uint16) "timestamp_ms": float, # 时间戳(毫秒) "depth_scale": float # 深度尺度 }
Python
frames = camera.get_frames(timeout_ms=3000)
color_image = frames["color"]     # 彩色图 (480, 640, 3)
depth_image = frames["depth"]     # 深度图 (480, 640)
timestamp = frames["timestamp_ms"]
depth_scale = frames["depth_scale"]

4. 获取相机内参 — get_intrinsics

方法 camera.get_intrinsics()
camera.get_intrinsics(stream_type) → dict
参数 类型 必需 默认值 说明
stream_type str 否 "color" 流类型:"color" 或 "depth"
dict — 内参数据
{ "width": int, # 图像宽度 "height": int, # 图像高度 "fx": float, # 焦距X "fy": float, # 焦距Y "ppx": float, # 主点X "ppy": float, # 主点Y "model": str, # 畸变模型 "coeffs": list # 畸变系数 }
Python
intrinsics = camera.get_intrinsics("color")
print(f"焦距: fx={intrinsics['fx']:.2f}, fy={intrinsics['fy']:.2f}")

5. 获取深度距离 — get_distance

方法 camera.get_distance()
camera.get_distance(depth_image, u, v) → float
参数 类型 必需 说明
depth_image np.ndarray 是 深度图
u int 是 像素X坐标
v int 是 像素Y坐标
float — 距离,单位:米
Python
frames = camera.get_frames()
depth_image = frames["depth"]

# 获取中心像素的深度
distance = camera.get_distance(depth_image, 320, 240)
print(f"中心像素深度: {distance:.3f}米")

6. 像素坐标转3D点 — pixel_to_point

方法 camera.pixel_to_point()
camera.pixel_to_point(u, v, depth_m) → list[float]
参数 类型 必需 说明
u int 是 像素X坐标
v int 是 像素Y坐标
depth_m float 是 深度值(米)
list[float] — [x, y, z],单位:米
Python
# 将中心像素转换为3D点
point = camera.pixel_to_point(320, 240, 1.0)
print(f"3D点: X={point[0]:.3f}, Y={point[1]:.3f}, Z={point[2]:.3f}米")

7. 完整使用示例

示例 完整相机控制流程
Python
from camera_api.D435_rgb_depth import D435Camera, print_realsense_devices
import cv2

# 1. 列出所有相机
print_realsense_devices()

# 2. 创建并启动相机
camera = D435Camera(
    serial="148522073368",
    width=640,
    height=480,
    fps=30
)
camera.start()

try:
    # 3. 获取图像
    frames = camera.get_frames()
    color_image = frames["color"]
    depth_image = frames["depth"]

    # 4. 获取深度距离
    distance = camera.get_distance(depth_image, 320, 240)
    print(f"中心点距离: {distance:.3f}米")

    # 5. 像素转3D点
    point_3d = camera.pixel_to_point(320, 240, distance)
    print(f"3D坐标: {point_3d}")

    # 6. 显示图像
    cv2.imshow("Color", color_image)
    cv2.waitKey(0)

finally:
    # 7. 停止相机
    camera.stop()

8. 依赖安装

Shell
pip install pyrealsense2 opencv-python numpy
W1 | 相机控制 API 文档 Ver1.0.0