首页
/ 10分钟上手Intel RealSense SDK:从零搭建Python深度视觉开发环境

10分钟上手Intel RealSense SDK:从零搭建Python深度视觉开发环境

2026-04-12 09:05:33作者:魏献源Searcher

Intel RealSense SDK(librealsense)是一款强大的开源工具包,为RealSense深度摄像头提供全面支持。本教程将帮助新手开发者快速配置Python开发环境,掌握深度图像采集、处理与应用开发的核心技能,让你轻松开启3D视觉应用之旅。

环境准备:搭建开发基础

开始前请确保系统已安装Python 3.6+、CMake 3.10+和Git工具。这些基础组件将帮助我们顺利完成后续的SDK编译与安装。

首先克隆项目代码库:

git clone https://gitcode.com/GitHub_Trending/li/librealsense

进入项目目录后,我们需要创建专门的构建文件夹以保持源代码目录整洁:

cd librealsense
mkdir build && cd build

编译配置:启用Python支持

配置CMake项目时,关键是启用Python绑定功能。通过以下命令生成Makefile:

cmake .. -DBUILD_PYTHON_BINDINGS=bool:true -DCMAKE_BUILD_TYPE=Release

这个配置会自动检测系统中的Python环境,并准备生成对应的pyrealsense2模块。如果需要指定特定Python版本,可以添加-DPYTHON_EXECUTABLE=/path/to/python参数。

编译安装:构建Python模块

执行编译命令,利用多线程加速构建过程:

make -j$(nproc)
sudo make install

编译完成后,pyrealsense2模块会被安装到系统Python路径中。对于需要自定义安装路径的用户,可以在cmake命令中添加-DCMAKE_INSTALL_PREFIX=/custom/path参数。

验证安装:测试RealSense Python API

创建一个简单的测试脚本test_realsense.py

import pyrealsense2 as rs

# 打印版本信息
print(f"pyrealsense2版本: {rs.__version__}")

# 列出已连接设备
ctx = rs.context()
devices = ctx.query_devices()
for dev in devices:
    print(f"发现设备: {dev.get_info(rs.camera_info.name)}")

运行脚本如果能正确显示版本号和设备信息,说明安装成功。

实战应用:深度图像采集与显示

下面我们创建一个完整的深度图像采集程序,展示如何使用Python API控制RealSense摄像头:

import pyrealsense2 as rs
import numpy as np
import cv2

# 初始化摄像头管道
pipeline = rs.pipeline()
config = rs.config()

# 配置流参数:640x480分辨率,30fps
config.enable_stream(rs.stream.depth, 640, 480, rs.format.z16, 30)
config.enable_stream(rs.stream.color, 640, 480, rs.format.bgr8, 30)

# 启动流传输
pipeline.start(config)

try:
    while True:
        # 等待获取帧数据
        frames = pipeline.wait_for_frames()
        depth_frame = frames.get_depth_frame()
        color_frame = frames.get_color_frame()
        
        if not depth_frame or not color_frame:
            continue
            
        # 转换为numpy数组
        depth_image = np.asanyarray(depth_frame.get_data())
        color_image = np.asanyarray(color_frame.get_data())
        
        # 应用颜色映射到深度图像
        depth_colormap = cv2.applyColorMap(cv2.convertScaleAbs(depth_image, alpha=0.03), cv2.COLORMAP_JET)
        
        # 显示图像
        images = np.hstack((color_image, depth_colormap))
        cv2.namedWindow('RealSense 深度与彩色图像', cv2.WINDOW_AUTOSIZE)
        cv2.imshow('RealSense 深度与彩色图像', images)
        
        # 按'q'键退出
        if cv2.waitKey(1) & 0xFF == ord('q'):
            break
            
finally:
    # 停止流传输
    pipeline.stop()
    cv2.destroyAllWindows()

这段代码将同时显示彩色图像和经过颜色映射的深度图像,帮助你直观理解深度数据的呈现方式。

数据记录与回放:保存深度数据

RealSense SDK提供了强大的录制和回放功能,让你可以离线处理深度数据。以下是一个简单的录制示例:

import pyrealsense2 as rs

# 创建录制器对象
pipeline = rs.pipeline()
config = rs.config()

# 启用流并指定录制文件
config.enable_stream(rs.stream.depth, 640, 480, rs.format.z16, 30)
config.enable_stream(rs.stream.color, 640, 480, rs.format.bgr8, 30)
config.enable_record_to_file("recording.bag")

# 开始录制
pipeline.start(config)

try:
    print("正在录制...按Ctrl+C停止")
    while True:
        # 只需等待帧即可自动录制
        pipeline.wait_for_frames()
        
except KeyboardInterrupt:
    pass
finally:
    pipeline.stop()
    print("录制已保存到recording.bag")

