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

一.介绍

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

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

二.实现方法及代码演示

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

首先给出CMakeLists.txt文件

CMakeists.txt

cmake_minimum_required(VERSION 2.8)
project(realsense)

set(CMAKE_BUILD_TYPE "Release")
# 添加c++ 11标准支持
set(CMAKE_CXX_FLAGS "-std=c++11 -O2")


# 寻找OpenCV库
find_package(OpenCV REQUIRED)
find_package(realsense2 REQUIRED)
find_package(Threads REQUIRED)

set(CMAKE_CXX_FLAGS
   "${CMAKE_CXX_FLAGS} -Wall -std=c++0x"
)

# 添加头文件
include_directories(${OpenCV_INCLUDE_DIRS})

add_executable(realsense src/main.cpp)
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

#include <iostream>
#include<stdlib.h>
#include<stdio.h>
#include<string>

#include<opencv2/imgproc/imgproc.hpp>
#include<opencv2/core/core.hpp>
#include<opencv2/highgui/highgui.hpp>
 
#include<librealsense2/rs.hpp>

using namespace std;
using namespace cv;
 
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);
 
//    //获取深度像素与长度单位的关系
//    float depth_scale = get_depth_scale(profile.get_device());
//    rs2_stream align_to = find_stream_to_align(profile.get_streams());
 
    while(waitKey(1)){
        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_image",depth_image);
        imshow("color_image",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;
}

运行后

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-03-13 18:04:03       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-03-13 18:04:03       100 阅读
  3. 在Django里面运行非项目文件

    2024-03-13 18:04:03       82 阅读
  4. Python语言-面向对象

    2024-03-13 18:04:03       91 阅读

热门阅读

  1. 一篇文章讲清楚HashMap

    2024-03-13 18:04:03       41 阅读
  2. 【数据结构学习笔记】选择排序

    2024-03-13 18:04:03       32 阅读
  3. Leetcode刷题笔记——贪心篇

    2024-03-13 18:04:03       34 阅读
  4. 完整的模型训练套路及GPU的利用

    2024-03-13 18:04:03       43 阅读
  5. 听力 3.12

    2024-03-13 18:04:03       37 阅读
  6. 万能近似定理

    2024-03-13 18:04:03       46 阅读
  7. C++之std::move

    2024-03-13 18:04:03       42 阅读