realsense d435i 詳解

一、介紹

realsense d435i是一款深度相機,由英特爾公司開發。它通過採用一組紅外發射器和攝像頭,來實現深度圖像的捕獲。它不僅可以捕獲彩色圖像,還可以捕獲深度圖像和紅外圖像,可以應用於機械人、工業自動化、虛擬現實等多個領域。

二、硬件描述

realsense d435i由多個模塊組成,包括RGB相機、紅外發射器、紅外接收器、深度傳感器和IMU傳感器。RGB相機用於捕獲彩色圖像,紅外發射器和紅外接收器用於計算深度數據,深度傳感器用於捕獲深度圖像,而IMU傳感器用於捕獲運動信息。

三、realsense SDK簡介

realsense SDK是一款開源軟件開發工具包,是由英特爾公司開發的。它可以方便地與realsense d435i相機進行交互,並進行深度信息的採集和處理。它支持多個編程語言,如C++、C#、Python等。

以下是一個使用C++語言編寫的簡單程序,用於捕獲realsense d435i相機的深度數據:

#include 
#include 

int main()
{
    rs2::pipeline pipe;
    rs2::config cfg;
    cfg.enable_stream(RS2_STREAM_DEPTH);
    pipe.start(cfg);

    while (true)
    {
        rs2::frameset frames = pipe.wait_for_frames();
        rs2::depth_frame depth = frames.get_depth_frame();

        std::cout << "Depth at (320, 240): " << depth.get_distance(320, 240) << std::endl;
    }

    return 0;
}

四、realsense與ROS結合

ROS(Robot Operating System)是一個用於機械人應用程序開發的開源框架,是由加州大學聖地亞哥分校開發的。ROS可以與realsense d435i相機結合起來,用於機械人的導航、建圖和路徑規劃等多個應用領域。

以下是一個使用ROS和realsense d435i相機實現體積估計的案例:

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 

class VolumeEstimation
{
public:
    VolumeEstimation(ros::NodeHandle& nh, ros::NodeHandle& pnh)
    {
        cloud_.reset(new pcl::PointCloud);
        cloud_filtered_.reset(new pcl::PointCloud);
        cloud_smoothed_.reset(new pcl::PointCloud);
        cloud_rotated_.reset(new pcl::PointCloud);
        cloud_transformed_.reset(new pcl::PointCloud);
        registered_.reset(new pcl::PointCloud);
        ndt_.reset(new pcl::NormalDistributionsTransform);
        ndt_->setTransformationEpsilon(0.01);
        ndt_->setStepSize(0.1);
        ndt_->setResolution(1.0);
        ndt_->setMaximumIterations(35);

        depth_sub_ = nh.subscribe("/depth/image_raw", 1, &VolumeEstimation::depthCallback, this);
        pcl_pub_ = nh.advertise("/pcl_cloud", 1);
        voxel_pub_ = nh.advertise("/pcl_cloud_voxel", 1);
        smoothed_pub_ = nh.advertise("/pcl_cloud_smoothed", 1);
        rotated_pub_ = nh.advertise("/pcl_cloud_rotated", 1);
        transformed_pub_ = nh.advertise("/pcl_cloud_transformed", 1);
        registered_pub_ = nh.advertise("/pcl_cloud_registered", 1);
    }

    ~VolumeEstimation() {}

