目标:分割出地板,以及地板以上的不同物体,对物体进行运动检测
欧式聚类的目标,分割出椅子,桌子,圆凳等物体
聚类失败,聚类的结果是一条一条线,是怎么回事,下采样参数10,聚类最小距离15
原点云:
分割出的点云:
// std::vector<int> nanIndex;
// pcl::removeNaNFromPointCloud(*cloud,*cloud,nanIndex);
// 创建体素网格滤波器对象
// 应用滤波器,进行下采样
pcl::PointCloud<pcl::PointXYZRGB>::Ptr cloud_downsampled(new pcl::PointCloud<pcl::PointXYZRGB>);
pcl::VoxelGrid<pcl::PointXYZRGB> vg;
vg.setInputCloud(cloud);
vg.setLeafSize(10.0f, 10.0f, 10.0f); // 设置体素网格的大小
vg.filter(*cloud_downsampled);
qDebug()<<"cloud_downsampled size"<<cloud_downsampled->size();
// 统计学滤波
pcl::PointCloud<pcl::PointXYZRGB>::Ptr cloud_filtered (new pcl::PointCloud<pcl::PointXYZRGB>);
pcl::StatisticalOutlierRemoval<pcl::PointXYZRGB> sor;
sor.setInputCloud(cloud_downsampled);
sor.setMeanK(10); // 设置用于计算邻域点的个数
sor.setStddevMulThresh(10); // 设置标准差乘数阈值
sor.filter(*cloud_filtered);
int v2=0;
// PointXYZRGB
pcl::search::KdTree<pcl::PointXYZRGB>::Ptr tree(new pcl::search::KdTree<pcl::PointXYZRGB>);
tree->setInputCloud(cloud_downsampled);
simpleVis(cloud_downsampled);
//聚类抽取结果保存在一个数组中,数组中每个元素代表抽取的一个组件点云的下标
std::vector<pcl::PointIndices> cluster_indices;
pcl::EuclideanClusterExtraction<pcl::PointXYZRGB> ec;
ec.setClusterTolerance(15);
ec.setMinClusterSize(1000);
ec.setMaxClusterSize(500000);
ec.setSearchMethod(tree);
ec.setInputCloud(cloud_downsampled);
ec.extract(cluster_indices);
pcl::visualization::PCLVisualizer viewer("Cluster Extraction");
//遍历抽取结果,将其显示并保存
int j = 0;
for (std::vector<pcl::PointIndices>::const_iterator it = cluster_indices.begin(); it != cluster_indices.end(); ++it)
{
//创建临时保存点云族的点云
pcl::PointCloud<pcl::PointXYZRGB>::Ptr cloud_cluster(new pcl::PointCloud<pcl::PointXYZRGB>);
//通过下标,逐个填充
for (std::vector<int>::const_iterator pit = it->indices.begin(); pit != it->indices.end(); pit++)
cloud_cluster->points.push_back(cloud->points[*pit]); //*
//设置点云属性
cloud_cluster->width = cloud_cluster->points.size();
cloud_cluster->height = 1;
cloud_cluster->is_dense = true;
pcl::PCDWriter writer;
std::cout << "NO: "<<j<<" include point cloud: " << cloud_cluster->points.size() << " data points." << std::endl;
std::stringstream ss;
ss << "cloud_cluster_" << j << ".pcd";
// writer.write<pcl::PointXYZRGB>(ss.str(), *cloud, false); //*
j++;
// 显示,随机设置不同颜色,以区分不同的聚类
pcl::visualization::PointCloudColorHandlerCustom<pcl::PointXYZRGB> cluster_color(cloud_cluster, rand()*100 + j * 80, rand() * 50 + j * 90, rand() * 200 + j * 100);
viewer.addPointCloud<pcl::PointXYZRGB>(cloud_cluster,cluster_color, ss.str(), v2);
viewer.addPointCloud<pcl::PointXYZRGB>(cloud_cluster, cluster_color, "cluster cloud");
//break;
//viewer.spinOnce(5000);
}
viewer.addCoordinateSystem(1000.0);
while (!viewer.wasStopped())
{
viewer.spinOnce();
}
return;