一、介紹
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
微信掃一掃
支付寶掃一掃