    void depthCallback(const sensor_msgs::ImageConstPtr& msg)
    {
        try
        {
            cv::Mat depth = cv_bridge::toCvShare(msg, "16UC1")->image;

            cloud_->height = depth.rows;
            cloud_->width = depth.cols;
            cloud_->is_dense = false;
            cloud_->points.resize(cloud_->height * cloud_->width);

            int index = 0;
            for (int i = 0; i < depth.rows; ++i)
            {
                for (int j = 0; j points[index];
                    point.x = (j - cx_) * depth.at(i,j) / fx_;
                    point.y = (i - cy_) * depth.at(i,j) / fy_;
                    point.z = depth.at(i,j) * 0.001;
                    index++;
                }
            }

            pcl_conversions::toPCL(ros::Time::now(), cloud_->header.stamp);
            cloud_->header.frame_id = "realsense_frame";
            pcl_pub_.publish(cloud_);

            passthroughFilter(cloud_);
            voxelGridFilter(cloud_filtered_);
            progressiveMorphFilter(cloud_filtered_, cloud_smoothed_);
            transformPointCloud(cloud_smoothed_, cloud_transformed_);
            ndtRegistration(cloud_transformed_, registered_);

            sensor_msgs::PointCloud2 pcl_cloud;
            pcl::toROSMsg(*cloud_, pcl_cloud);
            voxel_pub_.publish(pcl_cloud);

            pcl::toROSMsg(*cloud_smoothed_, pcl_cloud);
            smoothed_pub_.publish(pcl_cloud);

            pcl::toROSMsg(*cloud_rotated_, pcl_cloud);
            rotated_pub_.publish(pcl_cloud);

            pcl::toROSMsg(*cloud_transformed_, pcl_cloud);
            transformed_pub_.publish(pcl_cloud);

            pcl::toROSMsg(*registered_, pcl_cloud);
            registered_pub_.publish(pcl_cloud);
        }
        catch (cv_bridge::Exception& e)
        {
            ROS_ERROR("cv_bridge exception: %s", e.what());
        }
    }

    void passthroughFilter(const pcl::PointCloud::Ptr& cloud_in)
    {
        pcl::PassThrough filter;
        filter.setInputCloud(cloud_in);
        filter.setFilterFieldName("z");
        filter.setFilterLimits(0.01, 1.0);
        filter.filter(*cloud_filtered_);

        cloud_filtered_->header.frame_id = "realsense_frame";
    }

    void voxelGridFilter(const pcl::PointCloud::Ptr& cloud_in)
    {
        pcl::VoxelGrid filter;
        filter.setInputCloud(cloud_in);
        filter.setLeafSize(0.025, 0.025, 0.025);
        filter.filter(*cloud_filtered_);

        cloud_filtered_->header.frame_id = "realsense_frame";
    }

    void progressiveMorphFilter(const pcl::PointCloud::Ptr& cloud_in, pcl::PointCloud::Ptr& cloud_out)
    {
        pcl::ProgressiveMorphologicalFilter filter;
        filter.setInputCloud(cloud_in);
        filter.setMaxWindowSize(20);
        filter.setSlope(0.5f);
        filter.setInitialDistance(0.5f);
        filter.setThreshold(0.1f);
        filter.setErosionIterations(2);
        filter.setDilationIterations(2);
        filter.setNumberOfThreads(4);
        filter.filter(*cloud_out);

        cloud_out->header.frame_id = "realsense_frame";
    }

    void transformPointCloud(const pcl::PointCloud::Ptr& cloud_in, pcl::PointCloud::Ptr& cloud_out)
    {
        pcl::PointCloud::Ptr cloud_rotated(new pcl::PointCloud);
        Eigen::Matrix4f transform = Eigen::Matrix4f::Identity();
        float theta = -M_PI / 2.0;
        transform(0,0) = std::cos(theta);
        transform(0,1) = -std::sin(theta);
        transform(1,0) = std::sin(theta);
        transform(1,1) = std::cos(theta);
        pcl::transformPointCloud(*cloud_in, *cloud_rotated, transform);

        pcl::PassThrough filter;
        filter.setInputCloud(cloud_rotated);
        filter.setFilterFieldName("z");
        filter.setFilterLimits(0.01, 1.5);
        filter.filter(*cloud_out);

        cloud_out->header.frame_id = "realsense_frame";
    }

    void ndtRegistration(const pcl::PointCloud::Ptr& cloud_in, pcl::PointCloud::Ptr& cloud_out)
    {
        pcl::PointCloud::Ptr cloud_init(new pcl::PointCloud);
        pcl::VoxelGrid voxel;
        voxel.setLeafSize(0.05, 0.05, 0.05);
        voxel.setInputCloud(cloud_in);
        voxel.filter(*cloud_init);

        ndt_->setInputSource(cloud_init);
        ndt_->setInputTarget(cloud_out);

        pcl::PointCloud::Ptr output_cloud(new pcl::PointCloud);
        ndt_->align(*output_cloud);

        Eigen::Matrix4f transform = ndt_->getFinalTransformation();

        pcl::transformPointCloud(*cloud_init, *cloud_out, transform);

        cloud_out->header.frame_id = "realsense_frame";
    }

private:
    ros::Subscriber depth_sub_;
    ros::Publisher pcl_pub_;
    ros::Publisher voxel_pub_;
    ros::Publisher smoothed_pub_;
    ros::Publisher rotated_pub_;
    ros::Publisher transformed_pub_;
    ros::Publisher registered_pub_;

    pcl::PointCloud::Ptr cloud_;
    pcl::PointCloud::Ptr cloud_filtered_;
    pcl::PointCloud::Ptr cloud_smoothed_;
    pcl::PointCloud::Ptr cloud_rotated_;
    pcl::PointCloud::Ptr cloud_transformed_;
    pcl::PointCloud::Ptr registered_;

    std::unique_ptr<pcl::NormalDistributionsTransform> ndt_;

    float fx_ = 385.255, fy_ = 385.255, cx_ = 320.5, cy_ = 240.5;
};

int main(int argc, char* argv[])
{
    ros::init(argc, argv, "volume_estimation");
    ros::NodeHandle nh, pnh("~");

    VolumeEstimation estimation(nh, pnh);

    ros::spin();

    return 0;
}

五、結論

realsense d435i是一款高質量的深度相機,可以應用於機械人、工業自動化、虛擬現實等多個領域。通過使用realsense SDK和ROS,我們可以方便地與realsense d435i相機進行交互,並進行深度信息的採集和處理。

原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-hk/n/190384.html

(0)
打賞 微信掃一掃 微信掃一掃 支付寶掃一掃 支付寶掃一掃
小藍的頭像小藍
上一篇 2024-11-29 22:33
下一篇 2024-11-29 22:33

相關推薦

  • Linux sync詳解

    一、sync概述 sync是Linux中一個非常重要的命令,它可以將文件系統緩存中的內容,強制寫入磁盤中。在執行sync之前,所有的文件系統更新將不會立即寫入磁盤,而是先緩存在內存…

    編程 2025-04-25
  • 神經網絡代碼詳解

    神經網絡作為一種人工智能技術,被廣泛應用於語音識別、圖像識別、自然語言處理等領域。而神經網絡的模型編寫,離不開代碼。本文將從多個方面詳細闡述神經網絡模型編寫的代碼技術。 一、神經網…

    編程 2025-04-25
  • nginx與apache應用開發詳解

    一、概述 nginx和apache都是常見的web服務器。nginx是一個高性能的反向代理web服務器,將負載均衡和緩存集成在了一起,可以動靜分離。apache是一個可擴展的web…

    編程 2025-04-25
  • MPU6050工作原理詳解

    一、什麼是MPU6050 MPU6050是一種六軸慣性傳感器,能夠同時測量加速度和角速度。它由三個傳感器組成:一個三軸加速度計和一個三軸陀螺儀。這個組合提供了非常精細的姿態解算,其…

    編程 2025-04-25
  • Python安裝OS庫詳解

    一、OS簡介 OS庫是Python標準庫的一部分,它提供了跨平台的操作系統功能,使得Python可以進行文件操作、進程管理、環境變量讀取等系統級操作。 OS庫中包含了大量的文件和目…

    編程 2025-04-25
  • Python輸入輸出詳解

    一、文件讀寫 Python中文件的讀寫操作是必不可少的基本技能之一。讀寫文件分別使用open()函數中的’r’和’w’參數,讀取文件…

    編程 2025-04-25
  • git config user.name的詳解

    一、為什麼要使用git config user.name? git是一個非常流行的分佈式版本控制系統,很多程序員都會用到它。在使用git commit提交代碼時,需要記錄commi…

    編程 2025-04-25
  • Java BigDecimal 精度詳解

    一、基礎概念 Java BigDecimal 是一個用於高精度計算的類。普通的 double 或 float 類型只能精確表示有限的數字,而對於需要高精度計算的場景,BigDeci…

    編程 2025-04-25
  • Linux修改文件名命令詳解

    在Linux系統中,修改文件名是一個很常見的操作。Linux提供了多種方式來修改文件名,這篇文章將介紹Linux修改文件名的詳細操作。 一、mv命令 mv命令是Linux下的常用命…

    編程 2025-04-25
  • 詳解eclipse設置

    一、安裝與基礎設置 1、下載eclipse並進行安裝。 2、打開eclipse,選擇對應的工作空間路徑。 File -> Switch Workspace -> [選擇…

    編程 2025-04-25

發表回復

登錄後才能評論