机器视觉---Intel RealSense SDK 2.0 开发流程

ntel RealSense SDK 2.0的核心开发流程与其他深度相机类似围绕设备管理、数据流处理和高级功能集成三大模块展开。

tips:基于SDK的开发,目前可以高效利用AI搭建框架,然后针对具体的SDK去跳转查阅,将代码修改到正确,利用SDK自带的显示软件,查看不同初始化 滤波 等设置的效果,可以进行高效开发。

一、初始化与设备管理

1. 上下文创建

rs2::context是SDK的基础,负责管理所有设备资源和底层通信:

cpp 复制代码
rs2::context ctx;  // 创建上下文,自动检测系统中的RealSense设备

2. 设备枚举与筛选

通过上下文获取设备列表并进行筛选:

cpp 复制代码
rs2::device_list devices = ctx.query_devices();  // 获取所有连接的设备
if (devices.empty()) {
    throw std::runtime_error("未检测到RealSense设备");
}

// 遍历设备并筛选目标设备
rs2::device target_device;
for (auto&& dev : devices) {
    // 可根据设备名称、序列号等筛选
    if (std::string(dev.get_info(RS2_CAMERA_INFO_NAME)) == "Intel RealSense D435") {
        target_device = dev;
        break;
    }
}

3. 设备信息查询

获取设备基本参数和能力:

cpp 复制代码
// 设备基本信息
std::string name = target_device.get_info(RS2_CAMERA_INFO_NAME);
std::string serial = target_device.get_info(RS2_CAMERA_INFO_SERIAL_NUMBER);
std::string firmware = target_device.get_info(RS2_CAMERA_INFO_FIRMWARE_VERSION);

// 传感器列表
std::vector<rs2::sensor> sensors = target_device.query_sensors();
for (auto&& sensor : sensors) {
    // 传感器类型判断(深度、彩色、IMU等)
    if (sensor.is<rs2::depth_sensor>()) {
        // 处理深度传感器
    }
}

二、数据流配置与启动

1. 管道创建

rs2::pipeline是处理数据流的核心接口,简化了设备控制流程:

cpp 复制代码
rs2::pipeline pipe(ctx);  // 绑定上下文创建管道

2. 流配置

使用rs2::config配置需要启用的数据流参数:

cpp 复制代码
rs2::config cfg;

// 配置深度流:分辨率640x480,格式Z16,帧率30fps
cfg.enable_stream(RS2_STREAM_DEPTH, 640, 480, RS2_FORMAT_Z16, 30);
// 配置彩色流:分辨率1280x720,格式RGB8,帧率30fps
cfg.enable_stream(RS2_STREAM_COLOR, 1280, 720, RS2_FORMAT_RGB8, 30);

// 可选:从录制文件读取(替代实时设备)
// cfg.enable_device_from_file("recording.bag");

// 可选:录制到文件
// cfg.enable_record_to_file("output.bag");

3. 启动管道

应用配置并启动数据流:

cpp 复制代码
// 启动管道并获取配置文件
rs2::pipeline_profile profile = pipe.start(cfg);

// 从配置文件中获取实际启用的流信息
auto depth_stream = profile.get_stream(RS2_STREAM_DEPTH).as<rs2::video_stream_profile>();
int depth_width = depth_stream.width();
int depth_height = depth_stream.height();

三、帧捕获与处理

1. 帧获取

通过管道获取同步的帧集合:

cpp 复制代码
// 方法1:阻塞等待新帧(超时时间5000ms)
rs2::frameset frames = pipe.wait_for_frames(5000);

// 方法2:非阻塞获取(适合实时性要求高的场景)
if (rs2::frameset fs; pipe.poll_for_frames(&fs)) {
    // 处理帧数据
}

2. 帧类型提取

从帧集合中提取特定类型的帧:

cpp 复制代码
// 获取深度帧
rs2::depth_frame depth_frame = frames.get_depth_frame();
// 获取彩色帧
rs2::video_frame color_frame = frames.get_color_frame();

// 检查帧有效性
if (!depth_frame || !color_frame) {
    continue;  // 无效帧则跳过
}

3. 帧数据访问

获取帧的基本信息和原始数据:

cpp 复制代码
// 深度帧数据访问
uint16_t* depth_data = (uint16_t*)depth_frame.get_data();
int depth_stride = depth_frame.get_stride_in_bytes() / sizeof(uint16_t);
float center_distance = depth_frame.get_distance(width/2, height/2);  // 获取中心点距离

// 彩色帧数据访问
uint8_t* color_data = (uint8_t*)color_frame.get_data();
int color_stride = color_frame.get_stride_in_bytes();

四、高级功能集成

配置详细的操作参考SDK和开发工具 Depth Quality Tool for RealSense Cameras 、RealSense Viewer的实际效果进行修改

1. 帧对齐

将深度帧与彩色帧对齐(基于相机内参):

cpp 复制代码
// 创建对齐器(对齐到彩色流)
rs2::align align(RS2_STREAM_COLOR);

// 处理帧集合,获取对齐后的帧
rs2::frameset aligned_frames = align.process(frames);
rs2::depth_frame aligned_depth = aligned_frames.get_depth_frame();  // 已对齐的深度帧

2. 深度数据滤波

应用滤波算法优化深度数据:

cpp 复制代码
// 创建滤波器链
rs2::decimation_filter dec_filter;  // 降采样,减少数据量
rs2::spatial_filter spat_filter;    // 空间滤波,平滑深度数据
rs2::temporal_filter temp_filter;   // 时间滤波,利用帧间相关性

