使用opencv基于realsense D435i展示基本的图像

此示例是 Intel RealSense 相机与 OpenCV 集成的 "Hello World" 基础代码。示例将打开一个 OpenCV 的 UI 窗口,并将彩色化的深度流渲染到窗口中。以下代码片段演示了如何从 rs2::frame 创建 cv::Mat:

cpp 复制代码
// License: Apache 2.0. See LICENSE file in root directory.
// Copyright(c) 2017 Intel Corporation. All Rights Reserved.

#include <librealsense2/rs.hpp> // Include RealSense Cross Platform API
#include <opencv2/opencv.hpp>   // Include OpenCV API

int main(int argc, char * argv[]) try
{
    // Declare depth colorizer for pretty visualization of depth data
    // 建着色过滤器(Colorizer Filter)
    // 该过滤器根据输入的深度帧生成彩色图像。
    // 与直接使用OpenCV的区别,RealSense Colorizer	硬件加速、低延迟	依赖RealSense SDK
    // cv::applyColorMap()	跨平台、可定制	需手动处理深度值归一化
    rs2::colorizer color_map;

    // Declare RealSense pipeline, encapsulating the actual device and sensors
    rs2::pipeline pipe;
    // Start streaming with default recommended configuration
    pipe.start();

    using namespace cv;
    const auto window_name = "Display Image"; 
    namedWindow(window_name, WINDOW_AUTOSIZE);

    while (waitKey(1) < 0 && getWindowProperty(window_name, WND_PROP_AUTOSIZE) >= 0)
    {
        rs2::frameset data = pipe.wait_for_frames(); // Wait for next set of frames from the camera
        rs2::frame depth = data.get_depth_frame().apply_filter(color_map);

        // Query frame size (width and height)
        // 返回图像的像素高度
        const int w = depth.as<rs2::video_frame>().get_width();
        const int h = depth.as<rs2::video_frame>().get_height();

        // Create OpenCV matrix of size (w,h) from the colorized depth data
        Mat image(Size(w, h), CV_8UC3, (void*)depth.get_data(), Mat::AUTO_STEP);

        // Update the window with new data
        imshow(window_name, image);
    }

    return EXIT_SUCCESS;
}
catch (const rs2::error & e)
{
    std::cerr << "RealSense error calling " << e.get_failed_function() << "(" << e.get_failed_args() << "):\n    " << e.what() << std::endl;
    return EXIT_FAILURE;
}
catch (const std::exception& e)
{
    std::cerr << e.what() << std::endl;
    return EXIT_FAILURE;
}

此外,上面手动转换image如果是彩色图像色值会有问题,可以直接使用realsense提供的转换接口:

cpp 复制代码
// License: Apache 2.0. See LICENSE file in root directory.
// Copyright(c) 2017 Intel Corporation. All Rights Reserved.

#include <librealsense2/rs.hpp> // Include RealSense Cross Platform API
#include <opencv2/opencv.hpp>   // Include OpenCV API
#include "../cv-helpers.hpp"
int main(int argc, char * argv[]) try
{
    // Declare depth colorizer for pretty visualization of depth data
    // 建着色过滤器(Colorizer Filter)
    // 该过滤器根据输入的深度帧生成彩色图像。
    // 与直接使用OpenCV的区别,RealSense Colorizer	硬件加速、低延迟	依赖RealSense SDK
    // cv::applyColorMap()	跨平台、可定制	需手动处理深度值归一化
    rs2::colorizer color_map;

    // Declare RealSense pipeline, encapsulating the actual device and sensors
    rs2::pipeline pipe;
    // Start streaming with default recommended configuration
    pipe.start();

    using namespace cv;
    const auto window_name = "Display Image"; 
    namedWindow(window_name, WINDOW_AUTOSIZE);

    while (waitKey(1) < 0 && getWindowProperty(window_name, WND_PROP_AUTOSIZE) >= 0)
    {
        rs2::frameset data = pipe.wait_for_frames(); // Wait for next set of frames from the camera
        rs2::frame depth = data.get_depth_frame().apply_filter(color_map);
        rs2::frame color = data.get_color_frame();
        // Query frame size (width and height)
        // 返回图像的像素高度
        const int w = depth.as<rs2::video_frame>().get_width();
        const int h = depth.as<rs2::video_frame>().get_height();
        const int cw = color.as<rs2::video_frame>().get_width();
        const int ch = color.as<rs2::video_frame>().get_height();

        // Create OpenCV matrix of size (w,h) from the colorized depth data
        //Mat image(Size(w, h), CV_8UC3, (void*)depth.get_data(), Mat::AUTO_STEP);
        Mat image = frame_to_mat(depth);
        // Update the window with new data
        imshow(window_name, image);
    }

    return EXIT_SUCCESS;
}
catch (const rs2::error & e)
{
    std::cerr << "RealSense error calling " << e.get_failed_function() << "(" << e.get_failed_args() << "):\n    " << e.what() << std::endl;
    return EXIT_FAILURE;
}
catch (const std::exception& e)
{
    std::cerr << e.what() << std::endl;
    return EXIT_FAILURE;
}
相关推荐
Dfreedom.7 小时前
图像滤波:非线性滤波与边缘保留技术
图像处理·人工智能·opencv·计算机视觉·非线性滤波·图像滤波
Dfreedom.7 小时前
开运算与闭运算:图像形态学中的“清道夫”与“修复匠”
图像处理·python·opencv·开运算·闭运算
格林威9 小时前
Baumer相机铆钉安装状态检测:判断铆接是否到位的 5 个核心算法,附 OpenCV+Halcon 的实战代码!
人工智能·opencv·算法·计算机视觉·视觉检测·工业相机·堡盟相机
李昊哲小课10 小时前
OpenCV Haar级联分类器人脸检测完整教程
人工智能·opencv·计算机视觉
格林威10 小时前
Baumer相机铸件气孔与缩松识别:提升铸造良品率的 6 个核心算法,附 OpenCV+Halcon 实战代码!
人工智能·opencv·算法·安全·计算机视觉·堡盟相机·baumer相机
光羽隹衡10 小时前
计算机视觉——Opencv(图像金字塔)
人工智能·opencv·计算机视觉
sali-tec11 小时前
C# 基于OpenCv的视觉工作流-章20-仿射变换
图像处理·人工智能·opencv·算法·计算机视觉
子夜江寒13 小时前
基于dlib与OpenCV的人脸检测与特征点标定技术实践
人工智能·opencv·计算机视觉
Pyeako13 小时前
opencv计算机视觉--光流估计&视频读取方法
python·opencv·计算机视觉·pycharm·角点检测·光流估计·视频读取方法
无垠的广袤1 天前
【VisionFive 2 Lite 单板计算机】边缘AI视觉应用部署:缺陷检测
linux·人工智能·python·opencv·开发板