Point Cloud Library (PCL)  1.8.1-dev
min_cut_segmentation.h
1 /*
2  * Software License Agreement (BSD License)
3  *
4  * Point Cloud Library (PCL) - www.pointclouds.org
5  *
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * * Redistributions of source code must retain the above copyright
13  * notice, this list of conditions and the following disclaimer.
14  * * Redistributions in binary form must reproduce the above
15  * copyright notice, this list of conditions and the following
16  * disclaimer in the documentation and/or other materials provided
17  * with the distribution.
18  * * Neither the name of the copyright holder(s) nor the names of its
19  * contributors may be used to endorse or promote products derived
20  * from this software without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
25  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
26  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
27  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
28  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
29  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
30  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
32  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33  * POSSIBILITY OF SUCH DAMAGE.
34  *
35  * $Id:$
36  *
37  */
38 
39 #ifndef PCL_MIN_CUT_SEGMENTATION_H_
40 #define PCL_MIN_CUT_SEGMENTATION_H_
41 
42 #include <pcl/segmentation/boost.h>
43 #if (BOOST_VERSION >= 104400)
44 #include <pcl/pcl_base.h>
45 #include <pcl/point_cloud.h>
46 #include <pcl/point_types.h>
47 #include <pcl/search/search.h>
48 #include <string>
49 #include <set>
50 
51 namespace pcl
52 {
53  /** \brief This class implements the segmentation algorithm based on minimal cut of the graph.
54  * The description can be found in the article:
55  * "Min-Cut Based Segmentation of Point Clouds"
56  * \author: Aleksey Golovinskiy and Thomas Funkhouser.
57  */
58  template <typename PointT>
59  class PCL_EXPORTS MinCutSegmentation : public pcl::PCLBase<PointT>
60  {
61  public:
62 
63  typedef pcl::search::Search <PointT> KdTree;
64  typedef typename KdTree::Ptr KdTreePtr;
66  typedef typename PointCloud::ConstPtr PointCloudConstPtr;
67 
68  using PCLBase <PointT>::input_;
69  using PCLBase <PointT>::indices_;
70  using PCLBase <PointT>::initCompute;
71  using PCLBase <PointT>::deinitCompute;
72 
73  public:
74 
75  typedef boost::adjacency_list_traits< boost::vecS, boost::vecS, boost::directedS > Traits;
76 
77  typedef boost::adjacency_list< boost::vecS, boost::vecS, boost::directedS,
78  boost::property< boost::vertex_name_t, std::string,
79  boost::property< boost::vertex_index_t, long,
80  boost::property< boost::vertex_color_t, boost::default_color_type,
81  boost::property< boost::vertex_distance_t, long,
82  boost::property< boost::vertex_predecessor_t, Traits::edge_descriptor > > > > >,
83  boost::property< boost::edge_capacity_t, double,
84  boost::property< boost::edge_residual_capacity_t, double,
85  boost::property< boost::edge_reverse_t, Traits::edge_descriptor > > > > mGraph;
86 
87  typedef boost::property_map< mGraph, boost::edge_capacity_t >::type CapacityMap;
88 
89  typedef boost::property_map< mGraph, boost::edge_reverse_t>::type ReverseEdgeMap;
90 
91  typedef Traits::vertex_descriptor VertexDescriptor;
92 
93  typedef boost::graph_traits< mGraph >::edge_descriptor EdgeDescriptor;
94 
95  typedef boost::graph_traits< mGraph >::out_edge_iterator OutEdgeIterator;
96 
97  typedef boost::graph_traits< mGraph >::vertex_iterator VertexIterator;
98 
99  typedef boost::property_map< mGraph, boost::edge_residual_capacity_t >::type ResidualCapacityMap;
100 
101  typedef boost::property_map< mGraph, boost::vertex_index_t >::type IndexMap;
102 
103  typedef boost::graph_traits< mGraph >::in_edge_iterator InEdgeIterator;
104 
105  public:
106 
107  /** \brief Constructor that sets default values for member variables. */
108  MinCutSegmentation ();
109 
110  /** \brief Destructor that frees memory. */
111  virtual
112  ~MinCutSegmentation ();
113 
114  /** \brief This method simply sets the input point cloud.
115  * \param[in] cloud the const boost shared pointer to a PointCloud
116  */
117  virtual void
118  setInputCloud (const PointCloudConstPtr &cloud);
119 
120  /** \brief Returns normalization value for binary potentials. For more information see the article. */
121  double
122  getSigma () const;
123 
124  /** \brief Allows to set the normalization value for the binary potentials as described in the article.
125  * \param[in] sigma new normalization value
126  */
127  void
128  setSigma (double sigma);
129 
130  /** \brief Returns radius to the background. */
131  double
132  getRadius () const;
133 
134  /** \brief Allows to set the radius to the background.
135  * \param[in] radius new radius to the background
136  */
137  void
138  setRadius (double radius);
139 
140  /** \brief Returns weight that every edge from the source point has. */
141  double
142  getSourceWeight () const;
143 
144  /** \brief Allows to set weight for source edges. Every edge that comes from the source point will have that weight.
145  * \param[in] weight new weight
146  */
147  void
148  setSourceWeight (double weight);
149 
150  /** \brief Returns search method that is used for finding KNN.
151  * The graph is build such way that it contains the edges that connect point and its KNN.
152  */
153  KdTreePtr
154  getSearchMethod () const;
155 
156  /** \brief Allows to set search method for finding KNN.
157  * The graph is build such way that it contains the edges that connect point and its KNN.
158  * \param[in] search search method that will be used for finding KNN.
159  */
160  void
161  setSearchMethod (const KdTreePtr& tree);
162 
163  /** \brief Returns the number of neighbours to find. */
164  unsigned int
165  getNumberOfNeighbours () const;
166 
167  /** \brief Allows to set the number of neighbours to find.
168  * \param[in] number_of_neighbours new number of neighbours
169  */
170  void
171  setNumberOfNeighbours (unsigned int neighbour_number);
172 
173  /** \brief Returns the points that must belong to foreground. */
174  std::vector<PointT, Eigen::aligned_allocator<PointT> >
175  getForegroundPoints () const;
176 
177  /** \brief Allows to specify points which are known to be the points of the object.
178  * \param[in] foreground_points point cloud that contains foreground points. At least one point must be specified.
179  */
180  void
181  setForegroundPoints (typename pcl::PointCloud<PointT>::Ptr foreground_points);
182 
183  /** \brief Returns the points that must belong to background. */
184  std::vector<PointT, Eigen::aligned_allocator<PointT> >
185  getBackgroundPoints () const;
186 
187  /** \brief Allows to specify points which are known to be the points of the background.
188  * \param[in] background_points point cloud that contains background points.
189  */
190  void
191  setBackgroundPoints (typename pcl::PointCloud<PointT>::Ptr background_points);
192 
193  /** \brief This method launches the segmentation algorithm and returns the clusters that were
194  * obtained during the segmentation. The indices of points that belong to the object will be stored
195  * in the cluster with index 1, other indices will be stored in the cluster with index 0.
196  * \param[out] clusters clusters that were obtained. Each cluster is an array of point indices.
197  */
198  void
199  extract (std::vector <pcl::PointIndices>& clusters);
200 
201  /** \brief Returns that flow value that was calculated during the segmentation. */
202  double
203  getMaxFlow () const;
204 
205  /** \brief Returns the graph that was build for finding the minimum cut. */
206  typename boost::shared_ptr<typename pcl::MinCutSegmentation<PointT>::mGraph>
207  getGraph () const;
208 
209  /** \brief Returns the colored cloud. Points that belong to the object have the same color. */
211  getColoredCloud ();
212 
213  protected:
214 
215  /** \brief This method simply builds the graph that will be used during the segmentation. */
216  bool
217  buildGraph ();
218 
219  /** \brief Returns unary potential(data cost) for the given point index.
220  * In other words it calculates weights for (source, point) and (point, sink) edges.
221  * \param[in] point index of the point for which weights will be calculated
222  * \param[out] source_weight calculated weight for the (source, point) edge
223  * \param[out] sink_weight calculated weight for the (point, sink) edge
224  */
225  void
226  calculateUnaryPotential (int point, double& source_weight, double& sink_weight) const;
227 
228  /** \brief This method simply adds the edge from the source point to the target point with a given weight.
229  * \param[in] source index of the source point of the edge
230  * \param[in] target index of the target point of the edge
231  * \param[in] weight weight that will be assigned to the (source, target) edge
232  */
233  bool
234  addEdge (int source, int target, double weight);
235 
236  /** \brief Returns the binary potential(smooth cost) for the given indices of points.
237  * In other words it returns weight that must be assigned to the edge from source to target point.
238  * \param[in] source index of the source point of the edge
239  * \param[in] target index of the target point of the edge
240  */
241  double
242  calculateBinaryPotential (int source, int target) const;
243 
244  /** \brief This method recalculates unary potentials(data cost) if some changes were made, instead of creating new graph. */
245  bool
246  recalculateUnaryPotentials ();
247 
248  /** \brief This method recalculates binary potentials(smooth cost) if some changes were made, instead of creating new graph. */
249  bool
250  recalculateBinaryPotentials ();
251 
252  /** \brief This method analyzes the residual network and assigns a label to every point in the cloud.
253  * \param[in] residual_capacity residual network that was obtained during the segmentation
254  */
255  void
256  assembleLabels (ResidualCapacityMap& residual_capacity);
257 
258  protected:
259 
260  /** \brief Stores the sigma coefficient. It is used for finding smooth costs. More information can be found in the article. */
261  double inverse_sigma_;
262 
263  /** \brief Signalizes if the binary potentials are valid. */
264  bool binary_potentials_are_valid_;
265 
266  /** \brief Used for comparison of the floating point numbers. */
267  double epsilon_;
268 
269  /** \brief Stores the distance to the background. */
270  double radius_;
271 
272  /** \brief Signalizes if the unary potentials are valid. */
273  bool unary_potentials_are_valid_;
274 
275  /** \brief Stores the weight for every edge that comes from source point. */
276  double source_weight_;
277 
278  /** \brief Stores the search method that will be used for finding K nearest neighbors. Neighbours are used for building the graph. */
279  KdTreePtr search_;
280 
281  /** \brief Stores the number of neighbors to find. */
282  unsigned int number_of_neighbours_;
283 
284  /** \brief Signalizes if the graph is valid. */
285  bool graph_is_valid_;
286 
287  /** \brief Stores the points that are known to be in the foreground. */
288  std::vector<PointT, Eigen::aligned_allocator<PointT> > foreground_points_;
289 
290  /** \brief Stores the points that are known to be in the background. */
291  std::vector<PointT, Eigen::aligned_allocator<PointT> > background_points_;
292 
293  /** \brief After the segmentation it will contain the segments. */
294  std::vector <pcl::PointIndices> clusters_;
295 
296  /** \brief Stores the graph for finding the maximum flow. */
297  boost::shared_ptr<mGraph> graph_;
298 
299  /** \brief Stores the capacity of every edge in the graph. */
300  boost::shared_ptr<CapacityMap> capacity_;
301 
302  /** \brief Stores reverse edges for every edge in the graph. */
303  boost::shared_ptr<ReverseEdgeMap> reverse_edges_;
304 
305  /** \brief Stores the vertices of the graph. */
306  std::vector< VertexDescriptor > vertices_;
307 
308  /** \brief Stores the information about the edges that were added to the graph. It is used to avoid the duplicate edges. */
309  std::vector< std::set<int> > edge_marker_;
310 
311  /** \brief Stores the vertex that serves as source. */
312  VertexDescriptor source_;
313 
314  /** \brief Stores the vertex that serves as sink. */
315  VertexDescriptor sink_;
316 
317  /** \brief Stores the maximum flow value that was calculated during the segmentation. */
318  double max_flow_;
319 
320  public:
321  EIGEN_MAKE_ALIGNED_OPERATOR_NEW
322  };
323 }
324 
325 #ifdef PCL_NO_PRECOMPILE
326 #include <pcl/segmentation/impl/min_cut_segmentation.hpp>
327 #endif
328 
329 #endif
330 #endif
boost::shared_ptr< const PointCloud< PointT > > ConstPtr
Definition: point_cloud.h:429
boost::shared_ptr< PointCloud< PointT > > Ptr
Definition: point_cloud.h:428
boost::shared_ptr< KdTree< PointT > > Ptr
Definition: kdtree.h:71
PCL base class.
Definition: pcl_base.h:68
Generic search class.
Definition: search.h:74