PCL点云库学习笔记(6):点云超体素化(VCCS)

本文介绍了一种基于PCL库实现的点云数据预处理和超体素聚类算法,包括点云去噪、超体素构建及可视化过程。通过设定不同的参数,如体素分辨率、种子分辨率、颜色、空间和法向量的重要性,可以调整聚类效果。最终展示了如何将聚类结果可视化,包括体素质心和邻接体素间的连接。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

初学者笔记:

点云数据链接(不会上传,所以用了百度云):
链接:https://pan.baidu.com/s/1VTVxn3BntbAr9tGHv6L-HA
提取码:u81q

#include <pcl/console/parse.h>
#include <pcl/point_cloud.h>
#include <pcl/point_types.h>
#include <pcl/io/pcd_io.h>
#include <pcl/visualization/pcl_visualizer.h>
#include <pcl/segmentation/supervoxel_clustering.h>
#include <pcl/filters/statistical_outlier_removal.h>
#include<iostream>
//VTK include needed for drawing graph lines
#include <vtkPolyLine.h>

using namespace pcl;
using namespace std;

typedef PointXYZ PointT;
typedef PointXYZL PointTL;
//邻接线条可视化****************************************************************
void addSupervoxelConnectionsToViewer(PointXYZRGBA &supervoxel_center,
	PointCloud<PointXYZRGBA> &adjacent_supervoxel_centers,
	std::string supervoxel_name,
	pcl::visualization::PCLVisualizer::Ptr & viewer)
{
	vtkSmartPointer<vtkPoints> points = vtkSmartPointer<vtkPoints>::New();
	vtkSmartPointer<vtkCellArray> cells = vtkSmartPointer<vtkCellArray>::New();
	vtkSmartPointer<vtkPolyLine> polyLine = vtkSmartPointer<vtkPolyLine>::New();

	//Iterate through all adjacent points, and add a center point to adjacent point pair
	for (auto adjacent_itr = adjacent_supervoxel_centers.begin(); adjacent_itr != adjacent_supervoxel_centers.end(); ++adjacent_itr)
	{
		points->InsertNextPoint(supervoxel_center.data);
		points->InsertNextPoint(adjacent_itr->data);
	}
	// Create a polydata to store everything in
	vtkSmartPointer<vtkPolyData> polyData = vtkSmartPointer<vtkPolyData>::New();
	// Add the points to the dataset
	polyData->SetPoints(points);
	polyLine->GetPointIds()->SetNumberOfIds(points->GetNumberOfPoints());
	for (unsigned int i = 0; i < points->GetNumberOfPoints(); i++)
		polyLine->GetPointIds()->SetId(i, i);
	cells->InsertNextCell(polyLine);
	// Add the lines to the dataset
	polyData->SetLines(cells);
	viewer->addModelFromPolyData(polyData, supervoxel_name);
}
//主程序**************************************************************************
int main(){
	//点云读取****************************************************
	PointCloud<PointT>::Ptr cloud(new PointCloud<PointT>);
	io::loadPCDFile("C:\\Users\\admin\\Desktop\\desk.pcd", *cloud);
	//去噪声*******************************************************
	StatisticalOutlierRemoval<PointT>sor;
	PointCloud<PointT>::Ptr sor_cloud(new PointCloud<PointT>);
	sor.setInputCloud(cloud);
	sor.setMeanK(10);
	sor.setStddevMulThresh(1);
	sor.filter(*sor_cloud);
	//构建超体素**************************************************
	float voxel_resultion = 0.01f;
	float seed_resultion = 0.1f;
	float color_importance = 0.0f;
	float spatial_importance = 0.4f;
	float normal_importance = 5.0f;

	SupervoxelClustering<PointT> super(voxel_resultion,seed_resultion);
	super.setInputCloud(sor_cloud);
	super.setNormalImportance(normal_importance);
	super.setColorImportance(color_importance);
	super.setSpatialImportance(spatial_importance);
	std::map<uint32_t,Supervoxel<PointT>::Ptr >supervoxl_clustering;
	super.extract(supervoxl_clustering);

	cout << "supervoxel number is"<<supervoxl_clustering.size() << endl;

	visualization::PCLVisualizer::Ptr viewer(new visualization::PCLVisualizer("VCCS"));
	PointCloud<PointT>::Ptr supervoxel_centroid_cloud = super.getVoxelCentroidCloud();

	viewer->addPointCloud(supervoxel_centroid_cloud, "supervoxel_centroid_cloud");
	viewer->setPointCloudRenderingProperties(visualization::PCL_VISUALIZER_POINT_SIZE, 1, "supervoxel_centroid_cloud");
	viewer->setPointCloudRenderingProperties(visualization::PCL_VISUALIZER_OPACITY, 0.5, "supervoxel_centroid_cloud");


	PointCloud<PointTL>::Ptr supervoxel_cloud = super.getLabeledVoxelCloud();
	viewer->addPointCloud(supervoxel_cloud, "supervoxel_cloud");
	viewer->setPointCloudRenderingProperties(visualization::PCL_VISUALIZER_POINT_SIZE, 5, "supervoxel_cloud");
	viewer->setPointCloudRenderingProperties(visualization::PCL_VISUALIZER_OPACITY, 0.5, "supervoxel_cloud");
	//可视化法向量
	//PointCloud<PointNormal>::Ptr supervoxel_normal=super.makeSupervoxelNormalCloud(supervoxl_clustering);
	//viewer->addPointCloudNormals<PointNormal>(supervoxel_normal, 1, 0.2, "123");

	multimap<uint32_t, uint32_t>SupervoxelAdjacency;
	super.getSupervoxelAdjacency(SupervoxelAdjacency);
	//获得体素点云的邻接单元
	for (auto label_itr = SupervoxelAdjacency.cbegin(); label_itr != SupervoxelAdjacency.cend();)
	{
		uint32_t super_label = label_itr->first;//获取体素单元的标签
		Supervoxel<PointT>::Ptr super_cloud = supervoxl_clustering.at(super_label);//把对应标签内的点云、体素质心、以及质心对应的法向量提取出来
		PointCloud<PointXYZRGBA> adjacent_supervoxel_centers;
		for (auto adjacent_itr = SupervoxelAdjacency.equal_range(super_label).first; adjacent_itr != SupervoxelAdjacency.equal_range(super_label).second; ++adjacent_itr){
			Supervoxel<PointT>::Ptr neighbor_supervoxel = supervoxl_clustering.at(adjacent_itr->second);
			adjacent_supervoxel_centers.push_back(neighbor_supervoxel->centroid_);
		}
		std::stringstream ss;
		ss << "supervoxel_" << super_label;
		addSupervoxelConnectionsToViewer(super_cloud->centroid_, adjacent_supervoxel_centers, ss.str(), viewer);
		label_itr = SupervoxelAdjacency.upper_bound(super_label);
	}
	viewer->spin();
	return 0;
}

代码运行结果:
在这里插入图片描述

评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值