实现功能:
1. 获取realsense设备的 rgb、depth和两个IR红外视频流;
2.摁下p键,保存rgb、depth和两个IR红外各一张图至工程所在文件夹;
// License: Apache 2.0. See LICENSE file in root directory.
// Copyright(c) 2017 Intel Corporation. All Rights Reserved.
#include <librealsense2/rs.hpp> // Include RealSense Cross Platform API
#include <opencv2/opencv.hpp> // Include OpenCV API
#include <fstream> // File IO
#include <iostream> // Terminal IO
#include <sstream> // Stringstreams
#include <direct.h>
using namespace std;
using namespace cv;
#define RS_WIDTH 640//1280
#define RS_HEIGHT 480//720
#define RS_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();
// Declare depth colorizer for pretty visualization of depth data
rs2::colorizer color_map;
// Declare RealSense pipeline, encapsulating the actual device and sensors
rs2::pipeline pipe;
rs2::config cfg;//创建一个以非默认配置的配置用来配置管道
//Add desired streams to configuration
//彩色和深度可以不配置,按默认值输出;红外图必须进行配置,否则无法显示
cfg.enable_stream(RS2_STREAM_COLOR, RS_WIDTH, RS_HEIGHT, RS2_FORMAT_BGR8, RS_FPS);//向配置添加所需的流
cfg.enable_stream(RS2_STREAM_DEPTH, RS_WIDTH, RS_HEIGHT, RS2_FORMAT_Z16, RS_FPS);
cfg.enable_stream(RS2_STREAM_INFRARED, 1, RS_WIDTH, RS_HEIGHT, RS2_FORMAT_Y8, RS_FPS);
cfg.enable_stream(RS2_STREAM_INFRARED, 2, RS_WIDTH, RS_HEIGHT, RS2_FORMAT_Y8, RS_FPS);
// Start streaming with default recommended configuration
pipe.start(cfg);//指示管道使用所请求的配置启动流
const auto window_name = "Display Image";
namedWindow(window_name, WINDOW_AUTOSIZE);
char pBuf[255]; //存放路径的变量
_getcwd(pBuf, 255); //获取程序的当前目录
while (getWindowProperty(window_name, WND_PROP_AUTOSIZE) >= 0)
{
rs2::frameset data = pipe.wait_for_frames(); // Wait for next set of frames from the camera
rs2::frame depth = data.get_depth_frame().apply_filter(color_map);//Apply color map for visualization of depth// Find and colorize the depth data.如果没有apply_filter(color_map)深度图像为灰度图,对应CV_8UC1
rs2::frame rgb = data.get_color_frame();//.apply_filter(color_map);
rs2::frame ir1=data.get_infrared_frame(1);
rs2::frame ir2 = data.get_infrared_frame(2);
// Query frame size (width and height)
const int depthW = depth.as<rs2::video_frame>().get_width();
const int depthH = depth.as<rs2::video_frame>().get_height();
const int rgbW = rgb.as<rs2::video_frame>().get_width();
const int rgbH = rgb.as<rs2::video_frame>().get_height();
const int irW = ir1.as<rs2::video_frame>().get_width();
const int irH = ir1.as<rs2::video_frame>().get_height();
// Create OpenCV matrix of size (w,h) from the colorized depth data
Mat depthImage(Size(depthW, depthH), CV_8UC3, (void*)depth.get_data(), Mat::AUTO_STEP);
//Mat depthImage(Size(depthW, depthH), CV_8UC1, (void*)depth.get_data(), Mat::AUTO_STEP);
Mat rgbImage(Size(rgbW, rgbH), CV_8UC3, (void*)rgb.get_data(), Mat::AUTO_STEP);
Mat irImage(Size(irW, irH), CV_8UC1, (void*)ir1.get_data(), Mat::AUTO_STEP);
Mat ir2Image(Size(irW, irH), CV_8UC1, (void*)ir2.get_data(), Mat::AUTO_STEP);
stringstream depthFile;
stringstream rgbFile;
stringstream irFile;
stringstream ir2File;
static int image_num = 1;
char c = (char)waitKey(1);
if (c == 27)
std::cout<<"Exit boss"<<std::endl;//break;
switch (c)
{
case 'p':
depthFile.clear();
depthFile << (string)pBuf <<"/depth" << image_num << ".jpg";
imwrite(depthFile.str(), depthImage);//保存图片
rgbFile.clear();
rgbFile << (string)pBuf << "/rgb" << image_num << ".jpg";
imwrite(rgbFile.str(), rgbImage);//保存图片
irFile.clear();
irFile << (string)pBuf << "/ir1-" << image_num << ".jpg";
imwrite(irFile.str(), irImage);//保存图片
ir2File.clear();
ir2File << (string)pBuf << "/ir2-" << image_num << ".jpg";
imwrite(ir2File.str(), ir2Image);//保存图片
image_num++;
cout << image_num << endl;
break;
default:
break;
}
// Update the window with new data
imshow(window_name, depthImage);
}
return EXIT_SUCCESS;
}
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;
}