$extrastylesheet
Olena  User documentation 2.1
An Image Processing Platform
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
regional_minima.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_MINIMA_HH
28 # define MLN_LABELING_REGIONAL_MINIMA_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_minima(const Image<I>& input, const Neighborhood<N>& nbh,
60  L& nlabels);
61 
62 
63 # ifndef MLN_INCLUDE_ONLY
64 
65  namespace impl
66  {
67 
68  // Generic functor.
69 
70  template <typename I>
71  struct regional_minima_functor
72  {
73  typedef mln_psite(I) P;
74 
75  // requirements from mln::canvas::labeling:
76 
77  const I& input;
78 
79  // Generic implementation
80 
81  void init() { data::fill(attr, true); }
82  bool handles(const P&) const { return true; }
83  bool labels(const P& p) const { return attr(p); }
84  bool equiv(const P& n, const P& p) const { return input(n) ==
85  input(p); }
86  void do_no_union(const P& n, const P& p)
87  {
88  // Avoid a warning about an undefined variable when NDEBUG
89  // is not defined.
90  (void)n;
91 
92  mln_invariant(input(n) < input(p));
93  attr(p) = false;
94  }
95 
96  void init_attr(const P&) {}
97  void merge_attr(const P& r, const P& p) { attr(p) = attr(p) &&
98  attr(r); }
99 
100  // Fastest implementation
101 
102  void init_() { data::fill(attr, true); }
103  bool handles_(unsigned) const { return true; }
104  bool labels_(unsigned p) const { return attr.element(p); }
105  bool equiv_(unsigned n, unsigned p) const { return input.element(n) ==
106  input.element(p); }
107  void do_no_union_(unsigned n, unsigned p)
108  {
109  // Avoid a warning about an undefined variable when NDEBUG
110  // is not defined.
111  (void)n;
112 
113  mln_invariant(input.element(n) < input.element(p));
114  attr.element(p) = false;
115  }
116 
117  void init_attr_(unsigned) {}
118  void merge_attr_(unsigned r, unsigned p) { attr.element(p) = attr.element(p) &&
119  attr.element(r); }
120 
121  // end of requirements
122 
123  mln_ch_value(I, bool) attr;
124 
125  regional_minima_functor(const I& input)
126  : input(input)
127  {
128  initialize(attr, input);
129  }
130  };
131 
132 
133  } // end of namespace mln::labeling::impl
134 
135 
136 
137  // Facade.
138 
139  template <typename I, typename N, typename L>
140  mln_ch_value(I, L)
141  regional_minima(const Image<I>& input_, const Neighborhood<N>& nbh_,
142  L& nlabels)
143  {
144  mln_trace("labeling::regional_minima");
145 
146  const I& input = exact(input_);
147  const N& nbh = exact(nbh_);
148  mln_precondition(input.is_valid());
149 
150  // FIXME: abort if L is not wide enough to encode the set of
151  // minima.
152 
153  typedef impl::regional_minima_functor<I> F;
154  F f(exact(input));
155  mln_ch_value(I, L)
156  output = canvas::labeling::sorted(input, nbh, nlabels, f, false);
157 
158  return output;
159  }
160 
161 # endif // ! MLN_INCLUDE_ONLY
162 
163  } // end of namespace mln::labeling
164 
165 } // end of namespace mln
166 
167 
168 #endif // ! MLN_LABELING_REGIONAL_MINIMA_HH