$extrastylesheet
Olena  User documentation 2.1
An Image Processing Platform
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends 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  mln_trace("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  return is_crest;
163  }
164 
165  } // end of namespace mln::topo::skeleton::impl::generic
166 
167 
168 
169 
170  template <typename I, typename D, typename N>
171  mln_concrete(I)
172  crest_fastest_2d(const Image<I>& input_, const Image<D>& dist_map_,
173  const Neighborhood<N>& nbh_, unsigned psi_threshold)
174  {
175  mln_trace("topo::skeleton::impl::crest_fastest_2d");
176 
177  const I& input = exact(input_);
178  const D& dist_map = exact(dist_map_);
179  const N& nbh = exact(nbh_);
180 
181  mlc_equal(mln_value(I), bool)::check();
182  mln_precondition(input.is_valid());
183  mln_precondition(dist_map.is_valid());
184  mln_precondition(nbh.is_valid());
185  mln_precondition(input.domain() == dist_map.domain());
186 
187  extension::adjust(input, nbh);
188  border::equalize(input, dist_map, input.border());
189 
190  mln_concrete(I) is_crest;
191  initialize(is_crest, input);
192  data::fill(is_crest, false);
193 
194 
195  mln_pixter(const I) p(input);
196  util::array<int> dp = mln::offsets_wrt(input, nbh);
197  unsigned n_nbhs = dp.nelements();
198  for_all(p)
199  {
200  if (!p.val()
201  || dist_map.element(p) < static_cast<mln_value(D)>(0))
202  continue;
203 
204  unsigned nb_eq = 0;
205  unsigned nb_lt = 0;
206  for (unsigned i = 0; i < n_nbhs; ++i)
207  {
208  unsigned n = p.offset() + dp[i];
209  if (// We want to only consider sites which are part of
210  // the skeleton. If this test is removed, sometimes
211  // edge sites are considered as sites with a high PSI
212  // which is wrong.
213  dist_map.element(n) > static_cast<mln_value(D)>(0))
214  {
215  if (dist_map.element(n) == dist_map.element(p))
216  ++nb_eq;
217  else if (dist_map.element(n) < dist_map.element(p))
218  ++nb_lt;
219  }
220 
221  if ((nb_lt + nb_eq) >= psi_threshold) // Pixel Superiority index
222  is_crest.element(p) = true;
223  }
224 
225  }
226 
227  return is_crest;
228  }
229 
230  } // end of namespace mln::topo::skeleton::impl
231 
232 
233  // DISPATCH
234 
235  namespace internal
236  {
237 
238  template <typename I, typename D, typename N>
239  mln_concrete(I)
240  crest_dispatch_2d(mln::trait::image::value_storage::any,
241  mln::trait::image::value_access::any,
242  mln::trait::image::ext_domain::any,
243  const Image<I>& input, const Image<D>& dist_map,
244  const Neighborhood<N>& nbh, unsigned psi_threshold)
245  {
246  return skeleton::impl::generic::crest(input, dist_map,
247  nbh, psi_threshold);
248  }
249 
250 
251  template <typename I, typename D, typename N>
252  mln_concrete(I)
253  crest_dispatch_2d(mln::trait::image::value_storage::one_block,
254  mln::trait::image::value_access::direct,
255  mln::trait::image::ext_domain::some,
256  const Image<I>& input, const Image<D>& dist_map,
257  const Neighborhood<N>& nbh, unsigned psi_threshold)
258  {
259  return skeleton::impl::crest_fastest_2d(input, dist_map,
260  nbh, psi_threshold);
261  }
262 
263 
264  template <typename I, typename D, typename N>
265  mln_concrete(I)
266  crest_dispatch(const Image<I>& input, const Image<D>& dist_map,
267  const Neighborhood<N>& nbh, unsigned psi_threshold)
268  {
269  unsigned dim = mln_site_(I)::dim;
270 
271  if (dim == 2)
272  return
273  crest_dispatch_2d(mln_trait_image_value_storage(I)(),
274  mln_trait_image_value_access(I)(),
275  mln_trait_image_ext_domain(I)(),
276  input, dist_map, nbh, psi_threshold);
277 
278  return impl::generic::crest(input, dist_map, nbh, psi_threshold);
279  }
280 
281 
282  } // end of namespace mln::topo::skeleton::internal
283 
284 
285  // FACADES
286 
287  template <typename I, typename D, typename N>
288  mln_concrete(I)
289  crest(const Image<I>& input, const Image<D>& dist_map,
290  const Neighborhood<N>& nbh, unsigned psi_threshold)
291  {
292  mln_trace("topo::skeleton::crest");
293 
294  mlc_equal(mln_value(I), bool)::check();
295  mln_precondition(exact(input).is_valid());
296  mln_precondition(exact(dist_map).is_valid());
297  mln_precondition(exact(nbh).is_valid());
298 
299  mln_concrete(I)
300  output = internal::crest_dispatch(input, dist_map,
301  nbh, psi_threshold);
302 
303  return output;
304  }
305 
306 
307  template <typename I, typename D, typename N>
308  mln_concrete(I)
309  crest(const Image<I>& input, const Image<D>& dist_map,
310  const Neighborhood<N>& nbh)
311  {
312  return crest(input, dist_map, nbh, 6);
313  }
314 
315 # endif // ! MLN_INCLUDE_ONLY
316 
317 
318  } // end of namespace mln::topo::skeleton
319 
320  } // end of namespace mln::topo
321 
322 } // end of namespace mln
323 
324 #endif // ! MLN_TOPO_SKELETON_CREST_HH