實作功能:
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;
}