realsense SDK2.0学习1——读取D435视频【彩色&&深度】

读取D435图片


开发环境:

  • ubuntu 16.04LTS
  • Intel Realsense SDK2.0
  • C++
  • Opencv3.4
  • CMake

 网上关于Inetl Reaslsense SDK2.0的教程甚少,这里边学习边做系列教程

这个没什么原理好讲,直接上代码以及注释:


   
   
   
   
  1. #include
  2. #include
  3. #include
  4. #include
  5. using namespace std;
  6. #include
  7. #include
  8. #include
  9. using namespace cv;
  10. #include
  11. int main() try
  12. {
  13. //声明彩色图
  14. rs2::colorizer color_map;
  15. //声明realsense管道,
  16. rs2::pipeline pipe;
  17. //数据流配置信息【这步其实很重要】
  18. rs2::config pipe_config;
  19. pipe_config.enable_stream(RS2_STREAM_DEPTH, 640, 480,RS2_FORMAT_Z16, 30);
  20. pipe_config.enable_stream(RS2_STREAM_COLOR, 640, 480,RS2_FORMAT_BGR8, 30);
  21. //开始传送数据流
  22. rs2::pipeline_profile profile=pipe.start(pipe_config);
  23. const char* depth_win= "depth_Image";
  24. namedWindow(depth_win,WINDOW_AUTOSIZE);
  25. const char* color_win= "color_Image";
  26. namedWindow(color_win,WINDOW_AUTOSIZE);
  27. // //获取深度像素与长度单位的关系
  28. // float depth_scale = get_depth_scale(profile.get_device());
  29. // rs2_stream align_to = find_stream_to_align(profile.get_streams());
  30. while(waitKey( 1)&&cvGetWindowHandle(depth_win)){
  31. rs2::frameset data=pipe.wait_for_frames(); //等待下一帧
  32. rs2::frame depth=data.get_depth_frame().apply_filter(color_map); //获取深度图,加颜色滤镜
  33. rs2::frame color=data.get_color_frame();
  34. //获取宽高
  35. const int depth_w=depth. as().get_width();
  36. const int depth_h=depth. as().get_height();
  37. const int color_w=color. as().get_width();
  38. const int color_h=color. as().get_height();
  39. //创建OPENCV类型 并传入数据
  40. Mat depth_image(Size(depth_w,depth_h),CV_8UC3,(void*)depth.get_data(),Mat::AUTO_STEP);
  41. Mat color_image(Size(color_w,color_h),CV_8UC3,(void*)color.get_data(),Mat::AUTO_STEP);
  42. //显示
  43. imshow(depth_win,depth_image);
  44. imshow(color_win,color_image);
  45. }
  46. return EXIT_SUCCESS;
  47. }
  48. catch ( const rs2::error &e){
  49. std::cout<< "RealSense error calling"<"("<"):\n"
  50. <
  51. return EXIT_FAILURE;
  52. }
  53. catch ( const std::exception &e){
  54. std::cout<
  55. return EXIT_FAILURE;
  56. }

CMakeLists.txt


   
   
   
   
  1. project(realsense_readPic)
  2. cmake_minimum_required(VERSION 2.8)
  3. aux_source_directory(. SRC_LIST)
  4. add_executable( ${PROJECT_NAME} ${SRC_LIST})
  5. set(CMAKE_CXX_FLAGS "-std=c++11")
  6. #寻找opencv库
  7. find_package(OpenCV REQUIRED)
  8. #message(STATUS ${OpenCV_INCLUDE_DIRS})
  9. #添加头文件
  10. include_directories( ${OpenCV_INCLUDE_DIRS})
  11. #链接Opencv库
  12. target_link_libraries(realsense_readPic ${OpenCV_LIBS} )
  13. #添加后可进行调试
  14. set( CMAKE_BUILD_TYPE Debug )
  15. set(DEPENDENCIES realsense2 )
  16. target_link_libraries(realsense_readPic ${DEPENDENCIES})

实现效果: 

你可能感兴趣的:(PCL学习,RealSense,vSLAM)