代码
问题描述:
- realsense没有分离通道的python版本,因此尝试用c++写一个
c++源码
// realsense export left and right image and depth image
#include <iostream>
#include <sstream>
#include <stdlib.h>
#include <stdio.h>
#include <string>
#include <fstream>
#include <algorithm>
#include <cstring>
#include<opencv2/imgproc/imgproc.hpp>
#include<opencv2/core/core.hpp>
#include<opencv2/highgui/highgui.hpp>
#include <opencv2/highgui/highgui_c.h>
#include <librealsense2/rs.hpp> // Include RealSense Cross Platform API
#include <librealsense2/rsutil.h>
using namespace std;
// using namespace cv;
// using namespace std;
// Capture Example demonstrates how to
// capture depth and color video streams and render them to the screen
int main(int argc, char * argv[]) try
{
rs2::colorizer color_map;
rs2::config cfg;
cfg.enable_stream(RS2_STREAM_INFRARED, 1, 1280, 720, RS2_FORMAT_Y8, 30);
cfg.enable_stream(RS2_STREAM_INFRARED, 2, 1280, 720, RS2_FORMAT_Y8, 30);
cfg.enable_stream(RS2_STREAM_COLOR, 1280, 720, RS2_FORMAT_BGR8, 30);
cfg.enable_stream(RS2_STREAM_DEPTH, 1280, 720, RS2_FORMAT_Z16, 30);
rs2::pipeline pipe;
pipe.start(cfg);
bool stop = false;
const auto window_name = "Display Image";
cv::namedWindow(window_name, CV_WINDOW_AUTOSIZE);
while(!stop){
rs2::frameset frames = pipe.wait_for_frames();
auto ir_frame_left = frames.get_infrared_frame(1);
auto ir_frame_right = frames.get_infrared_frame(2);
auto colored_frame = frames.get_color_frame();
auto depth_frame = frames.get_depth_frame().apply_filter(color_map);
//save files
cv::Mat dMat_left = cv::Mat(cv::Size(1280, 720), CV_8UC1, (void*)ir_frame_left.get_data(),cv::Mat::AUTO_STEP);
cv::Mat dMat_right = cv::Mat(cv::Size(1280, 720), CV_8UC1, (void*)ir_frame_right.get_data(),cv::Mat::AUTO_STEP);
cv::Mat dMat_colored = cv::Mat(cv::Size(1280, 720), CV_8UC3, (void*)colored_frame.get_data(),cv::Mat::AUTO_STEP);
cv::Mat dMat_depth = cv::Mat(cv::Size(1280, 720), CV_8UC3, (void*)depth_frame.get_data(),cv::Mat::AUTO_STEP);
//cv::Mat dMat_depth = cv::Mat(cv::Size(1280, 720), CV_16U, (void*)depth_frame.get_data(),cv::Mat::AUTO_STEP);
//cv::imwrite( "/home/work/Documents/irLeft0.png", dMat_left );
//cv::imwrite( "/home/work/Documents/irRight0.png", dMat_right );
//cv::imwrite( "/home/work/Documents/coloredCV0.png", dMat_colored);
//cv::imwrite( "/home/work/Documents/depth0.png", dMat_depth);
cv::imshow("ir_frame_left", dMat_left);
cv::imshow("ir_frame_right", dMat_right);
cv::imshow("depth", dMat_depth);
cv::imshow("colored_frame", dMat_colored);
char key = cv::waitKey(1);
// Press esc or 'q' to close the image window
if (key == 27){
stop = true;
cv::destroyAllWindows();
break;
}
}
return EXIT_SUCCESS;
}
catch (const rs2::error &e){
std::cout<<"RealSense error calling"<<e.get_failed_function()<<"("<<e.get_failed_args()<<"):\n"
<<e.what()<<endl;
return EXIT_FAILURE;
}
catch (const std::exception &e){
std::cout<<e.what()<<endl;
return EXIT_FAILURE;
}
CMakeLists.txt
# License: Apache 2.0. See LICENSE file in root directory.
# Copyright(c) 2019 Intel Corporation. All Rights Reserved.
# minimum required cmake version: 3.1.0
cmake_minimum_required(VERSION 3.1.0)
project(RealsenseSpearateIMGS)
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(export_seprate_images export_seperate_images.cpp)
target_link_libraries( export_seprate_images ${OpenCV_LIBS} ${realsense2_LIBRARY})
遇到的问题
rs2::colorizer color_map;
要在前面声明,否则深度图depth_frame = frames.get_depth_frame().apply_filter(color_map);
会报错- 深度图采用
color_map
时,需要使用CV_8UC3
格式,即dMat_depth = cv::Mat(cv::Size(1280, 720), CV_8UC3, (void*)depth_frame.get_data(),cv::Mat::AUTO_STEP);