Pcl 点云多边缘 直线拟合 代码
时间: 2023-03-20 15:01:49 浏览: 196
以下是基于PCL(点云库)的多边形和直线拟合代码示例:
多边形拟合:
```
#include <iostream>
#include <pcl/io/pcd_io.h>
#include <pcl/point_types.h>
#include <pcl/filters/extract_indices.h>
#include <pcl/features/normal_3d.h>
#include <pcl/sample_consensus/method_types.h>
#include <pcl/sample_consensus/model_types.h>
#include <pcl/segmentation/sac_segmentation.h>
#include <pcl/segmentation/extract_polygonal_prism_data.h>
int main(int argc, char** argv)
{
// 加载点云数据
pcl::PointCloud<pcl::PointXYZ>::Ptr cloud(new pcl::PointCloud<pcl::PointXYZ>);
pcl::io::loadPCDFile<pcl::PointXYZ>("table_scene_lms400.pcd", *cloud);
// 创建法线估计对象
pcl::NormalEstimation<pcl::PointXYZ, pcl::Normal> ne;
pcl::PointCloud<pcl::Normal>::Ptr normals(new pcl::PointCloud<pcl::Normal>);
pcl::search::KdTree<pcl::PointXYZ>::Ptr tree(new pcl::search::KdTree<pcl::PointXYZ>);
tree->setInputCloud(cloud);
ne.setInputCloud(cloud);
ne.setSearchMethod(tree);
ne.setKSearch(50);
ne.compute(*normals);
// 多边形分割
pcl::PointCloud<pcl::PointXYZ>::Ptr cloud_plane(new pcl::PointCloud<pcl::PointXYZ>);
pcl::ModelCoefficients::Ptr coefficients_plane(new pcl::ModelCoefficients);
pcl::SACSegmentationFromNormals<pcl::PointXYZ, pcl::Normal> seg;
seg.setOptimizeCoefficients(true);
seg.setModelType(pcl::SACMODEL_NORMAL_PLANE);
seg.setNormalDistanceWeight(0.1);
seg.setMethodType(pcl::SAC_RANSAC);
seg.setMaxIterations(100);
seg.setDistanceThreshold(0.03);
seg.setInputCloud(cloud);
seg.setInputNormals(normals);
seg.segment(*inliers_plane, *coefficients_plane);
// 提取多边形数据
pcl::ExtractPolygonalPrismData<pcl::PointXYZ> ex;
pcl::PointIndices::Ptr inliers(new pcl::PointIndices);
ex.setHeightLimits(0.01, 1.0);
ex.setInputCloud(cloud);
ex.setInputPlanarHull(cloud_plane);
ex.segment(*inliers);
// 输出结果
std::cerr << "Number of inliers: " << inliers->indices.size() << std::endl;
for (int i = 0; i < inliers->indices.size(); ++i)
std::cerr << inliers->indices[i] << " " <<
cloud->points[inliers->indices[i]].x << " " <<
cloud->points[inliers->indices[i]].y << " " <<
cloud->points[inliers->indices[i]].z << std::endl;
return (0);
}
```
直线拟合:
```
#include <iostream>
#include <pcl/io/pcd_io.h>
#include <pcl/point_types.h>
#include <pcl/filters/extract_indices.h>
#include <pcl/features/normal_3d.h>
#include <pcl/sample_consensus/method_types.h>
#include <pcl/sample_consensus/model_types.h>
#include <pcl/segmentation/sac_segmentation.h>
int main(int argc, char** argv)
{
// 加载点云数据
pcl::PointCloud<pcl::PointXYZ>::Ptr cloud(new pcl::
阅读全文