Ubuntu-Realsense开发(一)

1.获取数据流

// include the librealsense C++ header file
#include <librealsense2/rs.hpp>

// include OpenCV header file
#include <opencv2/opencv.hpp>

using namespace std;
using namespace cv;

int main()
{
    //Contruct a pipeline which abstracts the device
    rs2::pipeline pipe;
    rs2::colorizer color_map;
    //Create a configuration for configuring the pipeline with a non default profile
    rs2::config cfg;

    //Add desired streams to configuration
    cfg.enable_stream(RS2_STREAM_COLOR, 640, 480, RS2_FORMAT_BGR8, 30);
    //cfg.enable_stream(RS2_STREAM_INFRARED, 640, 480, RS2_FORMAT_Y8, 30);
    cfg.enable_stream(RS2_STREAM_DEPTH, 640, 480, RS2_FORMAT_Z16, 30);

    //Instruct pipeline to start streaming with the requested configuration
    pipe.start(cfg);
//    const auto color_win = "color Image";
//    namedWindow(color_win, WINDOW_AUTOSIZE);
//    const auto depth_win = "depth Image";
//    namedWindow(depth_win, WINDOW_AUTOSIZE);

    while (1)
    {
        //Wait for all configured streams to produce a frame
        rs2::frameset frames = pipe.wait_for_frames();

        //Get each frame
        rs2::frame color_frame = frames.get_color_frame();
        //rs2::frame ir_frame = frames.first(RS2_STREAM_INFRARED);
        rs2::frame depth_frame1 = frames.get_depth_frame().apply_filter(color_map);
        rs2::frame depth_frame2= frames.get_depth_frame();

        // Creating OpenCV Matrix from a color image
        Mat color(Size(640, 480), CV_8UC3, (void*)color_frame.get_data(), Mat::AUTO_STEP);
        //Mat ir(Size(640, 480), CV_8UC1, (void*)ir_frame.get_data(), Mat::AUTO_STEP);
        Mat depth1(Size(640, 480), CV_8UC3, (void*)depth_frame1.get_data(), Mat::AUTO_STEP);
        Mat depth2(Size(640, 480), CV_16UC1, (void*)depth_frame2.get_data(), Mat::AUTO_STEP);

        // Apply Histogram Equalization
       // equalizeHist(ir, ir);
        //applyColorMap(ir, ir, COLORMAP_JET);

        // Display in a GUI
        imshow("depth Image", depth1);
        imshow("depth Image2", depth2);
        //imshow("Display ir", ir);
        imshow("color Image",color );
        waitKey(10);
    }
    return 0;
}

CMakeLists.txt

cmake_minimum_required(VERSION 3.10)
project(rs_test)
set(CMAKE_CXX_STANDARD 11)
#set(CMAKE_CXX_FLAGS "-std=c++11")

#aux_source_directory(. main.cpp)
add_executable(rs_test main.cpp)

#寻找opencv库
find_package(OpenCV REQUIRED)
#message(STATUS ${OpenCV_INCLUDE_DIRS})
#添加头文件
include_directories(${OpenCV_INCLUDE_DIRS})
#链接Opencv库
target_link_libraries(rs_test ${OpenCV_LIBS} )
#添加后可进行调试
set( CMAKE_BUILD_TYPE Debug )
set(DEPENDENCIES realsense2 )
target_link_libraries(rs_test ${DEPENDENCIES})

2.深度对齐到彩色

// include the librealsense C++ header file
#include <librealsense2/rs.hpp>

// include OpenCV header file
#include <opencv2/opencv.hpp>

using namespace std;
using namespace cv;


float get_depth_scale(rs2::device dev);
bool profile_changed(const std::vector<rs2::stream_profile>& current, const std::vector<rs2::stream_profile>& prev);

