Point Cloud Library (PCL)  1.8.0
random_walker.h
1 /*
2  * Software License Agreement (BSD License)
3  *
4  * Point Cloud Library (PCL) - www.pointclouds.org
5  * Copyright (c) 2012-, Open Perception, Inc.
6  *
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  *
13  * * Redistributions of source code must retain the above copyright
14  * notice, this list of conditions and the following disclaimer.
15  * * Redistributions in binary form must reproduce the above
16  * copyright notice, this list of conditions and the following
17  * disclaimer in the documentation and/or other materials provided
18  * with the distribution.
19  * * Neither the name of Willow Garage, Inc. nor the names of its
20  * contributors may be used to endorse or promote products derived
21  * from this software without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
26  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
27  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
28  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
29  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
30  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
31  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
33  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
34  * POSSIBILITY OF SUCH DAMAGE.
35  *
36  */
37 
38 #ifndef PCL_SEGMENTATION_RANDOM_WALKER_H
39 #define PCL_SEGMENTATION_RANDOM_WALKER_H
40 
41 #include <boost/graph/adjacency_list.hpp>
42 #include <boost/graph/graph_concepts.hpp>
43 #include <boost/concept/assert.hpp>
44 
45 #include <Eigen/Dense>
46 
47 namespace pcl
48 {
49 
50  namespace segmentation
51  {
52 
53  /** \brief Multilabel graph segmentation using random walks.
54  *
55  * This is an implementation of the algorithm described in "Random Walks
56  * for Image Segmentation" by Leo Grady.
57  *
58  * Given a weighted undirected graph and a small number of user-defined
59  * labels this algorithm analytically determines the probability that a
60  * random walker starting at each unlabeled vertex will first reach one
61  * of the prelabeled vertices. The unlabeled vertices are then assigned
62  * to the label for which the greatest probability is calculated.
63  *
64  * The input is a BGL graph, a property map that associates a weight to
65  * each edge of the graph, and a property map that contains initial
66  * vertex colors (the term "color" is used interchangeably with "label").
67  *
68  * \note The colors of unlabeled vertices should be set to 0, the colors
69  * of labeled vetices could be any positive numbers.
70  *
71  * \note This is the responsibility of the user to make sure that every
72  * connected component of the graph has at least one colored vertex. If
73  * the user failed to do so, then the behavior of the algorithm is
74  * undefined, i.e. it may or may not succeed, and also may or may not
75  * report failure.
76  *
77  * The output of the algorithm (i.e. label assignment) is written back
78  * to the color map.
79  *
80  * \param[in] graph an undirected graph with internal edge weight and
81  * vertex color property maps
82  *
83  * Several overloads of randomWalker() function are provided for
84  * convenience.
85  *
86  * \sa randomWalker(Graph&, EdgeWeightMap, VertexColorMap)
87  * \sa randomWalker(Graph&, EdgeWeightMap, VertexColorMap, Eigen::Matrix <typename boost::property_traits<EdgeWeightMap>::value_type, Eigen::Dynamic, Eigen::Dynamic>&, std::map<typename boost::property_traits <VertexColorMap>::value_type, size_t>&)
88  *
89  * \author Sergey Alexandrov
90  * \ingroup segmentation
91  */
92 
93  template <class Graph> bool
94  randomWalker (Graph& graph);
95 
96  /** \brief Multilabel graph segmentation using random walks.
97  *
98  * This is an overloaded function provided for convenience. See the
99  * documentation for randomWalker().
100  *
101  * \param[in] graph an undirected graph
102  * \param[in] weights an external edge weight property map
103  * \param[in,out] colors an external vertex color property map
104  *
105  * \author Sergey Alexandrov
106  * \ingroup segmentation
107  */
108  template <class Graph, class EdgeWeightMap, class VertexColorMap> bool
109  randomWalker (Graph& graph,
110  EdgeWeightMap weights,
111  VertexColorMap colors);
112 
113  /** \brief Multilabel graph segmentation using random walks.
114  *
115  * This is an overloaded function provided for convenience. See the
116  * documentation for randomWalker().
117  *
118  * \param[in] graph an undirected graph
119  * \param[in] weights an external edge weight property map
120  * \param[in,out] colors an external vertex color property map
121  * \param[out] potentials a matrix with calculated probabilities,
122  * where rows correspond to vertices, and columns
123  * correspond to colors
124  * \param[out] colors_to_columns_map a mapping between colors and
125  * columns in \a potentials matrix
126  *
127  * \author Sergey Alexandrov
128  * \ingroup segmentation
129  */
130  template <class Graph, class EdgeWeightMap, class VertexColorMap> bool
131  randomWalker (Graph& graph,
132  EdgeWeightMap weights,
133  VertexColorMap colors,
134  Eigen::Matrix<typename boost::property_traits<EdgeWeightMap>::value_type, Eigen::Dynamic, Eigen::Dynamic>& potentials,
135  std::map<typename boost::property_traits<VertexColorMap>::value_type, size_t>& colors_to_columns_map);
136 
137  }
138 
139 }
140 
141 #include <pcl/segmentation/impl/random_walker.hpp>
142 
143 #endif /* PCL_SEGMENTATION_RANDOM_WALKER_H */
144 
bool randomWalker(Graph &graph)
Multilabel graph segmentation using random walks.