PCL 平面分割
程序员文章站
2022-03-16 18:05:04
...
#include <iostream>
#include <pcl/ModelCoefficients.h>
#include <pcl/io/pcd_io.h>
#include <pcl/point_types.h>
#include <pcl/sample_consensus/method_types.h>
#include <pcl/sample_consensus/model_types.h>
#include <pcl/segmentation/sac_segmentation.h>
#include <pcl/filters/voxel_grid.h>
#include <pcl/filters/extract_indices.h>
int
main (int argc, char** argv)
{
pcl::PointCloud<pcl::PointXYZI>::Ptr cloud(new pcl::PointCloud<pcl::PointXYZI>);
pcl::PointCloud<pcl::PointXYZI>::Ptr cloud_filtered(new pcl::PointCloud<pcl::PointXYZI>),cloud_p (new pcl::PointCloud<pcl::PointXYZI>), cloud_f (new pcl::PointCloud<pcl::PointXYZI>);
// 填入点云数据
pcl::io::loadPCDFile("table_scene_lms400.pcd", *cloud);
// 创建滤波器对象
pcl::VoxelGrid<pcl::PointXYZI> sor;//滤波处理对象
sor.setInputCloud(cloud);
sor.setLeafSize(0.01f, 0.01f, 0.01f);//设置滤波器处理时采用的体素大小的参数
sor.filter(*cloud_filtered);
std::cerr << "PointCloud after filtering: " << cloud_filtered->width * cloud_filtered->height << " data points." << std::endl;
// 将下采样后的数据存入磁盘
pcl::PCDWriter writer;
writer.write<pcl::PointXYZI> ("table_scene_lms400_downsampled.pcd", *cloud_filtered, false);
pcl::ModelCoefficients::Ptr coefficients (new pcl::ModelCoefficients ());
pcl::PointIndices::Ptr inliers (new pcl::PointIndices ());
// 创建分割对象
pcl::SACSegmentation<pcl::PointXYZI> seg;
// 可选
seg.setOptimizeCoefficients (true);
// 必选
seg.setModelType (pcl::SACMODEL_PLANE);
seg.setMethodType (pcl::SAC_RANSAC);
seg.setMaxIterations (1000);
seg.setDistanceThreshold (0.01);
// 创建滤波器对象
pcl::ExtractIndices<pcl::PointXYZI> extract;
int i = 0, nr_points = (int) cloud_filtered->points.size ();
// 当还有30%原始点云数据时
while (cloud_filtered->points.size () > 0.3 * nr_points)
{
// 从余下的点云中分割最大平面组成部分
seg.setInputCloud (cloud_filtered);
seg.segment (*inliers, *coefficients);
if (inliers->indices.size () == 0)
{
std::cerr << "Could not estimate a planar model for the given dataset." << std::endl;
break;
}
// 分离内层
extract.setInputCloud (cloud_filtered);
extract.setIndices (inliers);
extract.setNegative (false);
extract.filter (*cloud_p);
std::cerr << "PointCloud representing the planar component: " << cloud_p->width * cloud_p->height << " data points." << std::endl;
std::stringstream ss;
ss << "table_scene_lms400_plane_" << i << ".pcd";
writer.write<pcl::PointXYZI> (ss.str (), *cloud_p, false);
// 创建滤波器对象
extract.setNegative (true);
extract.filter (*cloud_f);
cloud_filtered.swap (cloud_f);
i++;
}
return (0);
}
上一篇: CSS3 动画
下一篇: 深蓝学院:视觉SLAM学习记录1
推荐阅读
-
react怎样实现页面代码分割和按需加载
-
某 SCOI 模拟赛 T3 画图(draw)(无根无标号平面树计数)
-
php分割数组示例
-
php字符串分割函数explode的例子
-
用labelme制作自己的语义分割数据集
-
目标检测和语义分割数据集制作工具labelimg、labelme
-
好奇怪啊,往记事本里添加数据,用t分割,结果每个tab键的长度不一致?不一致?
-
对象上下文语义分割:OCR论文笔记(Object-Contextual Representations for Semantic Segmentation )
-
php 字符串分割函数的总结
-
PCL:使用VoxelGrid filter对点云进行下采样