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

运行后

相关推荐
许白掰24 分钟前
Linux入门篇学习——借助 U 盘或 TF 卡拷贝程序到开发板上
linux·学习·借助 u 盘拷贝程序到开发板上·借助 tf卡拷贝程序到开发板上
iFulling12 小时前
【计算机网络】第四章:网络层(上)
学习·计算机网络
香蕉可乐荷包蛋12 小时前
AI算法之图像识别与分类
人工智能·学习·算法
xiaoli232712 小时前
课题学习笔记1——文本问答与信息抽取关键技术研究论文阅读(用于无结构化文本问答的文本生成技术)
笔记·学习
人生游戏牛马NPC1号13 小时前
学习 Flutter (四):玩安卓项目实战 - 中
android·学习·flutter
LGGGGGQ14 小时前
嵌入式学习-PyTorch(7)-day23
人工智能·pytorch·学习
stm 学习ing14 小时前
Python暑期学习笔记3
笔记·python·学习
屁股割了还要学14 小时前
【C语言进阶】内存函数
c语言·开发语言·学习·算法·青少年编程
靴子学长14 小时前
Lotus-基于大模型的查询引擎 -开源学习整理
python·学习·自然语言处理
Littlewith15 小时前
Node.js:创建第一个应用
服务器·开发语言·后端·学习·node.js