realsense D435 D435i D415深度相机在ros下获得RGB图、左右红外摄像图、深度图、IMU数据
生活随笔
收集整理的這篇文章主要介紹了
realsense D435 D435i D415深度相机在ros下获得RGB图、左右红外摄像图、深度图、IMU数据
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
首先你要你確保你的相機驅(qū)動已經(jīng)安裝好,環(huán)境配置可以看我的另一篇文章:https://blog.csdn.net/weixin_46195203/article/details/119205851
**第一步:**新建一個文件 informationread(此處建議文件名為:informationread,這樣就不用單獨修改Makelist中的信息了)
**第二步:**在 informationread文件夾下新建兩個文件分別為build 和 src 還有一個CMakeLists.txt文件
**第三步:**在CMakeLists.txt文件中復(fù)制一下代碼
**第四步:**將以下代碼復(fù)制到一個.cpp文件 放到src文件中(此處建議cpp文件名為:GetAlldata.cpp,這樣就不用單獨修改Makelist中的信息了)
#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);//紅外圖像1cfg.enable_stream(RS2_STREAM_INFRARED, 2, width, height, RS2_FORMAT_Y8, fps);//紅外圖像2//若想獲得imu數(shù)據(jù)將一下兩行代碼取消注釋,并修改imu的賦值//cfg.enable_stream(RS2_STREAM_ACCEL, RS2_FORMAT_MOTION_XYZ32F);//陀螺儀//cfg.enable_stream(RS2_STREAM_GYRO, RS2_FORMAT_MOTION_XYZ32F);//陀螺儀int imu = 0;//若想獲取imu數(shù)據(jù),此處設(shè)置為1// 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(imu){if (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; }代碼中默認(rèn)是D415 或者D435 這種不帶imu的相機所以沒有輸出imu信息
如果你想要imu信息 按照指示進(jìn)行小修改便可。
**第四步:**在build文件中打開命令窗口輸入
cmake .. make -j4 ./informationread到此便可以看到實時運行結(jié)果了
總結(jié)
以上是生活随笔為你收集整理的realsense D435 D435i D415深度相机在ros下获得RGB图、左右红外摄像图、深度图、IMU数据的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: TQ2440开发板学习纪实(4)---
- 下一篇: D415的使用