|
添加聚类所需的库文件
#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/filters/passthrough.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/point_cloud_color_handlers.h>
#include <pcl/visualization/pcl_visualizer.h>
#include <iostream>
#include <pcl/io/io.h>
#include <pcl/io/pcd_io.h>
int
main (int argc, char** argv)
{
//从硬盘读取pcd格式的点云文件,点云数据含有RGB信息
pcl::PCDReader reader;
pcl::PointCloud<pcl::PointXYZRGBA>::Ptr cloud (new pcl::PointCloud<pcl::PointXYZRGBA>), cloud_f (new pcl::PointCloud<pcl::PointXYZRGBA>);
pcl::PointCloud<pcl::PointXYZRGBA>::Ptr pass_filtered (new pcl::PointCloud<pcl::PointXYZRGBA>);
reader.read ("scene_1.pcd", *cloud);//点云文件“scene_1.pcd”由Kinect采集获得
//滤波,只保留深度信息为0.4m到1.55m之间的点云数据
std::cout << "PointCloud before filtering has: " << cloud->points.size () << " data points." << std::endl; //*
pcl::PassThrough<pcl::PointXYZRGBA> pass;
// Build a passthrough filter to remove spurious NaNs
pass.setInputCloud (cloud);
pass.setFilterFieldName ("z");
pass.setFilterLimits (0.4, 1.55);
pass.filter (*pass_filtered);
//std::cerr << "PointCloud after filtering has: " << cloud_filtered->points.size () << " data points." << std::endl;
//可视化,显示滤波后的点云数据
pcl::visualization::CloudViewer viewerr ("filter");
viewerr.showCloud(pass_filtered);
while (!viewerr.wasStopped ())
{
//iewerr.spinOnce ();
}
// 下采样滤波,在保持点云数据形状不变的前提下,减少点云数据,其中叶子尺寸值越大,点云数据量越少
pcl::VoxelGrid<pcl::PointXYZRGBA> vg;
pcl::PointCloud<pcl::PointXYZRGBA>::Ptr cloud_filtered (new pcl::PointCloud<pcl::PointXYZRGBA>);
vg.setInputCloud (pass_filtered);
vg.setLeafSize (0.005f, 0.005f, 0.005f);
vg.filter (*cloud_filtered);
std::cout << "PointCloud after filtering has: " << cloud_filtered->points.size () << " data points." << std::endl; //*
// 随机采样分割出平面
pcl::SACSegmentation<pcl::PointXYZRGBA> seg;
pcl::PointIndices::Ptr inliers (new pcl::PointIndices);
pcl::ModelCoefficients::Ptr coefficients (new pcl::ModelCoefficients);
pcl::PointCloud<pcl::PointXYZRGBA>::Ptr cloud_plane (new pcl::PointCloud<pcl::PointXYZRGBA> ());
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::PointXYZRGBA> extract;
extract.setInputCloud (cloud_filtered);
extract.setIndices (inliers);
extract.setNegative (false);
// Write the planar inliers to disk
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;
}
// 创建近临搜索对象,聚类分割对象
pcl::search::KdTree<pcl::PointXYZRGBA>::Ptr tree (new pcl::search::KdTree<pcl::PointXYZRGBA>);
tree->setInputCloud (cloud_filtered);
std::vector<pcl::PointIndices> cluster_indices;//类型是向量,存储分割后的各个聚类
pcl::EuclideanClusterExtraction<pcl::PointXYZRGBA> ec;//聚类分割对象
ec.setClusterTolerance (0.1); // 2cm设置ClusterTolerance,该值为近临搜索半径
ec.setMinClusterSize (500);//聚类中点的数量下限
ec.setMaxClusterSize (25000);//聚类中点的数量上限
ec.setSearchMethod (tree);//搜索方法,radiusSearch
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)//迭代查询聚类向量cluster_indices中所存储的聚类
{
pcl::PointCloud<pcl::PointXYZRGBA>::Ptr cloud_cluster (new pcl::PointCloud<pcl::PointXYZRGBA>);
for (std::vector<int>::const_iterator pit = it->indices.begin (); pit != it->indices.end (); pit++)//将cluster_indices中第pit个聚类的点云数据写入cloud_cluster
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::PointXYZRGBA> (ss.str (), *cloud_cluster, false); //将聚类点云文件写入当前目录下
j++;
pcl::visualization::PCLVisualizer viewer("Cloud Viewer"); //窗口显示第pit个聚类
pcl::visualization::PointCloudColorHandlerRGBField<pcl::PointXYZRGBA> color(cloud_cluster); //显示点云RGB信息
viewer.addPointCloud(cloud_cluster,color);
viewer.setBackgroundColor(1,1,1);
while (!viewer.wasStopped ())
{
//ÔÚŽËŽŠ¿ÉÒÔÌíŒÓÆäËûŽŠÀí
viewer.spinOnce();
}
}
return (0);
}
//编译运行文件,将下面代码写入CMakeLists.txt文件的最后面
cmake_minimum_required(VERSION 2.8 FATAL_ERROR)
project(cluster_extraction)
find_package(PCL 1.7 REQUIRED)
include_directories(${PCL_INCLUDE_DIRS})
link_directories(${PCL_LIBRARY_DIRS})
add_definitions(${PCL_DEFINITIONS})
add_executable (cluster_extraction src/cluster_extraction.cpp)
target_link_libraries (cluster_extraction ${PCL_LIBRARIES})
//编译文件
rosmake
//运行
./cluster_extraction
//显示运行结果
|
|