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

初学者笔记:

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

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include
//VTK include needed for drawing graph lines
#include 

using namespace pcl;
using namespace std;

typedef PointXYZ PointT;
typedef PointXYZL PointTL;
//邻接线条可视化****************************************************************
void addSupervoxelConnectionsToViewer(PointXYZRGBA &supervoxel_center,
	PointCloud &adjacent_supervoxel_centers,
	std::string supervoxel_name,
	pcl::visualization::PCLVisualizer::Ptr & viewer)
{
	vtkSmartPointer points = vtkSmartPointer::New();
	vtkSmartPointer cells = vtkSmartPointer::New();
	vtkSmartPointer polyLine = vtkSmartPointer::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 polyData = vtkSmartPointer::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::Ptr cloud(new PointCloud);
	io::loadPCDFile("C:\\Users\\admin\\Desktop\\desk.pcd", *cloud);
	//去噪声*******************************************************
	StatisticalOutlierRemovalsor;
	PointCloud::Ptr sor_cloud(new PointCloud);
	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 super(voxel_resultion,seed_resultion);
	super.setInputCloud(sor_cloud);
	super.setNormalImportance(normal_importance);
	super.setColorImportance(color_importance);
	super.setSpatialImportance(spatial_importance);
	std::map::Ptr >supervoxl_clustering;
	super.extract(supervoxl_clustering);

	cout << "supervoxel number is"<::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::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::Ptr supervoxel_normal=super.makeSupervoxelNormalCloud(supervoxl_clustering);
	//viewer->addPointCloudNormals(supervoxel_normal, 1, 0.2, "123");

	multimapSupervoxelAdjacency;
	super.getSupervoxelAdjacency(SupervoxelAdjacency);
	//获得体素点云的邻接单元
	for (auto label_itr = SupervoxelAdjacency.cbegin(); label_itr != SupervoxelAdjacency.cend();)
	{
		uint32_t super_label = label_itr->first;//获取体素单元的标签
		Supervoxel::Ptr super_cloud = supervoxl_clustering.at(super_label);//把对应标签内的点云、体素质心、以及质心对应的法向量提取出来
		PointCloud adjacent_supervoxel_centers;
		for (auto adjacent_itr = SupervoxelAdjacency.equal_range(super_label).first; adjacent_itr != SupervoxelAdjacency.equal_range(super_label).second; ++adjacent_itr){
			Supervoxel::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;
}

代码运行结果:

你可能感兴趣的:(pcl)