// 配置滤波器参数
spat_filter.set_option(RS2_OPTION_HOLES_FILL, 2);  // 填充空洞

// 应用滤波
rs2::frame filtered = dec_filter.process(aligned_depth);
filtered = spat_filter.process(filtered);
filtered = temp_filter.process(filtered);

3. 点云生成

将深度数据转换为3D点云:

cpp 复制代码
rs2::pointcloud pc;
rs2::points points;

// 将点云与彩色帧关联(获取颜色信息)
pc.map_to(color_frame);
// 从深度帧计算点云
points = pc.calculate(aligned_depth);

// 访问点云数据
auto vertices = points.get_vertices();  // 点坐标数组
auto tex_coords = points.get_texture_coordinates();  // 纹理坐标

五、资源释放与错误处理

1. 停止

cpp 复制代码
pipe.stop();  // 停止管道,释放设备资源

2. 错误处理机制

cpp 复制代码
try {
    // SDK操作代码
}
catch (const rs2::error& e) {
    std::cerr << "SDK错误: " << e.what() << " (" << e.get_failed_function() << ")" << std::endl;
}
catch (const std::exception& e) {
    std::cerr << "系统错误: " << e.what() << std::endl;
}

核心流程示例代码

cpp 复制代码
#include <librealsense2/rs.hpp>
#include <iostream>
#include <vector>

int main() {
    try {
        // 1. 初始化上下文
        rs2::context ctx;
        auto devices = ctx.query_devices();
        if (devices.empty()) {
            throw std::runtime_error("未找到RealSense设备");
        }
        std::cout << "找到设备: " << devices[0].get_info(RS2_CAMERA_INFO_NAME) << std::endl;

        // 2. 创建并配置管道
        rs2::pipeline pipe(ctx);
        rs2::config cfg;
        
        // 配置流参数
        cfg.enable_stream(RS2_STREAM_DEPTH, 640, 480, RS2_FORMAT_Z16, 30);
        cfg.enable_stream(RS2_STREAM_COLOR, 640, 480, RS2_FORMAT_RGB8, 30);

        // 3. 启动管道
        rs2::pipeline_profile profile = pipe.start(cfg);
        
        // 获取流配置信息
        auto depth_profile = profile.get_stream(RS2_STREAM_DEPTH).as<rs2::video_stream_profile>();
        auto color_profile = profile.get_stream(RS2_STREAM_COLOR).as<rs2::video_stream_profile>();
        std::cout << "深度流: " << depth_profile.width() << "x" << depth_profile.height() << std::endl;
        std::cout << "彩色流: " << color_profile.width() << "x" << color_profile.height() << std::endl;

        // 4. 初始化辅助工具
        rs2::align align(RS2_STREAM_COLOR);  // 帧对齐器
        rs2::spatial_filter spat_filter;     // 空间滤波器

        // 5. 主循环:捕获并处理帧
        const int MAX_FRAMES = 100;
        for (int i = 0; i < MAX_FRAMES; ++i) {
            // 获取帧集合
            rs2::frameset frames = pipe.wait_for_frames();
            
            // 帧对齐
            auto aligned_frames = align.process(frames);
            rs2::depth_frame depth = aligned_frames.get_depth_frame();
            rs2::video_frame color = aligned_frames.get_color_frame();

            // 深度滤波
            rs2::frame filtered_depth = spat_filter.process(depth);

            // 计算中心点距离
            float distance = depth.get_distance(depth_profile.width()/2, depth_profile.height()/2);
            std::cout << "\r第" << i+1 << "帧 - 中心距离: " << distance << "米" << std::flush;
        }

        // 6. 停止管道
        pipe.stop();
        std::cout << "\n程序正常结束" << std::endl;

    } catch (const rs2::error& e) {
        std::cerr << "\nSDK错误: " << e.what() << std::endl;
        return EXIT_FAILURE;
    } catch (const std::exception& e) {
        std::cerr << "\n错误: " << e.what() << std::endl;
        return EXIT_FAILURE;
    }

    return EXIT_SUCCESS;
}

核心流程关键点

  1. 上下文与管道rs2::context是设备管理基础,rs2::pipeline是数据流处理的核心接口
  2. 流配置原则:需根据设备能力设置合理的分辨率、格式和帧率组合
  3. 帧同步机制rs2::frameset保证多流数据的时间同步
  4. 数据处理流程:原始帧 → 对齐 → 滤波 → 应用(点云/测量等)
  5. 资源管理:确保管道正确停止以释放设备资源
相关推荐
脏脏a3 小时前
类与对象(上):面向过程到面向对象的跨越,类的定义、封装与 this 指针等核心概念深度剖析
开发语言·c++
数智大号3 小时前
超云发布R2425存储服务器:以全栈自研引领国产存储新方向
运维·服务器
亚林瓜子3 小时前
在amazon linux 2023上面通过Fedora 36软件仓库源安装tesseract5
linux·运维·服务器·ocr·tesseract·amazon·fedor
豆沙粽子好吃嘛!3 小时前
windows环境下g++无输出的解决方案
windows
是专家不是砖家3 小时前
linux USB摄像头不停掉线问题
linux·运维·服务器
yuanManGan3 小时前
走进Linux的世界:初识进程(Task)
linux·运维·服务器
AI柠檬3 小时前
C语言基于MPI并行计算矩阵的乘法
c语言·c++·算法
小无名呀3 小时前
socket_udp
linux·网络·c++·网络协议·计算机网络·udp
小马哥编程3 小时前
【软考架构】案例分析-瘦客户端C/S架构
运维·服务器·架构