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 <pcl/point_types.h>
00046 #include <pcl/io/pcd_io.h>
00047 #include <pcl/filters/extract_indices.h>
00048 #include <pcl/features/normal_3d.h>
00049 #include <pcl/kdtree/kdtree.h>
00050 #include <pcl/kdtree/kdtree_flann.h>
00051 #include <pcl/segmentation/extract_clusters.h>
00052
00053
00054 int
00055 main (int, char **argv)
00056 {
00057 pcl::PointCloud<pcl::PointXYZ>::Ptr cloud_ptr (new pcl::PointCloud<pcl::PointXYZ> ());
00058 pcl::PointCloud<pcl::Normal>::Ptr cloud_normals (new pcl::PointCloud<pcl::Normal> ());
00059 pcl::PCDWriter writer;
00060
00061 if (pcl::io::loadPCDFile<pcl::PointXYZ> (argv[1], *cloud_ptr) == -1)
00062 {
00063 std::cout<<"Couldn't read the file "<<argv[1]<<std::endl;
00064 return (-1);
00065 }
00066 std::cout << "Loaded pcd file " << argv[1] << " with " << cloud_ptr->points.size () << std::endl;
00067
00068
00069 pcl::NormalEstimation<pcl::PointXYZ, pcl::Normal> ne;
00070 ne.setInputCloud (cloud_ptr);
00071
00072 pcl::search::KdTree<pcl::PointXYZ>::Ptr tree_n (new pcl::search::KdTree<pcl::PointXYZ>());
00073 ne.setSearchMethod (tree_n);
00074 ne.setRadiusSearch (0.03);
00075 ne.compute (*cloud_normals);
00076 std::cout << "Estimated the normals" << std::endl;
00077
00078
00079 boost::shared_ptr<pcl::KdTree<pcl::PointXYZ> > tree_ec (new pcl::KdTreeFLANN<pcl::PointXYZ> ());
00080 tree_ec->setInputCloud (cloud_ptr);
00081
00082
00083 std::vector<int> indices;
00084 std::vector<pcl::PointIndices> cluster_indices;
00085 const float tolerance = 0.5f;
00086 const double eps_angle = 5 * (M_PI / 180.0);
00087 const unsigned int min_cluster_size = 50;
00088
00089 pcl::extractEuclideanClusters (*cloud_ptr, *cloud_normals, tolerance, tree_ec, cluster_indices, eps_angle, min_cluster_size);
00090
00091 std::cout << "No of clusters formed are " << cluster_indices.size () << std::endl;
00092
00093
00094 int j = 0;
00095 for (std::vector<pcl::PointIndices>::const_iterator it = cluster_indices.begin (); it != cluster_indices.end (); ++it)
00096 {
00097 pcl::PointCloud<pcl::PointXYZ>::Ptr cloud_cluster (new pcl::PointCloud<pcl::PointXYZ>);
00098 for (std::vector<int>::const_iterator pit = it->indices.begin (); pit != it->indices.end (); pit++)
00099 cloud_cluster->points.push_back (cloud_ptr->points[*pit]);
00100 cloud_cluster->width = static_cast<uint32_t> (cloud_cluster->points.size ());
00101 cloud_cluster->height = 1;
00102 cloud_cluster->is_dense = true;
00103
00104 std::cout << "PointCloud representing the Cluster using xyzn: " << cloud_cluster->points.size () << " data points." << std::endl;
00105 std::stringstream ss;
00106 ss << "./cloud_cluster_" << j << ".pcd";
00107 writer.write<pcl::PointXYZ> (ss.str (), *cloud_cluster, false);
00108 j++;
00109 }
00110
00111 return (0);
00112 }