Point Cloud Library (PCL)  1.3.1
extract_labeled_clusters.hpp
Go to the documentation of this file.
00001 /*
00002  * Software License Agreement (BSD License)
00003  *
00004  *  Copyright (c) 2011, Willow Garage, Inc.
00005  *  All rights reserved.
00006  *
00007  *  Redistribution and use in source and binary forms, with or without
00008  *  modification, are permitted provided that the following conditions
00009  *  are met:
00010  *
00011  *   * Redistributions of source code must retain the above copyright
00012  *     notice, this list of conditions and the following disclaimer.
00013  *   * Redistributions in binary form must reproduce the above
00014  *     copyright notice, this list of conditions and the following
00015  *     disclaimer in the documentation and/or other materials provided
00016  *     with the distribution.
00017  *   * Neither the name of Willow Garage, Inc. nor the names of its
00018  *     contributors may be used to endorse or promote products derived
00019  *     from this software without specific prior written permission.
00020  *
00021  *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00022  *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00023  *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
00024  *  FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
00025  *  COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
00026  *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
00027  *  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00028  *  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00029  *  CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00030  *  LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
00031  *  ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00032  *  POSSIBILITY OF SUCH DAMAGE.
00033  *
00034  *
00035  */
00036 
00037 #ifndef PCL_SEGMENTATION_IMPL_EXTRACT_LABELED_CLUSTERS_H_
00038 #define PCL_SEGMENTATION_IMPL_EXTRACT_LABELED_CLUSTERS_H_
00039 
00040 #include "pcl/segmentation/extract_labeled_clusters.h"
00041 
00043 template <typename PointT> void
00044 pcl::extractLabeledEuclideanClusters (const PointCloud<PointT> &cloud, 
00045                                       const boost::shared_ptr<search::Search<PointT> > &tree,
00046                                       float tolerance, 
00047                                       std::vector<std::vector<PointIndices> > &labeled_clusters,
00048                                       unsigned int min_pts_per_cluster, 
00049                                       unsigned int max_pts_per_cluster,
00050                                       unsigned int max_label)
00051 {
00052   if (tree->getInputCloud ()->points.size () != cloud.points.size ())
00053   {
00054     PCL_ERROR ("[pcl::extractLabeledEuclideanClusters] Tree built for a different point cloud dataset (%lu) than the input cloud (%lu)!\n", (unsigned long)tree->getInputCloud ()->points.size (), (unsigned long)cloud.points.size ());
00055     return;
00056   }
00057   // Create a bool vector of processed point indices, and initialize it to false
00058   std::vector<bool> processed (cloud.points.size (), false);
00059 
00060   std::vector<int> nn_indices;
00061   std::vector<float> nn_distances;
00062 
00063   // Process all points in the indices vector
00064   for (size_t i = 0; i < cloud.points.size (); ++i)
00065   {
00066     if (processed[i])
00067       continue;
00068 
00069     std::vector<int> seed_queue;
00070     int sq_idx = 0;
00071     seed_queue.push_back (i);
00072 
00073     processed[i] = true;
00074 
00075     while (sq_idx < (int)seed_queue.size ())
00076     {
00077       // Search for sq_idx
00078       if (!tree->radiusSearch (seed_queue[sq_idx], tolerance, nn_indices, nn_distances))
00079       {
00080         sq_idx++;
00081         continue;
00082       }
00083 
00084       for (size_t j = 1; j < nn_indices.size (); ++j)             // nn_indices[0] should be sq_idx
00085       {
00086         if (processed[nn_indices[j]])                             // Has this point been processed before ?
00087           continue;
00088         if (cloud.points[i].label == cloud.points[nn_indices[j]].label)
00089         {
00090           // Perform a simple Euclidean clustering
00091           seed_queue.push_back (nn_indices[j]);
00092           processed[nn_indices[j]] = true;
00093         }
00094       }
00095 
00096       sq_idx++;
00097     }
00098 
00099     // If this queue is satisfactory, add to the clusters
00100     if (seed_queue.size () >= min_pts_per_cluster && seed_queue.size () <= max_pts_per_cluster)
00101     {
00102       pcl::PointIndices r;
00103       r.indices.resize (seed_queue.size ());
00104       for (size_t j = 0; j < seed_queue.size (); ++j)
00105         r.indices[j] = seed_queue[j];
00106 
00107       std::sort (r.indices.begin (), r.indices.end ());
00108       r.indices.erase (std::unique (r.indices.begin (), r.indices.end ()), r.indices.end ());
00109 
00110       r.header = cloud.header;
00111       labeled_clusters[cloud.points[i].label].push_back (r);   // We could avoid a copy by working directly in the vector
00112     }
00113   }
00114 }
00118 
00119 template <typename PointT> void 
00120 pcl::LabeledEuclideanClusterExtraction<PointT>::extract (std::vector<std::vector<PointIndices> > &labeled_clusters)
00121 {
00122   if (!initCompute () || 
00123       (input_ != 0   && input_->points.empty ()) ||
00124       (indices_ != 0 && indices_->empty ()))
00125   {
00126     labeled_clusters.clear ();
00127     return;
00128   }
00129 
00130   // Initialize the spatial locator
00131   if (!tree_)
00132   {
00133     if (input_->isOrganized ())
00134       tree_.reset (new pcl::search::OrganizedNeighbor<PointT> ());
00135     else
00136       tree_.reset (new pcl::search::KdTree<PointT> (false));
00137   }
00138 
00139   // Send the input dataset to the spatial locator
00140   tree_->setInputCloud (input_);
00141   extractLabeledEuclideanClusters (*input_, tree_, cluster_tolerance_, labeled_clusters, min_pts_per_cluster_, max_pts_per_cluster_, max_label_);
00142 
00143   // Sort the clusters based on their size (largest one first)
00144   for(unsigned int i = 0; i < labeled_clusters.size(); i++)
00145     std::sort (labeled_clusters[i].rbegin (), labeled_clusters[i].rend (), comparePointClusters);
00146 
00147   deinitCompute ();
00148 }
00149 
00150 #define PCL_INSTANTIATE_LabeledEuclideanClusterExtraction(T) template class PCL_EXPORTS pcl::LabeledEuclideanClusterExtraction<T>;
00151 #define PCL_INSTANTIATE_extractLabeledEuclideanClusters(T) template void PCL_EXPORTS pcl::extractLabeledEuclideanClusters<T>(const pcl::PointCloud<T> &, const boost::shared_ptr<pcl::search::Search<T> > &, float , std::vector<std::vector<pcl::PointIndices> > &, unsigned int, unsigned int, unsigned int);
00152 
00153 #endif        // PCL_EXTRACT_CLUSTERS_IMPL_H_
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines