依赖opencv,我这里用的412
1 #include <librealsense2/rs.hpp> 2 3 #include<iostream> 4 5 // include OpenCV header file 6 #include <opencv2/opencv.hpp> 7 using namespace cv; 8 9 #define RS_WIDTH 640 10 #define RS_HEIGHT 480 11 #define RS_FPS 60 12 13 14 int main(int argc, char** argv) try 15 { 16 // judge whether devices is exist or not 17 rs2::context ctx; 18 auto list = ctx.query_devices(); // Get a snapshot of currently connected devices 19 if (list.size() == 0) 20 throw std::runtime_error("No device detected. Is it plugged in?"); 21 rs2::device dev = list.front(); 22 23 // 24 rs2::frameset frames; 25 //Contruct a pipeline which abstracts the device 26 rs2::pipeline pipe;//创建一个通信管道//https://baike.so.com/doc/1559953-1649001.html pipeline的解释 27 //Create a configuration for configuring the pipeline with a non default profile 28 rs2::config cfg;//创建一个以非默认配置的配置用来配置管道 29 //Add desired streams to configuration 30 cfg.enable_stream(RS2_STREAM_COLOR, RS_WIDTH, RS_HEIGHT, RS2_FORMAT_BGR8, RS_FPS);//向配置添加所需的流 31 cfg.enable_stream(RS2_STREAM_DEPTH, RS_WIDTH, RS_HEIGHT, RS2_FORMAT_Z16, RS_FPS); 32 cfg.enable_stream(RS2_STREAM_INFRARED, 1, RS_WIDTH, RS_HEIGHT, RS2_FORMAT_Y8, RS_FPS); 33 cfg.enable_stream(RS2_STREAM_INFRARED, 2, RS_WIDTH, RS_HEIGHT, RS2_FORMAT_Y8, RS_FPS); 34 35 // get depth scale 36 // float depth_scale = get_depth_scale(profile.get_device()); 37 38 // start stream 39 pipe.start(cfg);//指示管道使用所请求的配置启动流 40 41 int c = 0; 42 43 while (true) 44 { 45 frames = pipe.wait_for_frames();//等待所有配置的流生成框架 46 47 // Align to depth 48 /*rs2::align align_to_depth(RS2_STREAM_DEPTH);*/ 49 rs2::align align_to_depth(rs2_stream::RS2_STREAM_COLOR); 50 frames = align_to_depth.process(frames); 51 52 //Get each frame 53 rs2::frame color_frame = frames.get_color_frame(); 54 rs2::frame depth_frame = frames.get_depth_frame(); 55 rs2::video_frame ir_frame_left = frames.get_infrared_frame(1); 56 rs2::video_frame ir_frame_right = frames.get_infrared_frame(2); 57 58 59 // Creating OpenCV Matrix from a color image 60 Mat color(Size(RS_WIDTH, RS_HEIGHT), CV_8UC3, (void*)color_frame.get_data(), Mat::AUTO_STEP); 61 Mat pic_right(Size(RS_WIDTH, RS_HEIGHT), CV_8UC1, (void*)ir_frame_right.get_data()); 62 Mat pic_left(Size(RS_WIDTH, RS_HEIGHT), CV_8UC1, (void*)ir_frame_left.get_data()); 63 Mat pic_depth(Size(RS_WIDTH, RS_HEIGHT), CV_16U, (void*)depth_frame.get_data(), Mat::AUTO_STEP); 64 65 // Display in a GUI 66 namedWindow("Display Image", WINDOW_AUTOSIZE); 67 imshow("Display Image", color); 68 waitKey(1); 69 imshow("Display depth", pic_depth * 15); // *15 for show 70 waitKey(1); 71 imshow("Display pic_left", pic_left); 72 waitKey(1); 73 imshow("Display pic_right", pic_right); 74 waitKey(1); 75 76 std::string dir = "D:\Data\6DOF\"; 77 if (c >= 100) 78 { 79 if (cv::imwrite(dir + "color\color-" + std::to_string(c) + ".png", color) && 80 cv::imwrite(dir + "depth\depth-" + std::to_string(c) + ".png", pic_depth)) 81 { 82 std::cout << std::to_string(c) << "st depth; " << std::to_string(c) << "st color" << std::endl; 83 } 84 } 85 86 c++; 87 } 88 return 0; 89 } 90 91 // error 92 catch (const rs2::error & e) 93 { 94 std::cerr << "RealSense error calling " << e.get_failed_function() << "(" << e.get_failed_args() << "): " << e.what() << std::endl; 95 return EXIT_FAILURE; 96 } 97 catch (const std::exception& e) 98 { 99 std::cerr << e.what() << std::endl; 100 return EXIT_FAILURE; 101 }
简单封装下:
realsense.h
1 #pragma once 2 #ifndef REALSENSE_H 3 #define REALSENSE_H 4 5 #include<librealsense2/rs.hpp> 6 #define RS_WIDTH 640 7 #define RS_HEIGHT 480 8 #define RS_FPS 60 // 调太高导致相机初始化失败 9 10 11 class RealSense 12 { 13 public: 14 RealSense(); 15 16 void GetFrames(); 17 18 ~RealSense(); 19 20 private: 21 rs2::context ctx; 22 23 rs2::frameset frames; 24 25 rs2::pipeline pipe; 26 27 rs2::config cfg; 28 29 public: 30 rs2::frame color_frame; 31 32 rs2::frame depth_frame; 33 34 }; 35 36 #endif
realsense.cpp
1 #include"realsense.h" 2 3 4 RealSense::RealSense() 5 { 6 auto list = ctx.query_devices(); 7 if (list.size() == 0) 8 throw std::runtime_error("No device detected. Is it plugged in?"); 9 //Add desired streams to configuration 10 cfg.enable_stream(RS2_STREAM_COLOR, RS_WIDTH, RS_HEIGHT, RS2_FORMAT_BGR8, RS_FPS); 11 cfg.enable_stream(RS2_STREAM_DEPTH, RS_WIDTH, RS_HEIGHT, RS2_FORMAT_Z16, RS_FPS); 12 13 pipe.start(cfg); 14 } 15 16 void RealSense::GetFrames() 17 { 18 this->frames = this->pipe.wait_for_frames();//等待所有配置的流生成框架 19 20 // Align to depth 21 rs2::align align_to_depth(rs2_stream::RS2_STREAM_COLOR); 22 this->frames = align_to_depth.process(this->frames); 23 24 //Get each frame 25 this->color_frame = frames.get_color_frame(); 26 this->depth_frame = frames.get_depth_frame(); 27 28 } 29 30 RealSense::~RealSense() 31 { 32 }
test.cpp
1 #include<iostream> 2 #include<chrono> 3 #include <opencv2/opencv.hpp> 4 using namespace cv; 5 #include"realsense.h" 6 7 8 int main(int argc, char** argv) 9 { 10 RealSense RS; 11 12 while (true) 13 { 14 std::chrono::steady_clock::time_point t1 = std::chrono::steady_clock::now(); 15 16 RS.GetFrames(); 17 18 // -> OpenCV 19 Mat color(cv::Size(RS_WIDTH, RS_HEIGHT), CV_8UC3, (void*)RS.color_frame.get_data(), cv::Mat::AUTO_STEP); 20 Mat depth(cv::Size(RS_WIDTH, RS_HEIGHT), CV_16U, (void*)RS.depth_frame.get_data(), cv::Mat::AUTO_STEP); 21 22 // Display in a GUI 23 namedWindow("Display Image", WINDOW_AUTOSIZE); 24 imshow("Display Image", color); 25 waitKey(1); 26 imshow("Display depth", depth * 15); // *15 for show 27 waitKey(1); 28 29 std::chrono::steady_clock::time_point t2 = std::chrono::steady_clock::now(); 30 std::chrono::duration<double> time_used = std::chrono::duration_cast<std::chrono::duration<double>>(t2 - t1); 31 std::cout << "the cost of time:" << 1000 * time_used.count() << " milliseconds." << std::endl; 32 } 33 return 0; 34 }
按照时间戳保存
1 #include <librealsense2/rs.hpp> 2 3 4 #include <iostream> 5 #include<string> 6 #include<Windows.h> 7 #include<opencv2/opencv.hpp> 8 9 #define width 640 10 #define height 480 11 #define fps 30 12 13 int main() 14 { 15 // timestamp 16 SYSTEMTIME st = { 0 }; 17 // 18 std::string dir = "F:\Dataset\cube\"; 19 std::string png = ".png"; 20 21 // judge whether devices is exist or not 22 rs2::context ctx; 23 auto list = ctx.query_devices(); // Get a snapshot of currently connected devices 24 if (list.size() == 0) 25 throw std::runtime_error("No device detected. Is it plugged in?"); 26 rs2::device dev = list.front(); 27 // 28 rs2::frameset frames; 29 rs2::pipeline pipe; 30 rs2::config cfg; 31 //Add desired streams to configuration 32 cfg.enable_stream(RS2_STREAM_COLOR, width, height, RS2_FORMAT_BGR8, fps);//向配置添加所需的流 33 cfg.enable_stream(RS2_STREAM_DEPTH, width, height, RS2_FORMAT_Z16, fps); 34 // start stream 35 pipe.start(cfg);//指示管道使用所请求的配置启动流 36 while (true) 37 { 38 frames = pipe.wait_for_frames();//等待所有配置的流生成框架 39 40 // Align to depth 41 rs2::align align_to_depth(rs2_stream::RS2_STREAM_COLOR); 42 frames = align_to_depth.process(frames); 43 44 //Get each frame 45 rs2::frame color_frame = frames.get_color_frame(); 46 rs2::frame depth_frame = frames.get_depth_frame(); 47 rs2::video_frame ir_frame_left = frames.get_infrared_frame(1); 48 rs2::video_frame ir_frame_right = frames.get_infrared_frame(2); 49 50 51 // Creating OpenCV Matrix from a color image 52 cv::Mat color(cv::Size(width, height), CV_8UC3, (void*)color_frame.get_data(), cv::Mat::AUTO_STEP); 53 cv::Mat pic_depth(cv::Size(width, height), CV_16U, (void*)depth_frame.get_data(), cv::Mat::AUTO_STEP); 54 55 // Display in a GUI 56 cv::namedWindow("Display Image", cv::WINDOW_AUTOSIZE); 57 imshow("Display Image", color); 58 //cv::waitKey(1); 59 //cv::imshow("Display depth", pic_depth * 15); // *15 for show 60 //cv::waitKey(1); 61 62 // 按照时间戳保存 63 GetLocalTime(&st); 64 std::string t = std::to_string(st.wYear) + 65 std::to_string(st.wMonth) + 66 std::to_string(st.wDay) + 67 std::to_string(st.wHour) + 68 std::to_string(st.wMinute) + 69 std::to_string(st.wSecond) + 70 std::to_string(st.wMilliseconds); 71 //std::cout << t << std::endl; 72 if (cv::waitKey(1) == 'C') 73 { 74 cv::imwrite(dir + t + png, color); 75 } 76 77 78 } 79 return 0; 80 }