$extrastylesheet
Olena  User documentation 2.1
An Image Processing Platform
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
elementary/gradient.hh
1 // Copyright (C) 2008, 2009 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 
26 #ifndef MLN_MORPHO_ELEMENTARY_GRADIENT_HH
27 # define MLN_MORPHO_ELEMENTARY_GRADIENT_HH
28 
32 
33 # include <mln/morpho/includes.hh>
34 # include <mln/accu/stat/min_max.hh>
35 
36 
37 namespace mln
38 {
39 
40  namespace morpho
41  {
42 
43  namespace elementary
44  {
45 
46 
47  template <typename I, typename N>
48  mln_concrete(I)
49  gradient(const Image<I>& input, const Neighborhood<N>& nbh);
50 
51 
52 # ifndef MLN_INCLUDE_ONLY
53 
54  namespace internal
55  {
56 
57  template <typename I, typename N>
58  void
59  gradient_tests(const Image<I>& input, const Neighborhood<N>& nbh)
60  {
61  mln_precondition(exact(input).is_valid());
62  mln_precondition(exact(nbh).is_valid());
63  (void) input;
64  (void) nbh;
65  }
66 
67  } // end of namespace mln::morpho::elementary::tests
68 
69 
70  namespace impl
71  {
72 
73  template <typename I, typename N>
74  mln_concrete(I)
75  gradient_on_function(const Image<I>& input_, const Neighborhood<N>& nbh_)
76  {
77  mln_trace("morpho::elementary::impl::gradient_on_function");
78 
79  const I& input = exact(input_);
80  const N& nbh = exact(nbh_);
81  internal::gradient_tests(input, nbh);
82 
83  accu::stat::min_max<mln_value(I)> a;
84 
85  extension::adjust_duplicate(input, nbh);
86 
87  mln_concrete(I) output;
88  initialize(output, input);
89 
90  mln_piter(I) p(input.domain());
91  mln_niter(N) n(nbh, p);
92  for_all(p)
93  {
94  a.take_as_init(input(p));
95  for_all(n) if (input.has(n))
96  a.take(input(n));
97  output(p) = a.second() - a.first();
98  }
99 
100  return output;
101  }
102 
103  template <typename I, typename N>
104  mln_concrete(I)
105  gradient_on_set(const Image<I>& input_, const Neighborhood<N>& nbh_)
106  {
107  mln_trace("morpho::elementary::impl::gradient_on_set");
108 
109  const I& input = exact(input_);
110  const N& nbh = exact(nbh_);
111  internal::gradient_tests(input, nbh);
112 
113  extension::adjust_duplicate(input, nbh);
114 
115  mln_concrete(I) output;
116  initialize(output, input);
117  data::fill(output, false);
118 
119  mln_piter(I) p(input.domain());
120  mln_niter(N) n(nbh, p);
121  for_all(p)
122  if (input(p) == true)
123  {
124  for_all(n) if (input.has(n))
125  if (input(n) == false)
126  {
127  output(p) = true;
128  break;
129  }
130  }
131  else // input(p) == false
132  {
133  for_all(n) if (input.has(n))
134  if (input(n) == true)
135  {
136  output(p) = true;
137  break;
138  }
139  }
140 
141  return output;
142  }
143 
144 
145  template <typename I, typename N>
146  mln_concrete(I)
147  gradient_on_function_fastest(const Image<I>& input_, const Neighborhood<N>& nbh_)
148  {
149  mln_trace("morpho::elementary::impl::gradient_on_function_fastest");
150 
151  const I& input = exact(input_);
152  const N& nbh = exact(nbh_);
153  internal::gradient_tests(input, nbh);
154 
155  accu::stat::min_max<mln_value(I)> a;
156  extension::adjust_duplicate(input, nbh);
157 
158  typedef mln_concrete(I) O;
159  O output;
160  initialize(output, input);
161 
162  mln_pixter(const I) p_in(input);
163  mln_pixter(O) p_out(output);
164  mln_nixter(const I, N) n(p_in, nbh);
165  for_all_2(p_in, p_out)
166  {
167  a.take_as_init(p_in.val());
168  for_all(n)
169  a.take(n.val());
170  p_out.val() = a.second() - a.first();
171  }
172 
173  return output;
174  }
175 
176  } // end of namespace mln::morpho::elementary::impl
177 
178 
179  namespace internal
180  {
181 
182  // Dispatch.
183 
184  template <typename I, typename N>
185  mln_concrete(I)
186  gradient_dispatch(trait::image::kind::any,
187  trait::image::speed::any,
188  const Image<I>& input, const Neighborhood<N>& nbh)
189  {
190  return impl::gradient_on_function(input, nbh);
191  }
192 
193  template <typename I, typename N>
194  mln_concrete(I)
195  gradient_dispatch(trait::image::kind::any,
196  trait::image::speed::fastest,
197  const Image<I>& input, const Neighborhood<N>& nbh)
198  {
199  return impl::gradient_on_function_fastest(input, nbh);
200  }
201 
202  template <typename I, typename N>
203  mln_concrete(I)
204  gradient_dispatch(trait::image::kind::logic,
205  trait::image::speed::any,
206  const Image<I>& input, const Neighborhood<N>& nbh)
207  {
208  return impl::gradient_on_set(input, nbh);
209  }
210 
211  // May be redundant with the previous version but fixes an ambiguity
212  // with image*d<bool> which is fasted AND logic.
213  template <typename I, typename N>
214  mln_concrete(I)
215  gradient_dispatch(trait::image::kind::logic,
216  trait::image::speed::fastest,
217  const Image<I>& input, const Neighborhood<N>& nbh)
218  {
219  return impl::gradient_on_set(input, nbh);
220  }
221 
222  template <typename I, typename N>
223  mln_concrete(I)
224  gradient_dispatch(const Image<I>& input, const Neighborhood<N>& nbh)
225  {
226  return gradient_dispatch(mln_trait_image_kind(I)(),
227  mln_trait_image_speed(I)(),
228  input, nbh);
229  }
230 
231  } // end of namespace mln::morpho::elementary::internal
232 
233 
234  // Facade.
235 
236  template <typename I, typename N>
237  mln_concrete(I)
238  gradient(const Image<I>& input, const Neighborhood<N>& nbh)
239  {
240  mln_trace("morpho::elementary::gradient");
241 
242  internal::gradient_tests(input, nbh);
243  mln_concrete(I) output = internal::gradient_dispatch(input, nbh);
244 
245  return output;
246  }
247 
248 # endif // ! MLN_INCLUDE_ONLY
249 
250  } // end of namespace mln::morpho::elementary
251 
252  } // end of namespace mln::morpho
253 
254 } // end of namespace mln
255 
256 
257 #endif // ! MLN_MORPHO_ELEMENTARY_GRADIENT_HH