00001 #include <iostream> 00002 #include <vector> 00003 #include <pcl/point_types.h> 00004 #include <pcl/io/pcd_io.h> 00005 #include <pcl/search/search.h> 00006 #include <pcl/search/kdtree.h> 00007 #include <pcl/features/normal_3d.h> 00008 #include <pcl/visualization/cloud_viewer.h> 00009 #include <pcl/filters/passthrough.h> 00010 #include <pcl/segmentation/region_growing.h> 00011 00012 int 00013 main (int argc, char** argv) 00014 { 00015 pcl::PointCloud<pcl::PointXYZ>::Ptr cloud (new pcl::PointCloud<pcl::PointXYZ>); 00016 if ( pcl::io::loadPCDFile <pcl::PointXYZ> ("region_growing_tutorial.pcd", *cloud) == -1) 00017 { 00018 std::cout << "Cloud reading failed." << std::endl; 00019 return (-1); 00020 } 00021 00022 pcl::search::Search<pcl::PointXYZ>::Ptr tree = boost::shared_ptr<pcl::search::Search<pcl::PointXYZ> > (new pcl::search::KdTree<pcl::PointXYZ>); 00023 pcl::PointCloud <pcl::Normal>::Ptr normals (new pcl::PointCloud <pcl::Normal>); 00024 pcl::NormalEstimation<pcl::PointXYZ, pcl::Normal> normal_estimator; 00025 normal_estimator.setSearchMethod (tree); 00026 normal_estimator.setInputCloud (cloud); 00027 normal_estimator.setKSearch (50); 00028 normal_estimator.compute (*normals); 00029 00030 pcl::IndicesPtr indices (new std::vector <int>); 00031 pcl::PassThrough<pcl::PointXYZ> pass; 00032 pass.setInputCloud (cloud); 00033 pass.setFilterFieldName ("z"); 00034 pass.setFilterLimits (0.0, 1.0); 00035 pass.filter (*indices); 00036 00037 pcl::RegionGrowing<pcl::PointXYZ, pcl::Normal> reg; 00038 reg.setMinClusterSize (100); 00039 reg.setMaxClusterSize (10000); 00040 reg.setSearchMethod (tree); 00041 reg.setNumberOfNeighbours (30); 00042 reg.setInputCloud (cloud); 00043 //reg.setIndices (indices); 00044 reg.setInputNormals (normals); 00045 reg.setSmoothnessThreshold (7.0 / 180.0 * M_PI); 00046 reg.setCurvatureThreshold (1.0); 00047 00048 std::vector <pcl::PointIndices> clusters; 00049 reg.extract (clusters); 00050 00051 std::cout << "Number of clusters is equal to " << clusters.size () << std::endl; 00052 std::cout << "First cluster has " << clusters[0].indices.size () << " points." << endl; 00053 std::cout << "These are the indices of the points of the initial" << 00054 std::endl << "cloud that belong to the first cluster:" << std::endl; 00055 int counter = 0; 00056 while (counter < 5 || counter > clusters[0].indices.size ()) 00057 { 00058 std::cout << clusters[0].indices[counter] << std::endl; 00059 counter++; 00060 } 00061 00062 pcl::PointCloud <pcl::PointXYZRGB>::Ptr colored_cloud = reg.getColoredCloud (); 00063 pcl::visualization::CloudViewer viewer ("Cluster viewer"); 00064 viewer.showCloud(colored_cloud); 00065 while (!viewer.wasStopped ()) 00066 { 00067 } 00068 00069 return (0); 00070 }