Milena (Olena)  User documentation 2.0a Id
 All Classes Namespaces Functions Variables Typedefs Enumerator Groups Pages
approx/dilation.hh
1 // Copyright (C) 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_APPROX_DILATION_HH
27 # define MLN_MORPHO_APPROX_DILATION_HH
28 
32 
33 # include <mln/core/concept/image.hh>
34 
35 # include <mln/core/routine/duplicate.hh>
36 # include <mln/data/compare.hh>
37 
38 # include <mln/transform/distance_front.hh>
39 # include <mln/pw/all.hh>
40 
41 # include <mln/core/alias/neighb2d.hh>
42 # include <mln/make/w_window2d_int.hh>
43 # include <mln/win/disk2d.hh>
44 
45 # include <mln/core/alias/neighb3d.hh>
46 # include <mln/make/w_window3d_int.hh>
47 # include <mln/win/sphere3d.hh>
48 
49 
50 
51 namespace mln
52 {
53 
54  namespace morpho
55  {
56 
57  namespace approx
58  {
59 
60 
61  template <typename I, typename W>
62  mln_concrete(I)
63  dilation(const Image<I>& input, const Window<W>& win);
64 
65 
66 
67 # ifndef MLN_INCLUDE_ONLY
68 
69 
70  // Implementations.
71 
72  namespace impl
73  {
74 
75 
76  // By distance thresholding.
77 
78  template <typename I>
79  mln_concrete(I)
80  dilation_by_distance_thresholding_2d(const Image<I>& input_,
81  const Window< win::disk2d >& win_)
82  {
83  trace::entering("morpho::approx::impl::dilation_by_distance_thresholding_2d");
84 
85  const I& input = exact(input_);
86  const win::disk2d& win = exact(win_);
87 
88  mln_precondition(input.is_valid());
89  mln_precondition(win.is_valid());
90 
91  int ws[] = { 00, 11, 0, 11, 0,
92  11, 7, 5, 7, 11,
93  00, 5, 0, 5, 0,
94  11, 7, 5, 7, 11,
95  00, 11, 0, 11, 0 };
96  const unsigned coef = 5;
97 
98  unsigned
99  radius = coef * win.diameter() / 2,
100  dmax = radius + 1;
101 
102  mln_ch_value(I, unsigned) dmap = transform::distance_front(input,
103  c4(), make::w_window2d_int(ws),
104  dmax);
105  mln_concrete(I) output;
106  output = duplicate((pw::value(dmap) <= pw::cst(radius)) | input.domain());
107 
108  trace::exiting("morpho::approx::impl::dilation_by_distance_thresholding_2d");
109  return output;
110  }
111 
112 
113 
114  template <typename I>
115  mln_concrete(I)
116  dilation_by_distance_thresholding_3d(const Image<I>& input_,
117  const Window< win::sphere3d >& win_)
118  {
119  trace::entering("morpho::approx::impl::dilation_by_distance_thresholding_3d");
120 
121  const I& input = exact(input_);
122  const win::sphere3d& win = exact(win_);
123 
124  mln_precondition(input.is_valid());
125  mln_precondition(win.is_valid());
126 
127  int ws[] = { 00, 21, 00,
128  21, 17, 21,
129  00, 21, 00,
130 
131  17, 12, 17,
132  12, 00, 12,
133  17, 12, 17,
134 
135  00, 21, 00,
136  21, 17, 21,
137  00, 21, 00 };
138  const unsigned coef = 12;
139 
140  unsigned
141  radius = coef * win.diameter() / 2,
142  dmax = radius + 1;
143 
144  mln_ch_value(I, unsigned) dmap = transform::distance_front(input,
145  c6(), make::w_window3d_int(ws),
146  dmax);
147  mln_concrete(I) output;
148  output = duplicate((pw::value(dmap) <= pw::cst(radius)) | input.domain());
149 
150  trace::exiting("morpho::approx::impl::dilation_by_distance_thresholding_3d");
151  return output;
152  }
153 
154 
155  } // end of namespace mln::morpho::approx::impl
156 
157 
158 
159  // Dispatch.
160 
161  namespace internal
162  {
163 
164  template <typename I>
165  mln_concrete(I)
166  dilation_dispatch(trait::image::kind::logic,
167  const I& input,
168  const win::disk2d& win)
169  {
170  return impl::dilation_by_distance_thresholding_2d(input, win);
171  }
172 
173  template <typename I>
174  mln_concrete(I)
175  dilation_dispatch(trait::image::kind::logic,
176  const I& input,
177  const win::sphere3d& win)
178  {
179  return impl::dilation_by_distance_thresholding_3d(input, win);
180  }
181 
182  // Entry point.
183 
184  template <typename I, typename W>
185  mln_concrete(I)
186  dilation_dispatch(const I& input, const W& win)
187  {
188  return dilation_dispatch(mln_trait_image_kind(I)(),
189  input, win);
190  }
191 
192  } // end of namespace mln::morpho::approx::internal
193 
194 
195  // Facade.
196 
197  template <typename I, typename W>
198  inline
199  mln_concrete(I)
200  dilation(const Image<I>& input, const Window<W>& win)
201  {
202  trace::entering("morpho::approx::dilation");
203 
204  mln_precondition(exact(input).is_valid());
205  mln_precondition(exact(win).is_valid());
206 
207  mln_concrete(I) output;
208  output = internal::dilation_dispatch(exact(input), exact(win));
209 
210  if (exact(win).is_centered())
211  mln_postcondition(output >= input);
212 
213  trace::exiting("morpho::approx::dilation");
214  return output;
215  }
216 
217 
218 # endif // ! MLN_INCLUDE_ONLY
219 
220  } // end of namespace mln::morpho::approx
221 
222  } // end of namespace mln::morpho
223 
224 } // end of namespace mln
225 
226 
227 #endif // ! MLN_MORPHO_APPROX_DILATION_HH