首先你要你确保你的相机驱动已经安装好,环境配置可以看我的另一篇文章: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 30
int 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 devices
if (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 device
rs2::pipeline pipe;//建立一个通讯管道//https://baike.so.com/doc/1559953-1649001.html pipeline的解释
//Create a configuration for configuring the pipeline with a non default profile
rs2::config cfg;
//Add desired streams to configuration
cfg.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);//红外图像1
cfg.enable_stream(RS2_STREAM_INFRARED, 2, width, height, RS2_FORMAT_Y8, fps);//红外图像2
//若想获得imu数据将一下两行代码取消注释,并修改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数据,此处设置为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 data
if(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 frame
rs2::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 image
Mat 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 GUI
namedWindow("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
到此便可以看到实时运行结果了