example_region_growing.cpp
Go to the documentation of this file.
00001 /*
00002  * Software License Agreement (BSD License)
00003  *
00004  * Point Cloud Library (PCL) - www.pointclouds.org
00005  * Copyright (c) 2009-2011, Willow Garage, Inc.
00006  *
00007  * All rights reserved.
00008  *
00009  * Redistribution and use in source and binary forms, with or without
00010  * modification, are permitted provided that the following conditions
00011  * are met:
00012  *
00013  * * Redistributions of source code must retain the above copyright
00014  *   notice, this list of conditions and the following disclaimer.
00015  * * Redistributions in binary form must reproduce the above
00016  *   copyright notice, this list of conditions and the following
00017  *   disclaimer in the documentation and/or other materials provided
00018  *   with the distribution.
00019  * * Neither the name of Willow Garage, Inc. nor the names of its
00020  *   contributors may be used to endorse or promote products derived
00021  *   from this software without specific prior written permission.
00022  *
00023  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00024  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00025  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
00026  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
00027  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
00028  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
00029  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00030  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00031  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00032  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
00033  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00034  * POSSIBILITY OF SUCH DAMAGE.
00035  *
00036  * $Id:$
00037  *
00038  */
00039 
00040 // STL
00041 #include <iostream>
00042 
00043 // PCL
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   // Remove the nans
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   // Estimate the normals
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   // Region growing
00094   pcl::RegionGrowing<pcl::PointXYZ, pcl::Normal> rg;
00095   rg.setSmoothModeFlag (false); // Depends on the cloud being processed
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   // Writing the resulting cloud into a pcd file
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 }


pcl
Author(s): Open Perception
autogenerated on Wed Aug 26 2015 15:23:36