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;
}

运行后

相关推荐
彤银浦14 分钟前
python学习记录7
python·学习
少女忧15 分钟前
51单片机学习第六课---B站UP主江协科技
科技·学习·51单片机
LabVIEW开发31 分钟前
LabVIEW程序怎么解决 Bug?
bug·labview
优雅的小武先生1 小时前
QT中的按钮控件和comboBox控件和spinBox控件无法点击的bug
开发语言·qt·bug
邓校长的编程课堂1 小时前
助力信息学奥赛-VisuAlgo:提升编程与算法学习的可视化工具
学习·算法
missmisslulu2 小时前
电容笔值得买吗?2024精选盘点推荐五大惊艳平替电容笔!
学习·ios·电脑·平板
yunhuibin2 小时前
ffmpeg面向对象——拉流协议匹配机制探索
学习·ffmpeg
hengzhepa2 小时前
ElasticSearch备考 -- Search across cluster
学习·elasticsearch·搜索引擎·全文检索·es
蜡笔小新星3 小时前
Python Kivy库学习路线
开发语言·网络·经验分享·python·学习
攸攸太上3 小时前
JMeter学习
java·后端·学习·jmeter·微服务