Milena (Olena)  User documentation 2.0a Id
 All Classes Namespaces Functions Variables Typedefs Enumerator 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>
93  labeling<F>::labeling(F& f)
94  : f(f)
95  {
96  trace::entering("canvas::labeling");
97 
98  init();
99  f.init(); // Client initialization.
100  pass_1();
101  pass_2();
102 
103  trace::exiting("canvas::labeling");
104  }
105 
106  template <typename F>
107  void
108  labeling<F>::init()
109  {
110  initialize(deja_vu, f.input);
111  mln::data::fill(deja_vu, false);
112  initialize(parent, f.input);
113  initialize(output, f.input);
114  mln::data::fill(output, L(literal::zero));
115  nlabels = 0;
116  }
117 
118  template <typename F>
119  void
120  labeling<F>::pass_1()
121  {
122  mln_fwd_piter(S) p(f.s);
123  mln_niter(N) n(f.nbh, p);
124  for_all(p) if (f.handles(p))
125  {
126  make_set(p);
127  for_all(n)
128  if (f.input.domain().has(n) && deja_vu(n))
129  {
130  if (f.equiv(n, p))
131  do_union(n, p);
132  else
133  f.do_no_union(n, p);
134  }
135  deja_vu(p) = true;
136  }
137  }
138 
139  template <typename F>
140  void
141  labeling<F>::pass_2()
142  {
143  mln_bkd_piter(S) p(f.s);
144  for_all(p) if (f.handles(p))
145  {
146  if (is_root(p))
147  {
148  if (f.labels(p))
149  {
150  if (nlabels == mln_max(L))
151  {
152  status = false;
153  return;
154  }
155  output(p) = ++nlabels;
156  }
157  }
158  else
159  output(p) = output(parent(p));
160  }
161  status = true;
162  }
163 
164  template <typename F>
165  void
166  labeling<F>::make_set(const psite& p)
167  {
168  parent(p) = p;
169  f.init_attr(p);
170  }
171 
172  template <typename F>
173  bool
174  labeling<F>::is_root(const psite& p) const
175  {
176  return parent(p) == p;
177  }
178 
179  template <typename F>
180  typename labeling<F>::psite
181  labeling<F>::find_root(const psite& x)
182  {
183  if (parent(x) == x)
184  return x;
185  else
186  return parent(x) = find_root(parent(x));
187  }
188 
189  template <typename F>
190  void
191  labeling<F>::do_union(const psite& n, const psite& p)
192  {
193  psite r = find_root(n);
194  if (r != p)
195  {
196  parent(r) = p;
197  f.merge_attr(r, p);
198  }
199  }
200 
201 
202  } // end of namespace mln::old_canvas
203 
204 
205 
206  namespace old_labeling
207  {
208 
209 
210  template <typename I_, typename N_, typename L_>
211  struct level_functor
212  {
213  typedef mln_psite(I_) P;
214 
215  // requirements from mln::canvas::labeling:
216 
217  typedef I_ I;
218  typedef N_ N;
219  typedef L_ L;
220  typedef mln_pset(I) S;
221 
222  const I& input;
223  const N& nbh;
224  const S& s;
225 
226  bool handles(const P& p) const { return input(p) == val; }
227  bool equiv(const P& n, const P&) const { return input(n) == val; }
228 
229  void init() {}
230  bool labels(const P&) const { return true; }
231  void do_no_union(const P&, const P&) {}
232  void init_attr(const P&) {}
233  void merge_attr(const P&, const P&) {}
234 
235  // end of requirements
236 
237  const mln_value(I_)& val;
238 
239  level_functor(const I& input, const mln_value(I)& val, const N& nbh)
240  : input(input),
241  nbh(nbh),
242  s(input.domain()),
243  val(val)
244  {}
245  };
246 
247 
248 
249  template <typename I, typename N, typename L>
250  mln_ch_value(I, L)
251  level(const Image<I>& input, const mln_value(I)& val, const Neighborhood<N>& nbh,
252  L& nlabels)
253  {
254  trace::entering("labeling::value");
255 
256  typedef level_functor<I,N,L> F;
257  F f(exact(input), val, exact(nbh));
258  old_canvas::labeling<F> run(f);
259 
260  nlabels = run.nlabels;
261 
262  trace::exiting("labeling::value");
263  return run.output;
264  }
265 
266  } // end of namespace mln::old_labeling
267 
268 } // end of namespace mln
269 
270 
271 
272 
273 int main()
274 {
275  using namespace mln;
276  using value::int_u8;
277 
278  image2d<int_u8> lena;
279  io::pgm::load(lena, "../../img/lena.pgm");
280 
281  {
282  util::timer t;
283  t.start();
284  unsigned n;
285  for (unsigned l = 0; l <= 255; ++l)
286  old_labeling::value(lena, l, c4(), n);
287  std::cout << "canvas as class: " << t.read() << std::endl;
288  }
289 
290  {
291  util::timer t;
292  t.start();
293  unsigned n;
294  for (unsigned l = 0; l <= 255; ++l)
295  labeling::impl::generic::data(lena, l, c4(), n);
296  std::cout << "canvas as proc.: " << t.read() << std::endl;
297  }
298 
299 }