3.PCL点云数据的分割

        点云分割常见算法有欧式聚类、欧式聚类的变种、超体素分割算法和其变种、GrabCut GrabCut算法、随机行走分割算法、随机抽样算法、区域生长算法等。下面分别介绍几种常见的分割算法。

1.RANSAC算法

代码如下:

#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/visualization/pcl_visualizer.h>
int
main()
{
    pcl::PointCloud<pcl::PointXYZ>::Ptr cloud(new pcl::PointCloud<pcl::PointXYZ>);
    pcl::PointCloud<pcl::PointXYZ>::Ptr inliers_points(new pcl::PointCloud<pcl::PointXYZ>);

    // Fill in the cloud data
    cloud->width = 50;
    cloud->height = 1;
    cloud->points.resize(cloud->width * cloud->height);

    // Generate the data
    for (auto& point : *cloud)
    {
        point.x = rand() / (RAND_MAX + 1.0f);
        point.y = rand() / (RAND_MAX + 1.0f);
        point.z = 1.0;
    }

    // Set a few outliers
    (*cloud)[0].z = 2.0;
    (*cloud)[3].z = -2.0;
    (*cloud)[6].z = 4.0;


    for(int i=10;i<40;i++)
        (*cloud)[i].z = 1.2;

    std::cerr << "Point cloud data: " << cloud->size() << " points" << std::endl;
    for (const auto& point : *cloud)
        std::cerr << "    " << point.x << " "
        << point.y << " "
        << point.z << std::endl;
    // 创建模型系数
    pcl::ModelCoefficients::Ptr coefficients(new pcl::ModelCoefficients);
    pcl::PointIndices::Ptr inliers(new pcl::PointIndices); // 创建索引
    // Create the segmentation object
    pcl::SACSegmentation<pcl::PointXYZ> seg; // 创建分割对象并设置参数
    // Optional
    seg.setOptimizeCoefficients(true);
    // Mandatory
    seg.setModelType(pcl::SACMODEL_PLANE);
    seg.setMethodType(pcl::SAC_RANSAC);
    seg.setDistanceThreshold(0.01);

    seg.setInputCloud(cloud);
    seg.segment(*inliers, *coefficients); //获取点云索引和对应的系数

    if (inliers->indices.size() == 0)
    {
        PCL_ERROR("Could not estimate a planar model for the given dataset.\n");
        return (-1);
    }

    std::cerr << "Model coefficients: " << coefficients->values[0] << " "
        << coefficients->values[1] << " "
        << coefficients->values[2] << " "
        << coefficients->values[3] << std::endl;

    std::cerr << "Model inliers: " << inliers->indices.size() << std::endl;
    for (const auto& idx : inliers->indices)
    {
        std::cerr << idx << "    " << cloud->points[idx].x << " "
            << cloud->points[idx].y << " "
            << cloud->points[idx].z << std::endl;
        auto& point = cloud->points[idx];
        inliers_points->emplace_back(point);
    }

    // 创建 PCL 可视化对象
    pcl::visualization::PCLVisualizer viewer("Point Cloud Viewer");
    // 设置显示点云的颜色为红色
    pcl::visualization::PointCloudColorHandlerCustom<pcl::PointXYZ> color1(cloud, 255, 0, 0);
    pcl::visualization::PointCloudColorHandlerCustom<pcl::PointXYZ> color2(inliers_points, 0, 255, 0);

    // 添加点云数据到可视化对象并设置颜色
    viewer.addPointCloud<pcl::PointXYZ>(cloud, color1, "cloud");
    viewer.addPointCloud<pcl::PointXYZ>(inliers_points, color2, "inliers_points");

    // 设置点云数据的显示参数
    viewer.setPointCloudRenderingProperties(pcl::visualization::PCL_VISUALIZER_POINT_SIZE, 5, "cloud");
    viewer.setPointCloudRenderingProperties(pcl::visualization::PCL_VISUALIZER_POINT_SIZE, 5, "inliers_points");
    // 添加坐标轴到可视化对象
    viewer.addCoordinateSystem(2.0); // 默认1.0为坐标轴的尺寸
    // 显示点云数据
    while (!viewer.wasStopped())
    {
        viewer.spinOnce();
    }

    return (0);
}

2.平面分割

代码如下:

#include <pcl/ModelCoefficients.h>
#include <pcl/point_types.h>
#include <pcl/io/pcd_io.h>
#include <pcl/filters/extract_indices.h>
#include <pcl/filters/voxel_grid.h>
#include <pcl/features/normal_3d.h>
#include <pcl/kdtree/kdtree.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_clusters.h>
//图像可视化
#include <pcl/visualization/cloud_viewer.h>
#include <pcl/visualization/pcl_visualizer.h>
#include <boost/thread/thread.hpp>

using namespace std;
using namespace pcl;



int main(int argc, char** argv) {

	pcl::PCDReader reader;
	pcl::PointCloud<pcl::PointXYZ>::Ptr cloud(new pcl::PointCloud<pcl::PointXYZ>), cloud_f(new pcl::PointCloud<pcl::PointXYZ>);
	reader.read("E:\\vs\\daima\\PCLpratict\\Projects\\isprs\\isprs\\1_1.pcd", *cloud);
	std::cout << "PointCloud before filtering has: " << cloud->points.size() << " data points." << std::endl; //*

	// 使用1cm体素进行下采样
	pcl::VoxelGrid<pcl::PointXYZ> vg;
	pcl::PointCloud<pcl::PointXYZ>::Ptr cloud_filtered(new pcl::PointCloud<pcl::PointXYZ>);
	vg.setInputCloud(cloud);
	vg.setLeafSize(0.5f, 0.5f, 0.5f);
	vg.filter(*cloud_filtered);
	std::cout << "PointCloud after filtering has: " << cloud_filtered->points.size() << " data points." << std::endl; //*

	// 进行平面分割
	pcl::SACSegmentation<pcl::PointXYZ> seg;
	pcl::PointIndices::Ptr inliers(new pcl::PointIndices);//平面点云索引信息
	pcl::ModelCoefficients::Ptr coefficients(new pcl::ModelCoefficients);//平面方程的系数
	pcl::PointCloud<pcl::PointXYZ>::Ptr cloud_plane(new pcl::PointCloud<pcl::PointXYZ>());//平面点云
	pcl::PCDWriter writer;
	seg.setOptimizeCoefficients(true);
	seg.setModelType(pcl::SACMODEL_PLANE);
	seg.setMethodType(pcl::SAC_RANSAC);
	seg.setMaxIterations(100);
	seg.setDistanceThreshold(5.0);
	int i = 0, nr_points = (int)cloud_filtered->points.size();
	while (cloud_filtered->points.size() > 0.3 * nr_points)
	{
		// Segment the largest planar component from the remaining cloud
		seg.setInputCloud(cloud_filtered);
		seg.segment(*inliers, *coefficients);
		if (inliers->indices.size() == 0)
		{
			std::cout << "Could not estimate a planar model for the given dataset." << std::endl;
			break;
		}

		// 通过索引提取平面点云
		pcl::ExtractIndices<pcl::PointXYZ> extract;
		extract.setInputCloud(cloud_filtered);
		extract.setIndices(inliers);
		extract.setNegative(false);

		// Get the points associated with the planar surface
		extract.filter(*cloud_plane);
		std::cout << "PointCloud representing the planar component: " << cloud_plane->points.size() << " data points." << std::endl;

		// 移去平面局内点,提取剩余点云
		extract.setNegative(true);
		extract.filter(*cloud_f);
		*cloud_filtered = *cloud_f;
	}

	//平面分割图像可视化
	boost::shared_ptr<visualization::PCLVisualizer> viewer(new
		visualization::PCLVisualizer("3D Viewer"));//页面名称是3D Viewer
	viewer->setBackgroundColor(0, 0, 0);//页面背景是黑色
	viewer->addPointCloud<PointXYZ>(cloud_filtered, "sample cloud");
	while (!viewer->wasStopped())
	{
		viewer->spinOnce(100);
		boost::this_thread::sleep(boost::posix_time::microseconds(100000));
	}



	return 0;
}

3.欧式聚类算法

代码如下:

#include <pcl/ModelCoefficients.h>
#include <pcl/point_types.h>
#include <pcl/io/pcd_io.h>
#include <pcl/filters/extract_indices.h>
#include <pcl/filters/voxel_grid.h>
#include <pcl/features/normal_3d.h>
#include <pcl/kdtree/kdtree.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_clusters.h>


