讀取D435圖片
開發環境:
- ubuntu 16.04LTS
- Intel Realsense SDK2.0
- C++
- Opencv3.4
- CMake
網上關於Inetl Reaslsense SDK2.0的教程甚少,這裏邊學習邊做系列教程
這個沒什麼原理好講,直接上代碼以及註釋:
#include <iostream>
#include<stdlib.h>
#include<stdio.h>
#include<string>
using namespace std;
#include<opencv2/imgproc/imgproc.hpp>
#include<opencv2/core/core.hpp>
#include<opencv2/highgui/highgui.hpp>
using namespace cv;
#include<librealsense2/rs.hpp>
int main() try
{
//聲明彩色圖
rs2::colorizer color_map;
//聲明realsense管道,
rs2::pipeline pipe;
//數據流配置信息【這步其實很重要】
rs2::config pipe_config;
pipe_config.enable_stream(RS2_STREAM_DEPTH,640,480,RS2_FORMAT_Z16,30);
pipe_config.enable_stream(RS2_STREAM_COLOR,640,480,RS2_FORMAT_BGR8,30);
//開始傳送數據流
rs2::pipeline_profile profile=pipe.start(pipe_config);
const char* depth_win="depth_Image";
namedWindow(depth_win,WINDOW_AUTOSIZE);
const char* color_win="color_Image";
namedWindow(color_win,WINDOW_AUTOSIZE);
// //獲取深度像素與長度單位的關係
// float depth_scale = get_depth_scale(profile.get_device());
// rs2_stream align_to = find_stream_to_align(profile.get_streams());
while(waitKey(1)&&cvGetWindowHandle(depth_win)){
rs2::frameset data=pipe.wait_for_frames();//等待下一幀
rs2::frame depth=data.get_depth_frame().apply_filter(color_map);//獲取深度圖,加顏色濾鏡
rs2::frame color=data.get_color_frame();
//獲取寬高
const int depth_w=depth.as<rs2::video_frame>().get_width();
const int depth_h=depth.as<rs2::video_frame>().get_height();
const int color_w=color.as<rs2::video_frame>().get_width();
const int color_h=color.as<rs2::video_frame>().get_height();
//創建OPENCV類型 並傳入數據
Mat depth_image(Size(depth_w,depth_h),CV_8UC3,(void*)depth.get_data(),Mat::AUTO_STEP);
Mat color_image(Size(color_w,color_h),CV_8UC3,(void*)color.get_data(),Mat::AUTO_STEP);
//顯示
imshow(depth_win,depth_image);
imshow(color_win,color_image);
}
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
project(realsense_readPic)
cmake_minimum_required(VERSION 2.8)
aux_source_directory(. SRC_LIST)
add_executable(${PROJECT_NAME} ${SRC_LIST})
set(CMAKE_CXX_FLAGS "-std=c++11")
#尋找opencv庫
find_package(OpenCV REQUIRED)
#message(STATUS ${OpenCV_INCLUDE_DIRS})
#添加頭文件
include_directories(${OpenCV_INCLUDE_DIRS})
#鏈接Opencv庫
target_link_libraries(realsense_readPic ${OpenCV_LIBS} )
#添加後可進行調試
set( CMAKE_BUILD_TYPE Debug )
set(DEPENDENCIES realsense2 )
target_link_libraries(realsense_readPic ${DEPENDENCIES})
實現效果: