当前位置:   article > 正文

Realsense 相机SDK学习(一)——librealsense使用方法及bug解决(不使用Ros)

realsense

一.介绍

realsense相机是一个intel开发出来的一款深度相机,我之前使用他来跑过slam,也配置过他的驱动,在此附上realsense的相机驱动安装方法:Ubuntu20.04安装Intelrealsense相机驱动(涉及Linux内核降级)

本人在之前使用realsense相机时,一直都是用ros驱动的,觉得很苦恼,好好的一个相机为什么非得使用Ros才能驱动呢,也太麻烦了,因此就有了不使用ros来驱动相机完成一些小项目的想法,如下。

二.实现方法及代码演示

到此,假设读者已经按照上面给出的链接顺利的安装好相机驱动,接下来我们使用C++来调用ros

首先给出CMakeLists.txt文件

CMakeists.txt

  1. cmake_minimum_required(VERSION 2.8)
  2. project(realsense)
  3. set(CMAKE_BUILD_TYPE "Release")
  4. # 添加c++ 11标准支持
  5. set(CMAKE_CXX_FLAGS "-std=c++11 -O2")
  6. # 寻找OpenCV库
  7. find_package(OpenCV REQUIRED)
  8. find_package(realsense2 REQUIRED)
  9. find_package(Threads REQUIRED)
  10. set(CMAKE_CXX_FLAGS
  11. "${CMAKE_CXX_FLAGS} -Wall -std=c++0x"
  12. )
  13. # 添加头文件
  14. include_directories(${OpenCV_INCLUDE_DIRS})
  15. add_executable(realsense src/main.cpp)
  16. target_link_libraries(realsense ${OpenCV_LIBS} ${realsense2_LIBRARY})

其中,最重要的两行代码是find_package(realsense2 REQUIRED) 和target_link_libraries(realsense ${OpenCV_LIBS} ${realsense2_LIBRARY}),这样我们的代码就可以链接到realsense库上了

main.cpp

好像还需要把librealsense2/rs.hpp找到,发现他的路径是/usr/local/include/,因此我把他移动到了/usr/include/下,使用指令

sudo cp -r /usr/local/include/librealsense2 /usr/include

  1. #include <iostream>
  2. #include<stdlib.h>
  3. #include<stdio.h>
  4. #include<string>
  5. #include<opencv2/imgproc/imgproc.hpp>
  6. #include<opencv2/core/core.hpp>
  7. #include<opencv2/highgui/highgui.hpp>
  8. #include<librealsense2/rs.hpp>
  9. using namespace std;
  10. using namespace cv;
  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. // //获取深度像素与长度单位的关系
  24. // float depth_scale = get_depth_scale(profile.get_device());
  25. // rs2_stream align_to = find_stream_to_align(profile.get_streams());
  26. while(waitKey(1)){
  27. rs2::frameset data=pipe.wait_for_frames();//等待下一帧
  28. rs2::frame depth=data.get_depth_frame().apply_filter(color_map);//获取深度图,加颜色滤镜
  29. rs2::frame color=data.get_color_frame();
  30. //获取宽高
  31. const int depth_w=depth.as<rs2::video_frame>().get_width();
  32. const int depth_h=depth.as<rs2::video_frame>().get_height();
  33. const int color_w=color.as<rs2::video_frame>().get_width();
  34. const int color_h=color.as<rs2::video_frame>().get_height();
  35. //创建OPENCV类型 并传入数据
  36. Mat depth_image(Size(depth_w,depth_h),CV_8UC3,(void*)depth.get_data(),Mat::AUTO_STEP);
  37. Mat color_image(Size(color_w,color_h),CV_8UC3,(void*)color.get_data(),Mat::AUTO_STEP);
  38. //显示
  39. imshow("depth_image",depth_image);
  40. imshow("color_image",color_image);
  41. }
  42. return EXIT_SUCCESS;
  43. }
  44. catch (const rs2::error &e){
  45. std::cout<<"RealSense error calling"<<e.get_failed_function()<<"("<<e.get_failed_args()<<"):\n"
  46. <<e.what()<<endl;
  47. return EXIT_FAILURE;
  48. }
  49. catch (const std::exception &e){
  50. std::cout<<e.what()<<endl;
  51. return EXIT_FAILURE;
  52. }

运行后

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/神奇cpp/article/detail/975970
推荐阅读
相关标签
  

闽ICP备14008679号