Milena (Olena)  User documentation 2.0a Id
 All Classes Namespaces Functions Variables Typedefs Enumerator Groups Pages
crest.hh
1 // Copyright (C) 2009, 2010, 2011 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_TOPO_SKELETON_CREST_HH
28 # define MLN_TOPO_SKELETON_CREST_HH
29 
33 
34 # include <mln/core/concept/image.hh>
35 # include <mln/core/concept/neighborhood.hh>
36 # include <mln/data/fill.hh>
37 # include <mln/extension/adjust.hh>
38 # include <mln/border/equalize.hh>
39 
40 
41 namespace mln
42 {
43 
44  namespace topo
45  {
46 
47  namespace skeleton
48  {
49 
50 
62  //
92  template <typename I, typename D, typename N>
93  mln_concrete(I)
94  crest(const Image<I>& input, const Image<D>& dist_map,
95  const Neighborhood<N>& nbh, unsigned psi_threshold);
96 
99  template <typename I, typename D, typename N>
100  mln_concrete(I)
101  crest(const Image<I>& input, const Image<D>& dist_map,
102  const Neighborhood<N>& nbh);
103 
104 
105 
106 # ifndef MLN_INCLUDE_ONLY
107 
108  // IMPLEMENTATIONS
109 
110  namespace impl
111  {
112 
113  namespace generic
114  {
115 
116  template <typename I, typename D, typename N>
117  mln_concrete(I)
118  crest(const Image<I>& input_, const Image<D>& dist_map_,
119  const Neighborhood<N>& nbh_, unsigned psi_threshold)
120  {
121  trace::entering("topo::skeleton::impl::generic::crest");
122  const I& input = exact(input_);
123  const D& dist_map = exact(dist_map_);
124  const N& nbh = exact(nbh_);
125 
126  mlc_equal(mln_value(I), bool)::check();
127  mln_precondition(input.is_valid());
128  mln_precondition(dist_map.is_valid());
129  mln_precondition(nbh.is_valid());
130 
131  mln_concrete(I) is_crest;
132  initialize(is_crest, input);
133  data::fill(is_crest, false);
134 
135  mln_piter(I) p(input.domain());
136  mln_niter(N) n(nbh, p);
137  for_all(p)
138  {
139  if (!input(p) || dist_map(p) < static_cast<mln_value(D)>(0))
140  continue;
141 
142  unsigned nb_eq = 0;
143  unsigned nb_lt = 0;
144  for_all(n)
145  if (input.domain().has(n)
146  // We want to only consider sites which are part of
147  // the skeleton. If this test is removed, sometimes
148  // edge sites are considered as sites with a high PSI
149  // which is wrong.
150  && dist_map(n) > static_cast<mln_value(D)>(0))
151  {
152  if (dist_map(n) == dist_map(p))
153  ++nb_eq;
154  else if (dist_map(n) < dist_map(p))
155  ++nb_lt;
156  }
157 
158  if ((nb_lt + nb_eq) >= psi_threshold) // Pixel Superiority index
159  is_crest(p) = true;
160  }
161 
162  trace::exiting("topo::skeleton::impl::generic::crest");
163  return is_crest;
164  }
165 
166  } // end of namespace mln::topo::skeleton::impl::generic
167 
168 
169 
170 
171  template <typename I, typename D, typename N>
172  mln_concrete(I)
173  crest_fastest_2d(const Image<I>& input_, const Image<D>& dist_map_,
174  const Neighborhood<N>& nbh_, unsigned psi_threshold)
175  {
176  trace::entering("topo::skeleton::impl::crest_fastest_2d");
177 
178  const I& input = exact(input_);
179  const D& dist_map = exact(dist_map_);
180  const N& nbh = exact(nbh_);
181 
182  mlc_equal(mln_value(I), bool)::check();
183  mln_precondition(input.is_valid());
184  mln_precondition(dist_map.is_valid());
185  mln_precondition(nbh.is_valid());
186  mln_precondition(input.domain() == dist_map.domain());
187 
188  extension::adjust(input, nbh);
189  border::equalize(input, dist_map, input.border());
190 
191  mln_concrete(I) is_crest;
192  initialize(is_crest, input);
193  data::fill(is_crest, false);
194 
195 
196  mln_pixter(const I) p(input);
197  util::array<int> dp = mln::offsets_wrt(input, nbh);
198  unsigned n_nbhs = dp.nelements();
199  for_all(p)
200  {
201  if (!p.val()
202  || dist_map.element(p) < static_cast<mln_value(D)>(0))
203  continue;
204 
205  unsigned nb_eq = 0;
206  unsigned nb_lt = 0;
207  for (unsigned i = 0; i < n_nbhs; ++i)
208  {
209  unsigned n = p.offset() + dp[i];
210  if (// We want to only consider sites which are part of
211  // the skeleton. If this test is removed, sometimes
212  // edge sites are considered as sites with a high PSI
213  // which is wrong.
214  dist_map.element(n) > static_cast<mln_value(D)>(0))
215  {
216  if (dist_map.element(n) == dist_map.element(p))
217  ++nb_eq;
218  else if (dist_map.element(n) < dist_map.element(p))
219  ++nb_lt;
220  }
221 
222  if ((nb_lt + nb_eq) >= psi_threshold) // Pixel Superiority index
223  is_crest.element(p) = true;
224  }
225 
226  }
227 
228  trace::exiting("topo::skeleton::impl::crest_fastest_2d");
229  return is_crest;
230  }
231 
232  } // end of namespace mln::topo::skeleton::impl
233 
234 
235  // DISPATCH
236 
237  namespace internal
238  {
239 
240  template <typename I, typename D, typename N>
241  mln_concrete(I)
242  crest_dispatch_2d(mln::trait::image::value_storage::any,
243  mln::trait::image::value_access::any,
244  mln::trait::image::ext_domain::any,
245  const Image<I>& input, const Image<D>& dist_map,
246  const Neighborhood<N>& nbh, unsigned psi_threshold)
247  {
248  return skeleton::impl::generic::crest(input, dist_map,
249  nbh, psi_threshold);
250  }
251 
252 
253  template <typename I, typename D, typename N>
254  mln_concrete(I)
255  crest_dispatch_2d(mln::trait::image::value_storage::one_block,
256  mln::trait::image::value_access::direct,
257  mln::trait::image::ext_domain::some,
258  const Image<I>& input, const Image<D>& dist_map,
259  const Neighborhood<N>& nbh, unsigned psi_threshold)
260  {
261  return skeleton::impl::crest_fastest_2d(input, dist_map,
262  nbh, psi_threshold);
263  }
264 
265 
266  template <typename I, typename D, typename N>
267  mln_concrete(I)
268  crest_dispatch(const Image<I>& input, const Image<D>& dist_map,
269  const Neighborhood<N>& nbh, unsigned psi_threshold)
270  {
271  unsigned dim = mln_site_(I)::dim;
272 
273  if (dim == 2)
274  return
275  crest_dispatch_2d(mln_trait_image_value_storage(I)(),
276  mln_trait_image_value_access(I)(),
277  mln_trait_image_ext_domain(I)(),
278  input, dist_map, nbh, psi_threshold);
279 
280  return impl::generic::crest(input, dist_map, nbh, psi_threshold);
281  }
282 
283 
284  } // end of namespace mln::topo::skeleton::internal
285 
286 
287  // FACADES
288 
289  template <typename I, typename D, typename N>
290  mln_concrete(I)
291  crest(const Image<I>& input, const Image<D>& dist_map,
292  const Neighborhood<N>& nbh, unsigned psi_threshold)
293  {
294  trace::entering("topo::skeleton::crest");
295 
296  mlc_equal(mln_value(I), bool)::check();
297  mln_precondition(exact(input).is_valid());
298  mln_precondition(exact(dist_map).is_valid());
299  mln_precondition(exact(nbh).is_valid());
300 
301  mln_concrete(I)
302  output = internal::crest_dispatch(input, dist_map,
303  nbh, psi_threshold);
304 
305  trace::exiting("topo::skeleton::crest");
306  return output;
307  }
308 
309 
310  template <typename I, typename D, typename N>
311  mln_concrete(I)
312  crest(const Image<I>& input, const Image<D>& dist_map,
313  const Neighborhood<N>& nbh)
314  {
315  return crest(input, dist_map, nbh, 6);
316  }
317 
318 # endif // ! MLN_INCLUDE_ONLY
319 
320 
321  } // end of namespace mln::topo::skeleton
322 
323  } // end of namespace mln::topo
324 
325 } // end of namespace mln
326 
327 #endif // ! MLN_TOPO_SKELETON_CREST_HH