$extrastylesheet
Olena  User documentation 2.1
An Image Processing Platform
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
regional_maxima.hh
1 // Copyright (C) 2007, 2008, 2009, 2013 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_LABELING_REGIONAL_MAXIMA_HH
28 # define MLN_LABELING_REGIONAL_MAXIMA_HH
29 
33 
34 # include <mln/core/concept/image.hh>
35 # include <mln/core/concept/neighborhood.hh>
36 # include <mln/canvas/labeling/sorted.hh>
37 # include <mln/data/fill.hh>
38 # include <mln/data/sort_psites.hh>
39 
40 
41 namespace mln
42 {
43 
44  namespace labeling
45  {
46 
57  template <typename I, typename N, typename L>
58  mln_ch_value(I, L)
59  regional_maxima(const Image<I>& input, const Neighborhood<N>& nbh,
60  L& nlabels);
61 
62 
63 
64 # ifndef MLN_INCLUDE_ONLY
65 
66  namespace impl
67  {
68 
69  // Generic functor.
70 
71  template <typename I>
72  struct regional_maxima_functor
73  {
74  typedef mln_psite(I) P;
75 
76  // requirements from mln::canvas::labeling:
77 
78  const I& input;
79 
80  // Generic implementation
81 
82  void init() { data::fill(attr, true); }
83  bool handles(const P&) const { return true; }
84  bool labels(const P& p) const { return attr(p); }
85  bool equiv(const P& n, const P& p) const { return input(n) ==
86  input(p); }
87  void do_no_union(const P& n, const P& p) { mln_invariant(input(n) >
88  input(p));
89  attr(p) = false; (void)n; }
90  void init_attr(const P&) {}
91  void merge_attr(const P& r, const P& p) { attr(p) = attr(p) &&
92  attr(r); }
93 
94  // Fastest implementation
95 
96  void init_() { data::fill(attr, true); }
97  bool handles_(unsigned) const { return true; }
98  bool labels_(unsigned p) const { return attr.element(p); }
99  bool equiv_(unsigned n, unsigned p) const { return input.element(n) ==
100  input.element(p); }
101  void do_no_union_(unsigned n, unsigned p) { // mln_invariant(input.element(n) >
102  // input.element(p));
103  attr.element(p) = false;
104  (void) n;
105  }
106  void init_attr_(unsigned) {}
107  void merge_attr_(unsigned r, unsigned p) { attr.element(p) = attr.element(p) &&
108  attr.element(r); }
109 
110  // end of requirements
111 
112  mln_ch_value(I, bool) attr;
113 
114  regional_maxima_functor(const I& input)
115  : input(input)
116  {
117  initialize(attr, input);
118  }
119  };
120 
121 
122  } // end of namespace mln::labeling::impl
123 
124 
125 
126 
127  // Facade.
128 
129  template <typename I, typename N, typename L>
130  mln_ch_value(I, L)
131  regional_maxima(const Image<I>& input_, const Neighborhood<N>& nbh_,
132  L& nlabels)
133  {
134  mln_trace("labeling::regional_maxima");
135 
136  const I& input = exact(input_);
137  const N& nbh = exact(nbh_);
138  mln_precondition(input.is_valid());
139 
140  typedef impl::regional_maxima_functor<I> F;
141  F f(exact(input));
142  mln_ch_value(I, L)
143  output = canvas::labeling::sorted(input, nbh, nlabels, f, true);
144 
145  return output;
146  }
147 
148 # endif // ! MLN_INCLUDE_ONLY
149 
150  } // end of namespace mln::labeling
151 
152 } // end of namespace mln
153 
154 
155 #endif // ! MLN_LABELING_REGIONAL_MAXIMA_HH