初学者笔记:
点云数据链接(不会上传,所以用了百度云):
链接: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;
}
代码运行结果: