Realsense D435i +Opencv 获取彩色、深度、IMU数据并对齐

  • 前言
  • 源码
  • CMakeLists.txt
  • 效果
  • 参考

前言

参考realsense官方文档和各位大佬的博客,在Ubuntu18.04 系统下,得到了Realsense D435i的所有数据(如果我没猜错的话),包括它的RGB图、左右红外摄像图、深度图、IMU数据,并且将深度图数据和RGB图进行对齐。

其中IMU数据获取得有点痛苦,最后还是google了才知道怎么去提取。

没什么好说的了…… 直接上代码吧,下面都有注释。

代码的功能就是循环显示所有获得的图像和IMU数据。

源码

#include <librealsense2/rs.hpp>// include OpenCV header file
#include <opencv2/opencv.hpp>using namespace std;
using namespace cv;#define width 640
#define height 480
#define fps 30int main(int argc, char** argv) try
{// judge whether devices is exist or not rs2::context ctx;auto list = ctx.query_devices(); // Get a snapshot of currently connected devicesif (list.size() == 0) throw std::runtime_error("No device detected. Is it plugged in?");rs2::device dev = list.front();//rs2::frameset frames;//Contruct a pipeline which abstracts the devicers2::pipeline pipe;//创建一个通信管道//https://baike.so.com/doc/1559953-1649001.html pipeline的解释//Create a configuration for configuring the pipeline with a non default profilers2::config cfg;//创建一个以非默认配置的配置用来配置管道//Add desired streams to configurationcfg.enable_stream(RS2_STREAM_COLOR, width, height, RS2_FORMAT_BGR8, fps);//向配置添加所需的流cfg.enable_stream(RS2_STREAM_DEPTH, width, height, RS2_FORMAT_Z16,fps);cfg.enable_stream(RS2_STREAM_INFRARED, 1, width, height, RS2_FORMAT_Y8, fps);cfg.enable_stream(RS2_STREAM_INFRARED, 2, width, height, RS2_FORMAT_Y8, fps);cfg.enable_stream(RS2_STREAM_ACCEL, RS2_FORMAT_MOTION_XYZ32F);cfg.enable_stream(RS2_STREAM_GYRO, RS2_FORMAT_MOTION_XYZ32F);// get depth scale // float depth_scale = get_depth_scale(profile.get_device());// start stream pipe.start(cfg);//指示管道使用所请求的配置启动流while(1){frames = pipe.wait_for_frames();//等待所有配置的流生成框架// Align to depth rs2::align align_to_depth(RS2_STREAM_DEPTH);frames = align_to_depth.process(frames);// Get imu dataif (rs2::motion_frame accel_frame = frames.first_or_default(RS2_STREAM_ACCEL)){rs2_vector accel_sample = accel_frame.get_motion_data();std::cout << "Accel:" << accel_sample.x << ", " << accel_sample.y << ", " << accel_sample.z << std::endl;}if (rs2::motion_frame gyro_frame = frames.first_or_default(RS2_STREAM_GYRO)){rs2_vector gyro_sample = gyro_frame.get_motion_data();std::cout << "Gyro:" << gyro_sample.x << ", " << gyro_sample.y << ", " << gyro_sample.z << std::endl;}//Get each framers2::frame color_frame = frames.get_color_frame();rs2::frame depth_frame = frames.get_depth_frame();rs2::video_frame ir_frame_left = frames.get_infrared_frame(1);rs2::video_frame ir_frame_right = frames.get_infrared_frame(2);// Creating OpenCV Matrix from a color imageMat color(Size(width, height), CV_8UC3, (void*)color_frame.get_data(), Mat::AUTO_STEP);Mat pic_right(Size(width,height), CV_8UC1, (void*)ir_frame_right.get_data());Mat pic_left(Size(width,height), CV_8UC1, (void*)ir_frame_left.get_data());Mat pic_depth(Size(width,height), CV_16U, (void*)depth_frame.get_data(), Mat::AUTO_STEP);// Display in a GUInamedWindow("Display Image", WINDOW_AUTOSIZE );imshow("Display Image", color);waitKey(1);imshow("Display depth", pic_depth*15);waitKey(1);imshow("Display pic_left", pic_left);waitKey(1);imshow("Display pic_right",pic_right);waitKey(1);}return 0;
}// error
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;
}

CMakeLists.txt

cmake_minimum_required(VERSION 3.1.0)project(alldata)set( CMAKE_CXX_COMPILER "g++" )
set( CMAKE_BUILD_TYPE "Release" )
set( CMAKE_CXX_FLAGS "-std=c++11 -O3" )
# Enable C++11
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED TRUE)FIND_PACKAGE(OpenCV REQUIRED)
FIND_PACKAGE(realsense2 REQUIRED)include_directories( ${OpenCV_INCLUDE_DIRS} )
include_directories( ${realsense2_INCLUDE_DIRS})
add_executable(alldata GetAlldata.cpp)target_link_libraries( alldata ${OpenCV_LIBS} ${realsense2_LIBRARY})