录制完成后,可以使用RealSense Viewer工具打开录制文件进行回放:

RealSense Viewer回放界面 RealSense Viewer工具提供直观的录制文件回放功能,支持深度和彩色数据流的同步播放

你也可以通过代码控制回放过程,实现高级数据处理和分析。

多摄像头配置:构建立体视觉系统

对于需要多视角深度感知的应用,RealSense SDK支持多摄像头同步采集。以下是多摄像头配置示例:

import pyrealsense2 as rs
import numpy as np

# 获取所有连接的设备
ctx = rs.context()
devices = ctx.query_devices()
pipelines = []

# 为每个设备创建管道
for i, dev in enumerate(devices):
    pipeline = rs.pipeline()
    config = rs.config()
    config.enable_device(dev.get_info(rs.camera_info.serial_number))
    config.enable_stream(rs.stream.depth, 640, 480, rs.format.z16, 30)
    pipeline.start(config)
    pipelines.append(pipeline)
    print(f"已启动设备 {i+1}: {dev.get_info(rs.camera_info.name)}")

try:
    while True:
        # 从所有设备获取帧
        frames_list = [pipeline.wait_for_frames() for pipeline in pipelines]
        depth_frames = [frames.get_depth_frame() for frames in frames_list]
        
        # 处理每个设备的深度数据
        for i, depth_frame in enumerate(depth_frames):
            if depth_frame:
                depth_data = np.asanyarray(depth_frame.get_data())
                print(f"设备 {i+1} 深度图像尺寸: {depth_data.shape}")
                
except KeyboardInterrupt:
    pass
finally:
    # 停止所有管道
    for pipeline in pipelines:
        pipeline.stop()

多摄像头系统在三维重建、立体视觉和机器人导航等领域有广泛应用。下图展示了一个多摄像头以太网同步采集的应用场景:

多摄像头以太网同步采集 通过以太网实现多台RealSense摄像头的同步数据采集,适用于大型空间的三维感知

尺寸测量应用:从深度数据到实际尺寸

利用RealSense的深度数据,我们可以实现物体尺寸的精确测量。以下是一个简单的距离测量示例:

import pyrealsense2 as rs
import numpy as np

pipeline = rs.pipeline()
config = rs.config()
config.enable_stream(rs.stream.depth, 640, 480, rs.format.z16, 30)
pipeline.start(config)

try:
    while True:
        frames = pipeline.wait_for_frames()
        depth_frame = frames.get_depth_frame()
        if not depth_frame:
            continue
            
        # 获取图像中心的距离
        width, height = depth_frame.get_width(), depth_frame.get_height()
        distance = depth_frame.get_distance(width//2, height//2)
        print(f"中心距离: {distance:.2f} 米")
        
except KeyboardInterrupt:
    pass
finally:
    pipeline.stop()

更复杂的应用可以实现三维物体的体积测量,如wrappers/python/examples/box_dimensioner_multicam/目录下的示例所示:

多摄像头箱体尺寸测量系统 基于多摄像头的箱体尺寸测量系统,可精确计算物体的长、宽、高参数

常见问题与解决方案

问题1:找不到pyrealsense2模块

解决方案:检查CMake配置时是否添加了-DBUILD_PYTHON_BINDINGS=bool:true参数,编译后确保pyrealsense2.sopyrealsense2.pyd文件被正确安装到Python的site-packages目录。

问题2:设备无法被识别

解决方案:运行sudo ./scripts/setup_udev_rules.sh安装设备规则,然后拔插摄像头重试。确保使用USB 3.0端口连接设备以获得最佳性能。

问题3:编译过程中缺少依赖

解决方案:运行sudo ./scripts/install_dependencies.sh安装所有必要的依赖库,包括libusb、OpenCV等。

探索更多示例代码

librealsense项目提供了丰富的Python示例,位于wrappers/python/examples/目录下,包括:

  • opencv_viewer_example.py - 使用OpenCV显示深度和彩色流
  • align_depth2color.py - 实现深度与彩色图像的精准对齐
  • export_ply_example.py - 将深度数据导出为PLY点云文件
  • pointcloud_example.py - 实时显示三维点云数据

这些示例覆盖了从基础操作到高级应用的各种场景,是学习RealSense开发的宝贵资源。

总结

通过本教程,你已经掌握了Intel RealSense SDK的Python开发环境配置方法,以及深度图像采集、处理和应用开发的基本技能。从单摄像头数据采集到多设备同步工作,从简单距离测量到复杂三维重建,RealSense SDK为你提供了构建各类深度视觉应用的强大工具。

现在,你可以开始探索更高级的功能,如点云处理、SLAM建图、物体识别等,将深度视觉技术应用到你的项目中。祝你的RealSense开发之旅顺利!

登录后查看全文
热门项目推荐
相关项目推荐