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

运行后

  • 6
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值