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 #include <iostream>
00042
00043
00044 #include <pcl/filters/filter.h>
00045 #include <pcl/features/normal_3d.h>
00046 #include <pcl/point_types.h>
00047 #include <pcl/point_cloud.h>
00048 #include <pcl/io/pcd_io.h>
00049 #include <pcl/segmentation/region_growing.h>
00050 #include <pcl/kdtree/kdtree.h>
00051 #include <pcl/common/time.h>
00052 #include <pcl/console/parse.h>
00053
00054 int
00055 main (int argc, char** av)
00056 {
00057 if (argc < 2)
00058 {
00059 pcl::console::print_info ("Syntax is: %s <source-pcd-file> [-dump]\n\n", av[0]);
00060 pcl::console::print_info ("If -dump is provided write the extracted clusters to cluster.dat\n\n");
00061 return (1);
00062 }
00063
00064 pcl::PointCloud<pcl::PointXYZ>::Ptr cloud_ptr (new pcl::PointCloud<pcl::PointXYZ>());
00065 pcl::PointCloud<pcl::PointXYZ>::Ptr cloud_no_nans (new pcl::PointCloud<pcl::PointXYZ>());
00066 pcl::PointCloud<pcl::Normal>::Ptr cloud_normals (new pcl::PointCloud<pcl::Normal>());
00067 pcl::PointCloud<pcl::PointXYZRGB>::Ptr cloud_segmented (new pcl::PointCloud<pcl::PointXYZRGB>());
00068
00069 pcl::PCDWriter writer;
00070 if (pcl::io::loadPCDFile(av[1], *cloud_ptr)==-1)
00071 {
00072 return -1;
00073 }
00074
00075 pcl::console::print_highlight ("Loaded cloud %s of size %zu\n", av[1], cloud_ptr->points.size ());
00076
00077
00078 cloud_ptr->is_dense = false;
00079 cloud_no_nans->is_dense = false;
00080 std::vector<int> indices;
00081 pcl::removeNaNFromPointCloud (*cloud_ptr, *cloud_no_nans, indices);
00082 pcl::console::print_highlight ("Removed nans from %zu to %zu\n", cloud_ptr->points.size (), cloud_no_nans->points.size ());
00083
00084
00085 pcl::NormalEstimation<pcl::PointXYZ, pcl::Normal> ne;
00086 ne.setInputCloud (cloud_no_nans);
00087 pcl::search::KdTree<pcl::PointXYZ>::Ptr tree_n (new pcl::search::KdTree<pcl::PointXYZ>());
00088 ne.setSearchMethod (tree_n);
00089 ne.setRadiusSearch (0.03);
00090 ne.compute (*cloud_normals);
00091 pcl::console::print_highlight ("Normals are computed and size is %zu\n", cloud_normals->points.size ());
00092
00093
00094 pcl::RegionGrowing<pcl::PointXYZ, pcl::Normal> rg;
00095 rg.setSmoothModeFlag (false);
00096 rg.setInputCloud (cloud_no_nans);
00097 rg.setInputNormals (cloud_normals);
00098
00099 std::vector <pcl::PointIndices> clusters;
00100 pcl::StopWatch watch;
00101 rg.extract (clusters);
00102 pcl::console::print_highlight ("Extraction time: %f\n", watch.getTimeSeconds());
00103 cloud_segmented = rg.getColoredCloud ();
00104
00105
00106 pcl::console::print_highlight ("Number of segments done is %zu\n", clusters.size ());
00107 writer.write<pcl::PointXYZRGB> ("segment_result.pcd", *cloud_segmented, false);
00108
00109 if (pcl::console::find_switch (argc, av, "-dump"))
00110 {
00111 pcl::console::print_highlight ("Writing clusters to clusters.dat\n");
00112 std::ofstream clusters_file;
00113 clusters_file.open ("clusters.dat");
00114 for (std::size_t i = 0; i < clusters.size (); ++i)
00115 {
00116 clusters_file << i << "#" << clusters[i].indices.size () << ": ";
00117 std::vector<int>::const_iterator pit = clusters[i].indices.begin ();
00118 clusters_file << *pit;
00119 for (; pit != clusters[i].indices.end (); ++pit)
00120 clusters_file << " " << *pit;
00121 clusters_file << std::endl;
00122 }
00123 clusters_file.close ();
00124 }
00125
00126 return (0);
00127 }