Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042 #include <iostream>
00043
00044
00045 #include <boost/thread/thread.hpp>
00046 #include <pcl/point_types.h>
00047 #include <pcl/io/pcd_io.h>
00048 #include <pcl/filters/extract_indices.h>
00049 #include <pcl/features/normal_3d.h>
00050 #include <pcl/kdtree/kdtree.h>
00051 #include <pcl/kdtree/kdtree_flann.h>
00052 #include <pcl/segmentation/extract_clusters.h>
00053
00054
00055 int
00056 main(int argc, char **argv)
00057 {
00058 pcl::PointCloud<pcl::PointXYZ>::Ptr cloud_ptr (new pcl::PointCloud<pcl::PointXYZ> ());
00059 pcl::PointCloud<pcl::Normal>::Ptr cloud_normals (new pcl::PointCloud<pcl::Normal> ());
00060 pcl::PCDWriter writer;
00061
00062 if(pcl::io::loadPCDFile<pcl::PointXYZ> (argv[1], *cloud_ptr) == -1)
00063 {
00064 cout<<"Couldn't read the file "<<argv[1]<<endl;
00065 return -1;
00066 }
00067 std::cout << "Loaded pcd file " << argv[1] << " with " << cloud_ptr->points.size () << std::endl;
00068
00069
00070 pcl::NormalEstimation<pcl::PointXYZ, pcl::Normal> ne;
00071 ne.setInputCloud(cloud_ptr);
00072
00073 pcl::search::KdTree<pcl::PointXYZ>::Ptr tree_n (new pcl::search::KdTree<pcl::PointXYZ>());
00074 ne.setSearchMethod(tree_n);
00075 ne.setRadiusSearch(0.03);
00076 ne.compute(*cloud_normals);
00077 std::cout << "Estimated the normals" << std::endl;
00078
00079
00080 boost::shared_ptr<pcl::KdTree<pcl::PointXYZ> > tree_ec (new pcl::KdTreeFLANN<pcl::PointXYZ> ());
00081 tree_ec->setInputCloud (cloud_ptr);
00082
00083
00084 std::vector<int> indices;
00085 std::vector<pcl::PointIndices> cluster_indices;
00086 const float tolerance = 0.5f;
00087 const double eps_angle = 5*(M_PI/180);
00088 const unsigned int min_cluster_size = 50;
00089
00090 pcl::extractEuclideanClusters(*cloud_ptr, *cloud_normals, tolerance, tree_ec, cluster_indices, eps_angle, min_cluster_size);
00091
00092
00093 std::cout << "No of clusters formed are " << cluster_indices.size () << std::endl;
00094
00095
00096 int j = 0;
00097 for (std::vector<pcl::PointIndices>::const_iterator it = cluster_indices.begin (); it != cluster_indices.end (); ++it)
00098 {
00099 pcl::PointCloud<pcl::PointXYZ>::Ptr cloud_cluster (new pcl::PointCloud<pcl::PointXYZ>);
00100 for (std::vector<int>::const_iterator pit = it->indices.begin (); pit != it->indices.end (); pit++)
00101 cloud_cluster->points.push_back (cloud_ptr->points[*pit]);
00102 cloud_cluster->width = cloud_cluster->points.size ();
00103 cloud_cluster->height = 1;
00104 cloud_cluster->is_dense = true;
00105
00106 std::cout << "PointCloud representing the Cluster using xyzn: " << cloud_cluster->points.size () << " data points." << std::endl;
00107 std::stringstream ss;
00108 ss << "./cloud_cluster_" << j << ".pcd";
00109 writer.write<pcl::PointXYZ> (ss.str (), *cloud_cluster, false);
00110 j++;
00111 }
00112
00113 return 0;
00114 }