$extrastylesheet
Olena  User documentation 2.1
An Image Processing Platform
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
rag_and_labeled_wsl.hh
1 // Copyright (C) 2009 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_RAG_AND_LABELED_WSL_HH
27 # define MLN_MAKE_RAG_AND_LABELED_WSL_HH
28 
37 
38 # include <map>
39 
40 # include <mln/core/concept/image.hh>
41 # include <mln/core/concept/neighborhood.hh>
42 # include <mln/core/image/image2d.hh>
43 # include <mln/core/alias/box2d.hh>
44 # include <mln/extension/adjust_fill.hh>
45 # include <mln/util/graph.hh>
46 # include <mln/util/couple.hh>
47 
48 
49 namespace mln
50 {
51 
52  namespace make
53  {
54 
64 
91  template <typename I, typename N>
92  util::couple<util::graph,mln_concrete(I)>
93  rag_and_labeled_wsl(const Image<I>& wshd_,
94  const Neighborhood<N>& nbh_,
95  const mln_value(I)& nbasins);
96 
97 
98 
99 # ifndef MLN_INCLUDE_ONLY
100 
101 
102  namespace internal
103  {
104 
105  template <typename I, typename N>
106  inline
107  void
108  rag_and_labeled_wsl_tests(const Image<I>& wshd,
109  const Neighborhood<N>& nbh,
110  const mln_value(I)&)
111  {
112  mln_precondition(exact(wshd).is_valid());
113  mln_precondition(exact(nbh).is_valid());
114  (void) wshd;
115  (void) nbh;
116  }
117 
118  } // end of namespace mln::make::internal
119 
120 
121  namespace impl
122  {
123 
124  namespace generic
125  {
126 
127  template <typename I, typename N>
128  inline
129  util::couple<util::graph,mln_concrete(I)>
130  rag_and_labeled_wsl(const Image<I>& wshd_,
131  const Neighborhood<N>& nbh_,
132  const mln_value(I)& nbasins)
133  {
134  mln_trace("make::impl::generic::rag_and_labeled_wsl");
135 
136  internal::rag_and_labeled_wsl_tests(wshd_, nbh_, nbasins);
137  const I& wshd = exact(wshd_);
138  const N& nbh = exact(nbh_);
139  typedef mln_value(I) L;
140 
141  extension::adjust_fill(wshd, nbh, 0u);
142 
143  mln_concrete(I) output(wshd.domain());
144 
145  // FIXME: we would like to not use mln_value() but 0 instead.
146  // We can't do that because graph edges are numbered from 0.
147  data::fill(output, mln_max(mln_value(I)).prev());
148 
149  std::map<util::couple<L,L>, mln_value(I)> c2la;
150  util::array<util::couple<L,L> > la2c;
151  mln_value(I) nlabels = literal::zero;
152 
153  L l1, l2;
154  mln_piter(I) p(wshd.domain());
155  mln_niter(N) n(nbh, p);
156  for_all(p)
157  {
158  if (wshd(p) != 0u)
159  continue;
160  // p is in the watershed line.
161  l1 = l2 = 0;
162  for_all(n)
163  if (wshd.has(n) && wshd(n) != 0u)
164  {
165  if (l1 == 0u) // First label to be stored.
166  l1 = wshd(n);
167  else
168  if (wshd(n) != l1) // Useless: && l2 == 0)
169  { // Second label to be stored.
170  mln_invariant(l2 == 0u);
171  l2 = wshd(n);
172  break;
173  }
174  }
175  if (l2 == 0u || l1 == 0u)
176  continue;
177  if (l2 < l1)
178  std::swap(l1, l2);
179 
180  // adjacency l1 l2
181  util::couple<L,L> c = make::couple(l2,l1);
182  if (c2la.find(c) == c2la.end())
183  {
184  c2la[c] = nlabels++;
185  la2c.append(c);
186  }
187  output(p) = c2la[c];
188 
189  }
190 
191  // Construct graph.
192  util::graph g;
193  g.add_vertices(nbasins.next());
194  for (unsigned i = 0; i < la2c.nelements(); ++i)
195  g.add_edge(la2c[i].first(), la2c[i].second());
196 
197  return make::couple(g, output);
198  }
199 
200  } // end of namespace mln::make::impl::generic
201 
202  } // end of namespace mln::make::impl
203 
204 
205 
206  namespace internal
207  {
208 
209  template <typename I, typename N>
210  inline
211  util::couple<util::graph,mln_concrete(I)>
212  rag_and_labeled_wsl_dispatch(const Image<I>& wshd,
213  const Neighborhood<N>& nbh,
214  const mln_value(I)& nbasins)
215  {
216  return make::impl::generic::rag_and_labeled_wsl(wshd, nbh, nbasins);
217  }
218 
219  } // end of namespace mln::make::internal
220 
221 
222 
223  // Facade
224 
225  template <typename I, typename N>
226  inline
227  util::couple<util::graph,mln_concrete(I)>
228  rag_and_labeled_wsl(const Image<I>& wshd,
229  const Neighborhood<N>& nbh,
230  const mln_value(I)& nbasins)
231  {
232  mln_trace("make::rag_and_labeled_wsl");
233 
234  internal::rag_and_labeled_wsl_tests(wshd, nbh, nbasins);
235 
236  util::couple<util::graph,mln_concrete(I)>
237  result = internal::rag_and_labeled_wsl_dispatch(wshd, nbh, nbasins);
238 
239  return result;
240  }
241 
242 
243 # endif // ! MLN_INCLUDE_ONLY
244 
245 
246  } // end of namespace mln::make
247 
248 } // end of namespace mln
249 
250 
251 #endif // ! MLN_MAKE_RAG_AND_LABELED_WSL_HH