$extrastylesheet
Olena  User documentation 2.1
An Image Processing Platform
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
canvas.cc
1 // Copyright (C) 2008 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 
29 
30 #include <mln/core/image/image2d.hh>
31 #include <mln/core/alias/neighb2d.hh>
32 #include <mln/value/int_u8.hh>
33 #include <mln/io/pgm/load.hh>
34 
35 #include <mln/labeling/level.hh>
36 #include <mln/util/timer.hh>
37 
38 
39 namespace mln
40 {
41 
42 
43  namespace old_canvas
44  {
45 
46  template <typename F>
47  struct labeling
48  {
49  // Functor.
50  F& f;
51 
52  typedef typename F::I I;
53  typedef typename F::N N;
54  typedef typename F::L L;
55  typedef typename F::S S;
56 
57  // Local type.
58  typedef mln_psite(I) psite;
59 
60  // Auxiliary data.
61  mln_ch_value(I, bool) deja_vu;
62  mln_ch_value(I, psite) parent;
63 
64  // Output.
65  mln_ch_value(I, L) output;
66  L nlabels;
67  bool status;
68 
69  // Ctor.
70  labeling(F& f);
71 
72  void init();
73 
74  void pass_1();
75 
76  void pass_2();
77 
78 
79  // Auxiliary methods.
80 
81  void make_set(const psite& p);
82 
83  bool is_root(const psite& p) const;
84 
85  psite find_root(const psite& x);
86 
87  void do_union(const psite& n, const psite& p);
88 
89  };
90 
91 
92  template <typename F>
94  : f(f)
95  {
96  mln_trace("canvas::labeling");
97 
98  init();
99  f.init(); // Client initialization.
100  pass_1();
101  pass_2();
102 
103  }
104 
105  template <typename F>
106  void
107  labeling<F>::init()
108  {
109  initialize(deja_vu, f.input);
110  mln::data::fill(deja_vu, false);
111  initialize(parent, f.input);
112  initialize(output, f.input);
113  mln::data::fill(output, L(literal::zero));
114  nlabels = 0;
115  }
116 
117  template <typename F>
118  void
119  labeling<F>::pass_1()
120  {
121  mln_fwd_piter(S) p(f.s);
122  mln_niter(N) n(f.nbh, p);
123  for_all(p) if (f.handles(p))
124  {
125  make_set(p);
126  for_all(n)
127  if (f.input.domain().has(n) && deja_vu(n))
128  {
129  if (f.equiv(n, p))
130  do_union(n, p);
131  else
132  f.do_no_union(n, p);
133  }
134  deja_vu(p) = true;
135  }
136  }
137 
138  template <typename F>
139  void
140  labeling<F>::pass_2()
141  {
142  mln_bkd_piter(S) p(f.s);
143  for_all(p) if (f.handles(p))
144  {
145  if (is_root(p))
146  {
147  if (f.labels(p))
148  {
149  if (nlabels == mln_max(L))
150  {
151  status = false;
152  return;
153  }
154  output(p) = ++nlabels;
155  }
156  }
157  else
158  output(p) = output(parent(p));
159  }
160  status = true;
161  }
162 
163  template <typename F>
164  void
165  labeling<F>::make_set(const psite& p)
166  {
167  parent(p) = p;
168  f.init_attr(p);
169  }
170 
171  template <typename F>
172  bool
173  labeling<F>::is_root(const psite& p) const
174  {
175  return parent(p) == p;
176  }
177 
178  template <typename F>
179  typename labeling<F>::psite
180  labeling<F>::find_root(const psite& x)
181  {
182  if (parent(x) == x)
183  return x;
184  else
185  return parent(x) = find_root(parent(x));
186  }
187 
188  template <typename F>
189  void
190  labeling<F>::do_union(const psite& n, const psite& p)
191  {
192  psite r = find_root(n);
193  if (r != p)
194  {
195  parent(r) = p;
196  f.merge_attr(r, p);
197  }
198  }
199 
200 
201  } // end of namespace mln::old_canvas
202 
203 
204 
205  namespace old_labeling
206  {
207 
208 
209  template <typename I_, typename N_, typename L_>
210  struct level_functor
211  {
212  typedef mln_psite(I_) P;
213 
214  // requirements from mln::canvas::labeling:
215 
216  typedef I_ I;
217  typedef N_ N;
218  typedef L_ L;
219  typedef mln_pset(I) S;
220 
221  const I& input;
222  const N& nbh;
223  const S& s;
224 
225  bool handles(const P& p) const { return input(p) == val; }
226  bool equiv(const P& n, const P&) const { return input(n) == val; }
227 
228  void init() {}
229  bool labels(const P&) const { return true; }
230  void do_no_union(const P&, const P&) {}
231  void init_attr(const P&) {}
232  void merge_attr(const P&, const P&) {}
233 
234  // end of requirements
235 
236  const mln_value(I_)& val;
237 
238  level_functor(const I& input, const mln_value(I)& val, const N& nbh)
239  : input(input),
240  nbh(nbh),
241  s(input.domain()),
242  val(val)
243  {}
244  };
245 
246 
247 
248  template <typename I, typename N, typename L>
249  mln_ch_value(I, L)
250  level(const Image<I>& input, const mln_value(I)& val, const Neighborhood<N>& nbh,
251  L& nlabels)
252  {
253  mln_trace("labeling::value");
254 
255  typedef level_functor<I,N,L> F;
256  F f(exact(input), val, exact(nbh));
257  old_canvas::labeling<F> run(f);
258 
259  nlabels = run.nlabels;
260 
261  return run.output;
262  }
263 
264  } // end of namespace mln::old_labeling
265 
266 } // end of namespace mln
267 
268 
269 
270 
271 int main()
272 {
273  using namespace mln;
274  using value::int_u8;
275 
276  image2d<int_u8> lena;
277  io::pgm::load(lena, "../../img/lena.pgm");
278 
279  {
280  util::timer t;
281  t.start();
282  unsigned n;
283  for (unsigned l = 0; l <= 255; ++l)
284  old_labeling::value(lena, l, c4(), n);
285  std::cout << "canvas as class: " << t.read() << std::endl;
286  }
287 
288  {
289  util::timer t;
290  t.start();
291  unsigned n;
292  for (unsigned l = 0; l <= 255; ++l)
293  labeling::impl::generic::data(lena, l, c4(), n);
294  std::cout << "canvas as proc.: " << t.read() << std::endl;
295  }
296 
297 }