int main(int argc, char** argv)
{
	// Read in the cloud data
	pcl::PCDReader reader;
	pcl::PointCloud<pcl::PointXYZ>::Ptr cloud(new pcl::PointCloud<pcl::PointXYZ>), cloud_f(new pcl::PointCloud<pcl::PointXYZ>);
	reader.read("E:\\vs\\daima\\PCLpratict\\Projects\\isprs\\isprs\\1_1.pcd", *cloud);
	std::cout << "PointCloud before filtering has: " << cloud->points.size() << " data points." << std::endl; //*

	// 使用1cm体素进行下采样
	pcl::VoxelGrid<pcl::PointXYZ> vg;
	pcl::PointCloud<pcl::PointXYZ>::Ptr cloud_filtered(new pcl::PointCloud<pcl::PointXYZ>);
	vg.setInputCloud(cloud);
	vg.setLeafSize(0.5f, 0.5f, 0.5f);
	vg.filter(*cloud_filtered);
	std::cout << "PointCloud after filtering has: " << cloud_filtered->points.size() << " data points." << std::endl; //*

	// 进行平面分割
	pcl::SACSegmentation<pcl::PointXYZ> seg;
	pcl::PointIndices::Ptr inliers(new pcl::PointIndices);//平面点云索引信息
	pcl::ModelCoefficients::Ptr coefficients(new pcl::ModelCoefficients);//平面方程的系数
	pcl::PointCloud<pcl::PointXYZ>::Ptr cloud_plane(new pcl::PointCloud<pcl::PointXYZ>());//平面点云
	pcl::PCDWriter writer;
	seg.setOptimizeCoefficients(true);
	seg.setModelType(pcl::SACMODEL_PLANE);
	seg.setMethodType(pcl::SAC_RANSAC);
	seg.setMaxIterations(100);
	seg.setDistanceThreshold(0.02);

	int i = 0, nr_points = (int)cloud_filtered->points.size();
	while (cloud_filtered->points.size() > 0.3 * nr_points)
	{
		// Segment the largest planar component from the remaining cloud
		seg.setInputCloud(cloud_filtered);
		seg.segment(*inliers, *coefficients);
		if (inliers->indices.size() == 0)
		{
			std::cout << "Could not estimate a planar model for the given dataset." << std::endl;
			break;
		}

		// 通过索引提取平面点云
		pcl::ExtractIndices<pcl::PointXYZ> extract;
		extract.setInputCloud(cloud_filtered);
		extract.setIndices(inliers);
		extract.setNegative(false);

		// Get the points associated with the planar surface
		extract.filter(*cloud_plane);
		std::cout << "PointCloud representing the planar component: " << cloud_plane->points.size() << " data points." << std::endl;

		// 移去平面局内点,提取剩余点云
		extract.setNegative(true);
		extract.filter(*cloud_f);
		*cloud_filtered = *cloud_f;
	}

	// 创建kd数对象
	pcl::search::KdTree<pcl::PointXYZ>::Ptr tree(new pcl::search::KdTree<pcl::PointXYZ>);
	tree->setInputCloud(cloud_filtered);

	std::vector<pcl::PointIndices> cluster_indices;//cluster_indices是一个向量,保存每个检测到的聚类,cluster_indices[0]即为第一个聚类包含的点集的所有索引
	pcl::EuclideanClusterExtraction<pcl::PointXYZ> ec;//创建欧式聚类对象
	ec.setClusterTolerance(0.02); // 近邻搜索的搜索半径为2cm
	ec.setMinClusterSize(100);//一个聚类需要的最少点数目为100
	ec.setMaxClusterSize(25000);//一个聚类需要的最大点数目为25000
	ec.setSearchMethod(tree);
	ec.setInputCloud(cloud_filtered);
	ec.extract(cluster_indices);

	int j = 0;
	for (std::vector<pcl::PointIndices>::const_iterator it = cluster_indices.begin(); it != cluster_indices.end(); ++it)//循环访问每个索引,把每个聚类的点分别写入到点云中
	{
		pcl::PointCloud<pcl::PointXYZ>::Ptr cloud_cluster(new pcl::PointCloud<pcl::PointXYZ>);
		for (std::vector<int>::const_iterator pit = it->indices.begin(); pit != it->indices.end(); ++pit)//it代表聚类索引,pit代表聚类里每个点的索引
			cloud_cluster->points.push_back(cloud_filtered->points[*pit]); //*
		cloud_cluster->width = cloud_cluster->points.size();
		cloud_cluster->height = 1;
		cloud_cluster->is_dense = true;

		std::cout << "PointCloud representing the Cluster: " << cloud_cluster->points.size() << " data points." << std::endl;
		std::stringstream ss;
		ss << "cloud_cluster_" << j << ".pcd";
		writer.write<pcl::PointXYZ>(ss.str(), *cloud_cluster, false); //*
		j++;
	}

	return (0);
}

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
PCL(Point Cloud Library)是一个开源的点云处理库用于处理和分析三维点云数据。在点云分割任务中,评价指标用于衡量算法的性能和准确度。以下是几个常用的PCL点云分割评价指标: 1. 点云分割准确率(Segmentation Accuracy):该指标用于评估算法点云数据进行正确分割的能力。准确率可以通过计算正确分割的点数与总点数之比来得到。 2. 点云分割召回率(Segmentation Recall):该指标用于评估算法点云数据进行完整分割的能力。召回率可以通过计算正确分割的点数与真实分割点数之比来得到。 3. 平均欠分割误差(Under-segmentation Error):该指标用于评估算法点云数据进行过度分割的程度。欠分割误差可以通过计算未正确分割的点数与总点数之比来得到。 4. 平均过分割误差(Over-segmentation Error):该指标用于评估算法点云数据进行不足分割的程度。过分割误差可以通过计算错误分割的点数与总点数之比来得到。 5. 边界正确率(Boundary Precision):该指标用于评估算法点云数据中物体边界的准确度。边界正确率可以通过计算正确分割的边界点数与总边界点数之比来得到。 6. 边界召回率(Boundary Recall):该指标用于评估算法点云数据中物体边界的完整性。边界召回率可以通过计算正确分割的边界点数与真实边界点数之比来得到。 以上是一些常见的PCL点云分割评价指标,可以根据具体任务和需求选择适合的指标进行评估。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值