36 #ifndef VIGRA_MULTI_LABELING_HXX
37 #define VIGRA_MULTI_LABELING_HXX
39 #include "multi_array.hxx"
40 #include "multi_gridgraph.hxx"
41 #include "union_find.hxx"
49 namespace lemon_graph {
51 template <
class Graph,
class T1Map,
class T2Map,
class Equal>
52 typename T2Map::value_type
53 labelGraph(Graph
const & g,
58 typedef typename Graph::NodeIt graph_scanner;
59 typedef typename Graph::OutBackArcIt neighbor_iterator;
60 typedef typename T2Map::value_type LabelType;
62 vigra::detail::UnionFindArray<LabelType> regions;
65 for (graph_scanner node(g); node != INVALID; ++node)
67 typename T1Map::value_type center = data[*node];
70 LabelType currentLabel = regions.nextFreeLabel();
72 for (neighbor_iterator arc(g, node); arc != INVALID; ++arc)
75 if(equal(center, data[g.target(*arc)]))
77 LabelType neighborLabel = regions[labels[g.target(*arc)]];
78 currentLabel = regions.makeUnion(neighborLabel, currentLabel);
82 labels[*node] = regions.finalizeLabel(currentLabel);
85 LabelType count = regions.makeContiguous();
88 for (graph_scanner node(g); node != INVALID; ++node)
90 labels[*node] = regions[labels[*node]];
95 template <
class Graph,
class T1Map,
class T2Map,
class Equal>
96 typename T2Map::value_type
97 labelGraphWithBackground(Graph
const & g,
100 typename T1Map::value_type backgroundValue,
103 typedef typename Graph::NodeIt graph_scanner;
104 typedef typename Graph::OutBackArcIt neighbor_iterator;
105 typedef typename T2Map::value_type LabelType;
107 vigra::detail::UnionFindArray<LabelType> regions;
110 for (graph_scanner node(g); node != INVALID; ++node)
112 typename T1Map::value_type center = data[*node];
115 if(equal(center, backgroundValue))
122 LabelType currentLabel = regions.nextFreeLabel();
124 for (neighbor_iterator arc(g, node); arc != INVALID; ++arc)
127 if(equal(center, data[g.target(*arc)]))
129 LabelType neighborLabel = regions[labels[g.target(*arc)]];
130 currentLabel = regions.makeUnion(neighborLabel, currentLabel);
134 labels[*node] = regions.finalizeLabel(currentLabel);
137 LabelType count = regions.makeContiguous();
140 for (graph_scanner node(g); node != INVALID; ++node)
142 labels[*node] = regions[labels[*node]];
217 doxygen_overloaded_function(template <...>
unsigned int labelMultiArray)
219 template <
unsigned int N,
class T,
class S1,
220 class Label,
class S2,
224 MultiArrayView<N, Label, S2> labels,
228 vigra_precondition(data.shape() == labels.shape(),
229 "labelMultiArray(): shape mismatch between input and output.");
231 GridGraph<N, undirected_tag> graph(data.shape(), neighborhood);
232 return lemon_graph::labelGraph(graph, data, labels, equal);
235 template <
unsigned int N,
class T,
class S1,
236 class Label,
class S2>
239 MultiArrayView<N, Label, S2> labels,
242 return labelMultiArray(data, labels, neighborhood, std::equal_to<T>());
312 template <
unsigned int N,
class T,
class S1,
313 class Label,
class S2,
317 MultiArrayView<N, Label, S2> labels,
322 vigra_precondition(data.shape() == labels.shape(),
323 "labelMultiArrayWithBackground(): shape mismatch between input and output.");
325 GridGraph<N, undirected_tag> graph(data.shape(), neighborhood);
326 return lemon_graph::labelGraphWithBackground(graph, data, labels, backgroundValue, equal);
329 template <
unsigned int N,
class T,
class S1,
330 class Label,
class S2>
333 MultiArrayView<N, Label, S2> labels,
335 T backgroundValue = T())
344 #endif //VIGRA_MULTI_LABELING_HXX