日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

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

發(fā)布時間:2023/12/20 编程问答 34 豆豆
生活随笔 收集整理的這篇文章主要介紹了 realsense D435 D435i D415深度相机在ros下获得RGB图、左右红外摄像图、深度图、IMU数据 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

首先你要你確保你的相機驅動已經(jīng)安裝好,環(huán)境配置可以看我的另一篇文章:https://blog.csdn.net/weixin_46195203/article/details/119205851

**第一步:**新建一個文件 informationread(此處建議文件名為:informationread,這樣就不用單獨修改Makelist中的信息了)
**第二步:**在 informationread文件夾下新建兩個文件分別為build 和 src 還有一個CMakeLists.txt文件
**第三步:**在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(informationread src/GetAlldata.cpp)target_link_libraries(informationread ${OpenCV_LIBS} ${realsense2_LIBRARY})

**第四步:**將以下代碼復制到一個.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ù),此處設置為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; }

代碼中默認是D415 或者D435 這種不帶imu的相機所以沒有輸出imu信息
如果你想要imu信息 按照指示進行小修改便可。

**第四步:**在build文件中打開命令窗口輸入

cmake .. make -j4 ./informationread

到此便可以看到實時運行結果了

總結

以上是生活随笔為你收集整理的realsense D435 D435i D415深度相机在ros下获得RGB图、左右红外摄像图、深度图、IMU数据的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。