效果

1、从左到右从上到下分别是红外左、红外右、对齐后的图像显示、深度灰度图,是视频流,如果只想获取一帧图像的话可以把代码的while去掉。

2、IMU数据是一直打印的,这里就不放出来了。


如果你觉得文章对你有帮助,欢迎关注、点赞、转发。

参考

https://www.google.com/amp/s/www.intelrealsense.com/how-to-getting-imu-data-from-d435i-and-t265/amp/
https://dev.intelrealsense.com/docs/api-how-to
https://blog.csdn.net/LongXiaoYue0/article/details/81059635

Realsense D435i +Opencv 获取彩色、深度、IMU数据并对齐相关推荐

  1. 乐视体感astra pro深度摄像头在ros系统获取 深度图像 彩色图像 无色彩点云数据 彩色点云数据

    1.astra pro深度摄像头介绍 2.astra pro驱动安装 3.astra pro获取深度图像   无色彩pointCloud2 4.astra pro获取彩色图像  带彩色的pointCl ...

  2. Linex Ubuntu环境下 Intel Realsense D435I 驱动+ROS驱动安装配置

    任务背景 在ROS环境中使用d435i,订阅图像和imu数据 任务概述 实现在ros中使用d435i主要有两步骤:1.安装d435i sdk,即librealsense: 2.安装realsense- ...

  3. Kinect DK相机与Realsense D435i相机记录

    Kinect DK相机与Realsense D435i相机记录 文章目录 Kinect DK相机与Realsense D435i相机记录 前言 一.Kinect DK相机 1.imu 2.RGB摄像头 ...

  4. robot_localization多传感器定位-IMU数据的校准与融合

    robot_localization多传感器融合中的IMU数据获取分四个部分来说明,分别是数据类型.IMU校准及融合理论.rikirobot源码解析以及优化 第一部分:数据类型 robot_local ...

  5. realsense D435 D435i D415深度相机在ros下获得RGB图、左右红外摄像图、深度图、IMU数据

    首先你要你确保你的相机驱动已经安装好,环境配置可以看我的另一篇文章:https://blog.csdn.net/weixin_46195203/article/details/119205851 ** ...

  6. win10 打开 Intel Realsense R200获取深度和彩色视频流 ,并测试python opencv 获取

    一:win10打开 Intel Realsense  R200 1.python 需3.6或以上,我的是3.7 2.去官网下载https://github.com/IntelRealSense/lib ...

  7. (已修正精度 1mm左右)Realsense d435i深度相机+Aruco+棋盘格+OpenCV手眼标定全过程记录

    文章目录 2023.5更新 ------------------下面为原文--------------------- 一.前期准备 1.1 手眼标定原理 1.2 Aruco返回位姿的原理 1.3 生成 ...

  8. Intel RealSense D435i 深度相机介绍

    参考: https://www.sohu.com/a/340984033_715754 https://www.chiphell.com/thread-1945054-1-1.html https:/ ...

  9. 基于深度摄像头的yolov4算法应用 (Realsense D435i)python

    硬件使用的是教授提供的RealSense D435i摄像头 深度值用来当作测距传感器 识别工作在RBG图像上做文章 融合了一些大佬们的code,ps:深度摄像头有专门的库(pyrealsense2), ...

最新文章

  1. 《Java编程思想》学习笔记4——集合容器
  2. 关于统计学,几个简单易懂的小故事
  3. Python正则表达式模式备忘表
  4. 解决Windows上编译PHP7.1拓展的错误
  5. 防抖 节流_坚持造轮子第二天 防抖与节流
  6. iOS 中文转拼音 多音字处理
  7. 【摩尔吧】Altium 17 OT6166全景行车记录仪六层高速PCB设计实战视频
  8. oracle sqlldr decode,sqlldr的用法总结
  9. 光学成像原理之景深(Depth of Field)
  10. 苦橙花---不喧哗,自有声
  11. log(二)——MDC实现之ThreadLocal
  12. vue+高德地图绘制路径
  13. echats 柱状图的点击事件及高亮
  14. Map集合以及Map集合的实现类Stream流的使用
  15. 计算机学习资料(全)——含视频资料
  16. 程序人生:心中的那朵花
  17. 内网环境能连接数据库 使用vpn用工具能连接数据库但是java驱动连接不了
  18. 电子邮箱免费申请注册教程,公司企业邮箱怎么开通注册?
  19. Azure Key Vault入门
  20. 眼下最流行的五大CSS框架,你都知道么?(转)

热门文章

  1. from...import
  2. 网络信息安全攻防学习平台-基础关
  3. 如何设计一枚「拟态」按钮
  4. 理想低通滤波器(频率域滤波)
  5. Linux Ethernet PHY 驱动
  6. 再谈FEC与UDP可靠传输,音视频FEC 应用
  7. 最新手机号验证正则表达式(电信、移动、广电号段)
  8. 服务器配置jdk环境
  9. 【JS】对象数组去重+查重+合并同类项
  10. 图灵机器人api调用