example_extract_clusters_normals.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: example_extract_clusters_normals.cpp 6079 2012-07-01 13:46:51Z desinghkar $
00037  *
00038  *
00039  */
00040 
00041 // STL
00042 #include <iostream>
00043 
00044 // PCL
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   // Normal estimation
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   // Creating the kdtree object for the search method of the extraction
00080   boost::shared_ptr<pcl::KdTree<pcl::PointXYZ> > tree_ec  (new pcl::KdTreeFLANN<pcl::PointXYZ> ());
00081   tree_ec->setInputCloud (cloud_ptr);
00082   
00083   // Extracting Euclidean clusters using cloud and its normals
00084   std::vector<int> indices;
00085   std::vector<pcl::PointIndices> cluster_indices;
00086   const float tolerance = 0.5f; // 50cm tolerance in (x, y, z) coordinate system
00087   const double eps_angle = 5*(M_PI/180); // 5degree tolerance in normals
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   // Saving the clusters in seperate pcd files
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 }


pcl
Author(s): Open Perception
autogenerated on Mon Oct 6 2014 03:14:52