int main()
{
    //Contruct a pipeline which abstracts the device
    rs2::pipeline pipe;
    rs2::colorizer color_map;
    //Create a configuration for configuring the pipeline with a non default profile
    rs2::config cfg;

    //Add desired streams to configuration
    cfg.enable_stream(RS2_STREAM_COLOR, 640, 480, RS2_FORMAT_BGR8, 30);
    //cfg.enable_stream(RS2_STREAM_INFRARED, 640, 480, RS2_FORMAT_Y8, 30);
    cfg.enable_stream(RS2_STREAM_DEPTH, 640, 480, RS2_FORMAT_Z16, 30);

    //Instruct pipeline to start streaming with the requested configuration
   // pipe.start(cfg);
//    const auto color_win = "color Image";
//    namedWindow(color_win, WINDOW_AUTOSIZE);
//    const auto depth_win = "depth Image";
//    namedWindow(depth_win, WINDOW_AUTOSIZE);
    rs2::pipeline_profile profile = pipe.start(cfg);
    float depth_scale = get_depth_scale(profile.get_device());
    rs2_stream align_to = RS2_STREAM_COLOR;

    rs2::align align(align_to);
    float depth_clipping_distance = 1.f;

    while (1)
    {


        //Wait for all configured streams to produce a frame
        rs2::frameset frames = pipe.wait_for_frames();


        if (profile_changed(pipe.get_active_profile().get_streams(), profile.get_streams()))
        {
            //If the profile was changed, update the align object, and also get the new device's depth scale
            //如果profile发生改变,则更新align对象,重新获取深度图像像素到长度单位的转换比例
            profile = pipe.get_active_profile();
            align = rs2::align(align_to);
            depth_scale = get_depth_scale(profile.get_device());
        }

        //Get processed aligned frame
        //获取对齐后的帧
        auto processed = align.process(frames);

        // Trying to get both other and aligned depth frames
        //尝试获取对齐后的深度图像帧和其他帧
        rs2::frame aligned_color_frame = processed.get_color_frame();//processed.first(align_to);
//        rs2::frame aligned_depth_frame = processed.get_depth_frame().apply_filter(color_map);;
//        rs2::frame depth_frame=frames.get_depth_frame().apply_filter(color_map);

        rs2::frame aligned_depth_frame = processed.get_depth_frame();
        rs2::frame depth_frame=frames.get_depth_frame();

        // Creating OpenCV Matrix from a color image
        Mat color(Size(640, 480), CV_8UC3, (void*)aligned_color_frame.get_data(), Mat::AUTO_STEP);
        //Mat ir(Size(640, 480), CV_8UC1, (void*)ir_frame.get_data(), Mat::AUTO_STEP);
//        Mat aligned_depth(Size(640, 480), CV_8UC3, (void*)aligned_depth_frame.get_data(), Mat::AUTO_STEP);
//        Mat depth(Size(640, 480), CV_8UC3, (void*)depth_frame.get_data(), Mat::AUTO_STEP);

        Mat aligned_depth(Size(640, 480), CV_16UC1, (void*)aligned_depth_frame.get_data(), Mat::AUTO_STEP);
        Mat depth(Size(640, 480), CV_16UC1, (void*)depth_frame.get_data(), Mat::AUTO_STEP);


        // Display in a GUI
        imshow("depth", depth);
        imshow("aligned_depth", aligned_depth);
        //imshow("Display ir", ir);
        imshow("color",color );
        waitKey(100);


    }
    return 0;
}

float get_depth_scale(rs2::device dev)
{
    // Go over the device's sensors
    for (rs2::sensor& sensor : dev.query_sensors())
    {
        // Check if the sensor if a depth sensor
        if (rs2::depth_sensor dpt = sensor.as<rs2::depth_sensor>())
        {
            return dpt.get_depth_scale();
        }
    }
    throw std::runtime_error("Device does not have a depth sensor");
}

bool profile_changed(const std::vector<rs2::stream_profile>& current, const std::vector<rs2::stream_profile>& prev)
{
    for (auto&& sp : prev)
    {
        //If previous profile is in current (maybe just added another)
        auto itr = std::find_if(std::begin(current), std::end(current), [&sp](const rs2::stream_profile& current_sp) { return sp.unique_id() == current_sp.unique_id(); });
        if (itr == std::end(current)) //If it previous stream wasn't found in current
        {
            return true;
        }
    }
    return false;
}

参考自:
https://blog.csdn.net/dieju8330/article/details/85272919
https://github.com/IntelRealSense/librealsense/blob/master/examples/align-advanced/rs-align-advanced.cpp

上一篇:RealSense D435i 在ubuntu上运行ORB-SLAM2


下一篇:【Python入门教程】第14篇 逻辑运算符