$extrastylesheet
Olena  User documentation 2.1
An Image Processing Platform
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
region_adjacency_graph.hh
1 // Copyright (C) 2009, 2011 EPITA Research and Development Laboratory (LRDE)
2 //
3 // This file is part of Olena.
4 //
5 // Olena is free software: you can redistribute it and/or modify it under
6 // the terms of the GNU General Public License as published by the Free
7 // Software Foundation, version 2 of the License.
8 //
9 // Olena is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 // General Public License for more details.
13 //
14 // You should have received a copy of the GNU General Public License
15 // along with Olena. If not, see <http://www.gnu.org/licenses/>.
16 //
17 // As a special exception, you may use this file as part of a free
18 // software project without restriction. Specifically, if other files
19 // instantiate templates or use macros or inline functions from this
20 // file, or you compile this file and link it with other files to produce
21 // an executable, this file does not by itself cause the resulting
22 // executable to be covered by the GNU General Public License. This
23 // exception does not however invalidate any other reasons why the
24 // executable file might be covered by the GNU General Public License.
25 
26 #ifndef MLN_MAKE_REGION_ADJACENCY_GRAPH_HH
27 # define MLN_MAKE_REGION_ADJACENCY_GRAPH_HH
28 
36 
37 # include <mln/core/concept/image.hh>
38 # include <mln/core/concept/neighborhood.hh>
39 # include <mln/core/image/image2d.hh>
40 # include <mln/core/alias/box2d.hh>
41 # include <mln/extension/adjust_fill.hh>
42 # include <mln/util/graph.hh>
43 # include <mln/util/adjacency_matrix.hh>
44 
45 
46 namespace mln
47 {
48 
49  namespace make
50  {
51 
59  template <typename I, typename N>
60  util::graph
61  region_adjacency_graph(const Image<I>& wshd_,
62  const Neighborhood<N>& nbh,
63  const mln_value(I)& nbasins);
64 
65 
66 
67 # ifndef MLN_INCLUDE_ONLY
68 
69 
70  namespace internal
71  {
72 
73  template <typename I, typename N>
74  void
75  region_adjacency_graph_tests(const Image<I>& wshd,
76  const Neighborhood<N>& nbh,
77  const mln_value(I)&)
78  {
79  mln_precondition(exact(wshd).is_valid());
80  mln_precondition(exact(nbh).is_valid());
81  (void) wshd;
82  (void) nbh;
83  }
84 
85  } // end of namespace mln::make::internal
86 
87 
88  namespace impl
89  {
90 
91  namespace generic
92  {
93 
94  template <typename I, typename N>
95  util::graph
96  region_adjacency_graph(const Image<I>& wshd_,
97  const Neighborhood<N>& nbh_,
98  const mln_value(I)& nbasins)
99  {
100  mln_trace("make::impl::generic::region_adjacency_graph");
101 
102  internal::region_adjacency_graph_tests(wshd_, nbh_, nbasins);
103  const I& wshd = exact(wshd_);
104  const N& nbh = exact(nbh_);
105 
106  util::adjacency_matrix<> adj(nbasins.next());
107  extension::adjust_fill(wshd, nbh, 0u);
108 
109  typedef mln_value(I) L;
110  L l1, l2;
111  mln_piter(I) p(wshd.domain());
112  mln_niter(N) n(nbh, p);
113  for_all(p)
114  {
115  if (wshd(p) != 0u)
116  continue;
117  // p is in the watershed line.
118  l1 = l2 = 0;
119  for_all(n)
120  if (wshd.has(n) && wshd(n) != 0u)
121  {
122  if (l1 == 0u) // First label to be stored.
123  l1 = wshd(n);
124  else
125  if (wshd(n) != l1) // Useless: && l2 == 0)
126  { // Second label to be stored.
127  mln_invariant(l2 == 0u);
128  l2 = wshd(n);
129  break;
130  }
131  }
132  if (l2 == 0u || l1 == 0u)
133  continue;
134 
135  // adjacency l1 l2
136  adj.add(l2, l1);
137  }
138 
139  // Construct graph.
140  util::graph g;
141  g.add_vertices(nbasins.next());
142  for (unsigned i = 1; i < nbasins.next(); ++i)
143  for (unsigned j = 1; j < i; ++j)
144  if (adj.are_adjacent(i, j))
145  g.add_edge(i, j);
146 
147 
148  return g;
149  }
150 
151  } // end of namespace mln::make::impl::generic
152 
153  } // end of namespace mln::make::impl
154 
155 
156 
157  namespace internal
158  {
159 
160  template <typename I, typename N>
161  util::graph
162  region_adjacency_graph_dispatch(const Image<I>& wshd,
163  const Neighborhood<N>& nbh,
164  const mln_value(I)& nbasins)
165  {
166  return make::impl::generic::region_adjacency_graph(wshd, nbh, nbasins);
167  }
168 
169  } // end of namespace mln::make::internal
170 
171 
172 
173  // Facade
174 
175  template <typename I, typename N>
176  inline
177  util::graph
178  region_adjacency_graph(const Image<I>& wshd,
179  const Neighborhood<N>& nbh,
180  const mln_value(I)& nbasins)
181  {
182  mln_trace("make::region_adjacency_graph");
183 
184  internal::region_adjacency_graph_tests(wshd, nbh, nbasins);
185 
186  util::graph g = internal::region_adjacency_graph_dispatch(wshd, nbh, nbasins);
187 
188  return g;
189  }
190 
191 
192 # endif // ! MLN_INCLUDE_ONLY
193 
194 
195  } // end of namespace mln::make
196 
197 } // end of namespace mln
198 
199 
200 #endif // ! MLN_MAKE_REGION_ADJACENCY_GRAPH_HH