$extrastylesheet
Olena  User documentation 2.1
An Image Processing Platform
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
attribute_filter.hh
1 // Copyright (C) 2007, 2008, 2009, 2012 EPITA Research and Development
2 // Laboratory (LRDE)
3 //
4 // This file is part of Olena.
5 //
6 // Olena is free software: you can redistribute it and/or modify it under
7 // the terms of the GNU General Public License as published by the Free
8 // Software Foundation, version 2 of the License.
9 //
10 // Olena is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 // General Public License for more details.
14 //
15 // You should have received a copy of the GNU General Public License
16 // along with Olena. If not, see <http://www.gnu.org/licenses/>.
17 //
18 // As a special exception, you may use this file as part of a free
19 // software project without restriction. Specifically, if other files
20 // instantiate templates or use macros or inline functions from this
21 // file, or you compile this file and link it with other files to produce
22 // an executable, this file does not by itself cause the resulting
23 // executable to be covered by the GNU General Public License. This
24 // exception does not however invalidate any other reasons why the
25 // executable file might be covered by the GNU General Public License.
26 
27 #ifndef MLN_CANVAS_MORPHO_ATTRIBUTE_FILTER_HH
28 # define MLN_CANVAS_MORPHO_ATTRIBUTE_FILTER_HH
29 
33 
34 
35 # include <mln/core/concept/image.hh>
36 # include <mln/core/concept/neighborhood.hh>
37 # include <mln/core/concept/accumulator.hh>
38 
39 # include <mln/data/sort_offsets.hh>
40 # include <mln/trait/accumulators.hh>
41 # include <mln/util/pix.hh>
42 
43 # include <mln/border/get.hh>
44 # include <mln/data/fill.hh>
45 # include <mln/extension/adjust_fill.hh>
46 # include <mln/data/sort_psites.hh>
47 
48 namespace mln
49 {
50 
51  namespace canvas
52  {
53 
54  namespace morpho
55  {
56 
62  template <typename I, typename N, typename A>
63  mln_concrete(I)
64  attribute_filter(const Image<I>& input, const Neighborhood<N>& nbh,
65  const Accumulator<A>& a, const mln_result(A)& lambda,
66  bool increasing);
67 
68 
69 
70 # ifndef MLN_INCLUDE_ONLY
71 
72  namespace impl
73  {
74 
75  template <typename A, typename I>
76  void take_as_init_fastest(trait::accumulator::when_pix::use_none, A& accu,
77  const I& input, const unsigned p)
78  {
79  (void)input;
80  (void)p;
81  accu.take_as_init();
82  }
83 
84  template <typename A, typename I, typename P>
85  void take_as_init(trait::accumulator::when_pix::use_p, A& accu,
86  const I& input, const P& p)
87  {
88  (void)input;
89  accu.take_as_init(p);
90  }
91 
92  template <typename A, typename I, typename P>
93  void take_as_init(trait::accumulator::when_pix::use_none, A& accu,
94  const I& input, const P& p)
95  {
96  (void)input;
97  (void)p;
98  accu.take_as_init();
99  }
100 
101  template <typename A, typename I, typename P>
102  void take_as_init(trait::accumulator::when_pix::use_pix, A& accu,
103  const I& input, const P& p)
104  {
105  accu.take_as_init(make::pix(input, p));
106  }
107 
108  template <typename A, typename I, typename P>
109  void take_as_init(trait::accumulator::when_pix::use_v, A& accu,
110  const I& input, const P& p)
111  {
112  accu.take_as_init(input(p));
113  }
114 
115  template <typename A, typename I>
116  void take_as_init_fastest(trait::accumulator::when_pix::use_v, A& accu,
117  const I& input, const unsigned p)
118  {
119  accu.take_as_init(input.element(p));
120  }
121 
122 
123  template <typename A, typename I, typename P>
124  void take_as_init(A& accu, const I& input, const P& p)
125  {
126  take_as_init(mln_trait_accumulator_when_pix(A)(), accu, input, p);
127  }
128 
129  template <typename A, typename I, typename P>
130  void take_as_init_fastest(A& accu, const I& input, const P& p)
131  {
132  take_as_init_fastest(mln_trait_accumulator_when_pix(A)(), accu, input, p);
133  }
134 
135 
136 
137  namespace generic
138  {
139 
143 
144 
145  template <typename I>
146  static inline
147  mln_psite(I)
148  find_root(I& parent, const mln_psite(I) & x)
149  {
150  if (parent(x) == x)
151  return x;
152  else
153  return parent(x) = find_root(parent, parent(x));
154  }
155 
156  template <typename I, typename N, typename S, typename A>
157  mln_concrete(I)
158  attribute_filter(const Image<I>& input_,
159  const Neighborhood<N>& nbh_,
160  const Site_Set<S>& s_,
161  const Accumulator<A>& a_,
162  const mln_result(A)& lambda)
163  {
164  mln_trace("canvas::morpho::impl::generic::attribute_filter");
165  // FIXME: Test?!
166 
167  const I& input = exact(input_);
168  const N& nbh = exact(nbh_);
169  const S& s = exact(s_);
170  (void)a_; // To avoid warning at compilation
171 
172  mln_concrete(I) output;
173  initialize(output, input);
174 
175  // Local type.
176  typedef mln_psite(I) P;
177 
178 
179  // Auxiliary data.
180  mln_ch_value(I, bool) deja_vu;
181  mln_ch_value(I, bool) activity;
182  mln_ch_value(I, P) parent;
183  mln_ch_value(I, A) data;
184 
185  // Initialization.
186  {
187  initialize(deja_vu, input);
188  data::fill(deja_vu, false);
189  initialize(activity, input);
190  data::fill(activity, true);
191  initialize(parent, input);
192  initialize(data, input);
193  //a.init(); // init required.
194  }
195 
196  // First pass.
197  {
198  mln_fwd_piter(S) p(s); // s required.
199  mln_niter(N) n(nbh, p);
200 
201  for_all(p)
202  {
203  // Make set.
204  {
205  parent(p) = p;
206 
207  // Check accumulator trait to handle argument type (Pix or Site).
208  take_as_init(data(p), input, p);
209  }
210 
211  for_all(n)
212  if (input.domain().has(n) && deja_vu(n))
213  {
214  //do_union(n, p);
215  P r = find_root(parent, n);
216  if (r != p)
217  {
218  if (input(r) == input(p) || (activity(r) && (data(r) < lambda))) // Equiv(r, p)
219  // Either a flat zone or the component of r is still growing.
220  {
221  /* FIXME: Same remark as above concerning the
222  initialization of data(p); instead of
223 
224  data(p).take(data(r));
225 
226  we should (or could) have
227 
228  unite_data(p, r);
229 
230  so as to keep the generic aspect of this canvas
231  (as long as the set of acceptable types for the
232  template parameter A is not bound). */
233 
234  data(p).take(data(r));
235  parent(r) = p;
236  if (activity(r) == false)
237  activity(p) = false;
238  }
239  else
240  {
241  activity(p) = false;
242  }
243  }
244  }
245  deja_vu(p) = true;
246  }
247  }
248 
249  // Second pass.
250  {
251  mln_bkd_piter(S) p(s);
252  for_all(p)
253  if (parent(p) == p) // p is root.
254  output(p) = input(p);
255  else
256  output(p) = output(parent(p));
257  }
258 
259  return output;
260  }
261 
262  } // end of namespace mln::canvas::morpho::impl::generic
263 
264 
268 
269  template <typename I>
270  inline
271  unsigned
272  find_root_fastest(I& parent, unsigned x)
273  {
274  if (parent.element(x) == x)
275  return x;
276  else
277  return parent.element(x) = find_root_fastest(parent, parent.element(x));
278  }
279 
280  template <typename I, typename N, typename A>
281  mln_concrete(I)
282  attribute_filter_fastest(const Image<I>& input_,
283  const Neighborhood<N>& nbh_,
284  const util::array<unsigned>& s,
285  const Accumulator<A>& a_,
286  const mln_result(A)& lambda)
287  {
288  mln_trace("canvas::morpho::impl::attribute_filter_fastest");
289  // FIXME: Tests?
290 
291  const I& input = exact(input_);
292  const N& nbh = exact(nbh_);
293  (void)a_;
294 
295  // The border adaptation is performed in the calling scope since
296  // we want offsets of 's' to match the image structure / dimensions.
297  mln_precondition(border::get(input) >= nbh.delta());
298 
299  mln_concrete(I) output;
300  initialize(output, input);
301 
302  // Local type.
303  typedef mln_psite(I) P;
304 
305  // Auxiliary data.
306  mln_ch_value(I, bool) deja_vu;
307  mln_ch_value(I, bool) activity;
308  mln_ch_value(I, unsigned) parent;
309  mln_ch_value(I, A) data;
310 
311  // Initialization.
312  {
313  initialize(deja_vu, input);
314  data::fill(deja_vu, false);
315  extension::fill(deja_vu, false); // So the border is neutral.
316 
317  initialize(activity, input);
318  data::fill(activity, true);
319  initialize(parent, input);
320  data::fill(parent, 0);
321  initialize(data, input);
322  }
323 
324  util::array<int> dp = offsets_wrt(input, nbh);
325  const unsigned n_nbhs = dp.nelements();
326  const unsigned n_points = s.nelements();
327 
328  // First pass.
329  {
330  for (unsigned i = 0; i < n_points; ++i)
331  {
332  unsigned p = s[i]; // An offset.
333 
334  // Make set.
335  parent.element(p) = p;
336 
337  // Check accumulator trait to handle argument type (Value or None).
338  take_as_init_fastest(data.element(p), input, p);
339 
340  for (unsigned j = 0; j < n_nbhs; ++j)
341  {
342  unsigned n = p + dp[j];
343  if (! deja_vu.element(n))
344  continue;
345 
346  unsigned r = find_root_fastest(parent, n);
347  if (r != p)
348  {
349  if (input.element(r) == input.element(p)
350  || (activity.element(r)
351  && (data.element(r) < lambda)))
352  {
353  data.element(p).take(data.element(r));
354  parent.element(r) = p;
355  if (activity.element(r) == false)
356  activity.element(p) = false;
357  }
358  else
359  activity.element(p) = false;
360  }
361  }
362 
363  deja_vu.element(p) = true;
364  }
365  }
366 
367 
368  // Second pass.
369  {
370  for (int i = n_points - 1; i >= 0 ; --i)
371  {
372  unsigned p = s[i];
373  if (parent.element(p) == p) // p is root.
374  output.element(p) = input.element(p);
375  else
376  output.element(p) = output.element(parent.element(p));
377  }
378  }
379 
380  return output;
381  }
382 
383  } // end of namespace mln::canvas::morpho::impl
384 
385 
386 
387 
388  // Dispatch.
389 
390 
391  namespace internal
392  {
393 
394  // Dispatch to generic.
395 
396  template <typename I, typename N, typename A>
397  inline
398  mln_concrete(I)
399  attribute_filter_dispatch(metal::false_,
400  const Image<I>& input,
401  const Neighborhood<N>& nbh,
402  const Accumulator<A>& a,
403  const mln_result(A)& lambda,
404  bool increasing)
405  {
406  p_array<mln_psite(I)> s = increasing ?
409 
410  return impl::generic::attribute_filter(input, nbh, s, a, lambda);
411  }
412 
413 
414  // Dispatch to fastest.
415 
416  template <typename I, typename N, typename A>
417  inline
418  mln_concrete(I)
419  attribute_filter_dispatch(metal::true_,
420  const Image<I>& input,
421  const Neighborhood<N>& nbh,
422  const Accumulator<A>& a,
423  const mln_result(A)& lambda,
424  bool increasing)
425  {
426  extension::adjust(input, nbh);
427 
428  util::array<unsigned> s =
429  increasing ?
431  data::sort_offsets_decreasing(input);
432 
433  return impl::attribute_filter_fastest(input, nbh, s, a, lambda);
434  }
435 
436 
437 
438  template <typename I, typename N, typename A>
439  inline
440  mln_concrete(I)
441  attribute_filter_dispatch(const Image<I>& input,
442  const Neighborhood<N>& nbh,
443  const Accumulator<A>& a,
444  const mln_result(A)& lambda,
445  bool increasing)
446  {
447  enum {
448  test = (mlc_equal(mln_trait_image_speed(I),
449  trait::image::speed::fastest)::value &&
450  mln_is_simple_neighborhood(N)::value &&
451  (mlc_equal(mln_trait_accumulator_when_pix(A),
452  trait::accumulator::when_pix::use_none)::value ||
453  mlc_equal(mln_trait_accumulator_when_pix(A),
454  trait::accumulator::when_pix::use_v)::value))
455  };
456  return attribute_filter_dispatch(metal::bool_<test>(), input, nbh, a, lambda, increasing);
457  }
458 
459  } // end of namespace mln::canvas::morpho::internal
460 
461 
462 
463  // Facade.
464 
465  template <typename I, typename N, typename A>
466  inline
467  mln_concrete(I)
468  attribute_filter(const Image<I>& input,
469  const Neighborhood<N>& nbh,
470  const Accumulator<A>& a,
471  const mln_result(A)& lambda,
472  bool increasing)
473  {
474  return internal::attribute_filter_dispatch(input, nbh, a, lambda, increasing);
475  }
476 
477 
478 # endif // ! MLN_INCLUDE_ONLY
479 
480  } // end of namespace mln::canvas::morpho
481 
482  } // end of namespace mln::canvas
483 
484 } // end of namespace mln
485 
486 
487 #endif // ! MLN_CANVAS_MORPHO_ATTRIBUTE_FILTER_HH