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 小时前
倍增法和ST算法 个人学习笔记&代码
笔记·学习·算法
aha-凯心1 小时前
前端学习 vben 之 axios interceptors
前端·学习
许白掰3 小时前
Linux入门篇学习——Linux 编写第一个自己的命令
linux·运维·数据库·嵌入式硬件·学习
生如夏花℡3 小时前
HarmonyOS学习记录4
学习·华为·harmonyos
彤银浦3 小时前
Web学习笔记2
笔记·学习
长风破浪会有时呀3 小时前
Git 学习笔记
笔记·git·学习
偷偷的卷3 小时前
【算法笔记 day three】滑动窗口(其他类型)
数据结构·笔记·python·学习·算法·leetcode
大白的编程日记.4 小时前
【计算机基础理论知识】C++篇(二)
开发语言·c++·学习
Chef_Chen4 小时前
从0开始学习R语言--Day42--LM检验
学习
C语言小火车4 小时前
野指针:C/C++内存管理的“幽灵陷阱”与系统化规避策略
c语言·c++·学习·指针