supervoxel_clustering.h
Go to the documentation of this file.
00001  
00002 /*
00003  * Software License Agreement (BSD License)
00004  *
00005  *  Point Cloud Library (PCL) - www.pointclouds.org
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  * Author : jpapon@gmail.com
00037  * Email  : jpapon@gmail.com
00038  *
00039  */
00040 
00041 #ifndef PCL_SEGMENTATION_SUPERVOXEL_CLUSTERING_H_
00042 #define PCL_SEGMENTATION_SUPERVOXEL_CLUSTERING_H_
00043 
00044 #include <pcl/features/normal_3d.h>
00045 #include <pcl/pcl_base.h>
00046 #include <pcl/point_cloud.h>
00047 #include <pcl/point_types.h>
00048 #include <pcl/octree/octree.h>
00049 #include <pcl/octree/octree_pointcloud_adjacency.h>
00050 #include <pcl/search/search.h>
00051 #include <pcl/segmentation/boost.h>
00052 
00053 
00054 
00055 //DEBUG TODO REMOVE
00056 #include <pcl/common/time.h>
00057 
00058 
00059 namespace pcl
00060 {
00063   template <typename PointT>
00064   class Supervoxel
00065   {
00066     public:
00067       Supervoxel () :
00068         voxels_ (new pcl::PointCloud<PointT> ()),
00069         normals_ (new pcl::PointCloud<Normal> ())
00070         {  } 
00071       
00072       typedef boost::shared_ptr<Supervoxel<PointT> > Ptr;
00073       typedef boost::shared_ptr<const Supervoxel<PointT> > ConstPtr;
00074 
00078       void
00079       getCentroidPoint (PointXYZRGBA &centroid_arg)
00080       {
00081         centroid_arg = centroid_;
00082       }
00083       
00088       void
00089       getCentroidPointNormal (PointNormal &normal_arg)
00090       {
00091         normal_arg.x = centroid_.x;
00092         normal_arg.y = centroid_.y;
00093         normal_arg.z = centroid_.z;
00094         normal_arg.normal_x = normal_.normal_x;
00095         normal_arg.normal_y = normal_.normal_y;
00096         normal_arg.normal_z = normal_.normal_z;
00097         normal_arg.curvature = normal_.curvature;
00098       }
00099       
00101       pcl::Normal normal_;
00103       pcl::PointXYZRGBA centroid_;
00105       typename pcl::PointCloud<PointT>::Ptr voxels_;
00107       typename pcl::PointCloud<Normal>::Ptr normals_;
00108   };
00109   
00118   template <typename PointT>
00119   class PCL_EXPORTS SupervoxelClustering : public pcl::PCLBase<PointT>
00120   {
00121     //Forward declaration of friended helper class
00122     class SupervoxelHelper;
00123     friend class SupervoxelHelper;
00124     #define MAX_LABEL 16384
00125     public:
00129       class VoxelData
00130       {
00131         public:
00132           VoxelData ():
00133             xyz_ (0.0f, 0.0f, 0.0f),
00134             rgb_ (0.0f, 0.0f, 0.0f),
00135             normal_ (0.0f, 0.0f, 0.0f, 0.0f),
00136             curvature_ (0.0f),
00137             owner_ (0)
00138             {}
00139             
00143           void
00144           getPoint (PointT &point_arg) const;
00145           
00149           void
00150           getNormal (Normal &normal_arg) const;
00151           
00152           Eigen::Vector3f xyz_;
00153           Eigen::Vector3f rgb_;
00154           Eigen::Vector4f normal_;
00155           float curvature_;
00156           float distance_;
00157           int idx_;
00158           SupervoxelHelper* owner_;
00159       };
00160       
00161       typedef pcl::octree::OctreePointCloudAdjacencyContainer<PointT, VoxelData> LeafContainerT;
00162       typedef std::vector <LeafContainerT*> LeafVectorT;
00163       
00164       typedef typename pcl::PointCloud<PointT> PointCloudT;
00165       typedef typename pcl::PointCloud<Normal> NormalCloudT;
00166       typedef typename pcl::octree::OctreePointCloudAdjacency<PointT, LeafContainerT> OctreeAdjacencyT;
00167       typedef typename pcl::octree::OctreePointCloudSearch <PointT> OctreeSearchT;
00168       typedef typename pcl::search::KdTree<PointT> KdTreeT;
00169       typedef boost::shared_ptr<std::vector<int> > IndicesPtr;
00170            
00171       using PCLBase <PointT>::initCompute;
00172       using PCLBase <PointT>::deinitCompute;
00173       using PCLBase <PointT>::input_;
00174       
00175       typedef boost::adjacency_list<boost::setS, boost::setS, boost::undirectedS, uint32_t, float> VoxelAdjacencyList;
00176       typedef VoxelAdjacencyList::vertex_descriptor VoxelID;
00177       typedef VoxelAdjacencyList::edge_descriptor EdgeID;
00178       
00179       
00180     public:
00181 
00187       SupervoxelClustering (float voxel_resolution, float seed_resolution, bool use_single_camera_transform = true);
00188 
00192       virtual
00193       ~SupervoxelClustering ();
00194 
00196       void
00197       setVoxelResolution (float resolution);
00198       
00200       float 
00201       getVoxelResolution () const;
00202       
00204       void
00205       setSeedResolution (float seed_resolution);
00206       
00208       float 
00209       getSeedResolution () const;
00210         
00212       void
00213       setColorImportance (float val);
00214       
00216       void
00217       setSpatialImportance (float val);
00218             
00220       void
00221       setNormalImportance (float val);
00222       
00227       virtual void
00228       extract (std::map<uint32_t,typename Supervoxel<PointT>::Ptr > &supervoxel_clusters);
00229 
00233       virtual void
00234       setInputCloud (typename pcl::PointCloud<PointT>::ConstPtr cloud);
00235       
00240       virtual void
00241       refineSupervoxels (int num_itr, std::map<uint32_t,typename Supervoxel<PointT>::Ptr > &supervoxel_clusters);
00242       
00244 
00250       typename pcl::PointCloud<PointXYZRGBA>::Ptr
00251       getColoredCloud () const;
00252       
00254       typename pcl::PointCloud<PointT>::Ptr
00255       getVoxelCentroidCloud () const;
00256       
00261       typename pcl::PointCloud<PointXYZL>::Ptr
00262       getLabeledCloud () const;
00263       
00270       pcl::PointCloud<pcl::PointXYZRGBA>::Ptr
00271       getColoredVoxelCloud () const;
00272       
00277       pcl::PointCloud<pcl::PointXYZL>::Ptr
00278       getLabeledVoxelCloud () const;
00279 
00283       void
00284       getSupervoxelAdjacencyList (VoxelAdjacencyList &adjacency_list_arg) const;
00285       
00289       void 
00290       getSupervoxelAdjacency (std::multimap<uint32_t, uint32_t> &label_adjacency) const;
00291             
00297       static pcl::PointCloud<pcl::PointNormal>::Ptr
00298       makeSupervoxelNormalCloud (std::map<uint32_t,typename Supervoxel<PointT>::Ptr > &supervoxel_clusters);
00299     private:
00300       
00304       virtual bool
00305       prepareForSegmentation ();
00306 
00310       void
00311       selectInitialSupervoxelSeeds (std::vector<PointT, Eigen::aligned_allocator<PointT> > &seed_points);
00312       
00316       void
00317       createSupervoxelHelpers (std::vector<PointT, Eigen::aligned_allocator<PointT> > &seed_points);
00318       
00320       void
00321       expandSupervoxels (int depth);
00322 
00324       void 
00325       computeVoxelData ();
00326      
00328       void
00329       reseedSupervoxels ();
00330       
00332       void
00333       makeSupervoxels (std::map<uint32_t,typename Supervoxel<PointT>::Ptr > &supervoxel_clusters);
00334       
00336       float resolution_;
00337     
00339       float seed_resolution_;
00340       
00342       float
00343       voxelDataDistance (const VoxelData &v1, const VoxelData &v2) const;
00344       
00346       void
00347       transformFunction (PointT &p);
00348       
00350       typename pcl::search::KdTree<PointT>::Ptr voxel_kdtree_;
00351       
00353       typename OctreeAdjacencyT::Ptr adjacency_octree_;
00354       
00356       typename PointCloudT::Ptr voxel_centroid_cloud_;
00357       
00359       float color_importance_;
00361       float spatial_importance_;
00363       float normal_importance_;
00364       
00366       std::vector<uint32_t> label_colors_;
00367       
00372       class SupervoxelHelper
00373       {
00374         public:
00375           SupervoxelHelper (uint32_t label, SupervoxelClustering* parent_arg):
00376             label_ (label),
00377             parent_ (parent_arg)
00378           { }
00379           
00380           void
00381           addLeaf (LeafContainerT* leaf_arg);
00382         
00383           void
00384           removeLeaf (LeafContainerT* leaf_arg);
00385         
00386           void
00387           removeAllLeaves ();
00388           
00389           void 
00390           expand ();
00391           
00392           void 
00393           refineNormals ();
00394           
00395           void 
00396           updateCentroid ();
00397           
00398           void 
00399           getVoxels (typename pcl::PointCloud<PointT>::Ptr &voxels) const;
00400           
00401           void 
00402           getNormals (typename pcl::PointCloud<Normal>::Ptr &normals) const;
00403           
00404           typedef float (SupervoxelClustering::*DistFuncPtr)(const VoxelData &v1, const VoxelData &v2);
00405           
00406           uint32_t
00407           getLabel () const 
00408           { return label_; }
00409           
00410           Eigen::Vector4f 
00411           getNormal () const 
00412           { return centroid_.normal_; }
00413           
00414           Eigen::Vector3f 
00415           getRGB () const 
00416           { return centroid_.rgb_; }
00417           
00418           Eigen::Vector3f
00419           getXYZ () const 
00420           { return centroid_.xyz_;}
00421           
00422           void
00423           getXYZ (float &x, float &y, float &z) const
00424           { x=centroid_.xyz_[0]; y=centroid_.xyz_[1]; z=centroid_.xyz_[2]; }
00425           
00426           void
00427           getRGB (uint32_t &rgba) const
00428           { 
00429             rgba = static_cast<uint32_t>(centroid_.rgb_[0]) << 16 | 
00430                    static_cast<uint32_t>(centroid_.rgb_[1]) << 8 | 
00431                    static_cast<uint32_t>(centroid_.rgb_[2]); 
00432           }
00433           
00434           void 
00435           getNormal (pcl::Normal &normal_arg) const 
00436           { 
00437             normal_arg.normal_x = centroid_.normal_[0];
00438             normal_arg.normal_y = centroid_.normal_[1];
00439             normal_arg.normal_z = centroid_.normal_[2];
00440             normal_arg.curvature = centroid_.curvature_;
00441           }
00442           
00443           void
00444           getNeighborLabels (std::set<uint32_t> &neighbor_labels) const;
00445           
00446           VoxelData
00447           getCentroid () const
00448           { return centroid_; }
00449             
00450           
00451           size_t
00452           size () const { return leaves_.size (); }
00453       private:
00454           //Stores leaves
00455           std::set<LeafContainerT*> leaves_;
00456           uint32_t label_;
00457           VoxelData centroid_;
00458           SupervoxelClustering* parent_;
00459           
00460 
00461       };
00462       
00463       typedef boost::ptr_list<SupervoxelHelper> HelperListT;
00464       HelperListT supervoxel_helpers_;
00465       
00466       //TODO DEBUG REMOVE
00467       StopWatch timer_;
00468     public:
00469       EIGEN_MAKE_ALIGNED_OPERATOR_NEW
00470       
00471      
00472 
00473   };
00474 
00475 }
00476 
00477 #ifdef PCL_NO_PRECOMPILE
00478 #include <pcl/segmentation/impl/supervoxel_clustering.hpp>
00479 #endif
00480 
00481 #endif


pcl
Author(s): Open Perception
autogenerated on Wed Aug 26 2015 15:33:59