当前位置: 首页 > news >正文

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

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

// 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 APIint 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 sensorsrs2::pipeline pipe;// Start streaming with default recommended configurationpipe.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 camerars2::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 dataMat image(Size(w, h), CV_8UC3, (void*)depth.get_data(), Mat::AUTO_STEP);// Update the window with new dataimshow(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提供的转换接口:

// 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 sensorsrs2::pipeline pipe;// Start streaming with default recommended configurationpipe.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 camerars2::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 dataimshow(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;
}
http://www.dtcms.com/a/313775.html

相关文章:

  • 如何基于MQ实现分布式事务
  • 深入浅出 RabbitMQ:简单队列实战指南
  • 消防器材检测数据集介绍-9,600 张图片 智慧安防系统 建筑施工安全监管 AI 消防巡检机器人 自动审核系统 公共场所安全监测
  • 深入解析线程同步中WaitForSingleObject的超时问题
  • Flutter 事件总线 Event Bus
  • 【2025WACV-最佳论文】RayGauss:基于体积高斯的光线投射,用于逼真的小说视图合成
  • 【机器学习】(算法优化二)提升算法之:AdaBoost与随机梯度
  • Java 中 BigDecimal、Float、Double 的取整与保留小数处理方法详解
  • 从 0 到 1 开发图书管理系统:飞算 JavaAI 让技术落地更简单
  • 13.Home-面板组件封装
  • 如何设计和实施高效的向量化数据检索解决方案
  • 阿里云-通义灵码:解锁云原生智能开发新能力,让云开发更“灵”~
  • Clion STM32CubeMX LED闪灯
  • 为什么叫电磁兼容?
  • 【Java】一篇详解HashMap的扩容机制!!
  • SCI论文选词炼句(下)
  • vue3指定设置了dom元素的ref但是为null问题
  • Druid手写核心实现案例 实现一个简单Select 解析,包含Lexer、Parser、AstNode
  • 第三章 浏览器 【5. 事件】
  • Java项目:基于SSM框架实现的电子病历管理系统【ssm+B/S架构+源码+数据库+毕业论文+远程部署】
  • 前端开发(HTML,CSS,VUE,JS)从入门到精通!第五天(jQuery函数库)
  • 深入理解Java的SPI机制,使用auto-service库优化SPI
  • 打造个人数字图书馆:LeaNote+cpolar如何成为你的私有化知识中枢?
  • 【MySQL02】: MySQL类型
  • 深度学习TR3周:Pytorch复现Transformer
  • 软件测试自学之路
  • 架构师面试(三十九):微服务重构单体应用
  • 第三阶段—8天Python从入门到精通【itheima】-143节(pyspark实战——数据计算——flatmap方法)
  • RAG From Scratch 系列教程-4: Query_Construction
  • 向量空间模型