$extrastylesheet
Olena  User documentation 2.1
An Image Processing Platform
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
rank_filter.hh
1 // Copyright (C) 2007, 2008, 2009, 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_MORPHO_RANK_FILTER_HH
28 # define MLN_MORPHO_RANK_FILTER_HH
29 
35 
36 # include <mln/morpho/includes.hh>
37 # include <mln/accu/transform_line.hh>
38 # include <mln/convert/to_p_array.hh>
39 
40 
41 
42 namespace mln
43 {
44 
45  namespace morpho
46  {
47 
52  template <typename I, typename W>
53  mln_concrete(I)
54  rank_filter(const Image<I>& input, const Window<W>& win, unsigned k);
55 
56 
57 # ifndef MLN_INCLUDE_ONLY
58 
59 
60  // Tests.
61 
62 
63  namespace internal
64  {
65 
66  template <typename I, typename W>
67  inline
68  void
69  rank_filter_tests(const Image<I>& input_, const Window<W>& win_, unsigned k)
70  {
71  const I& input = exact(input_);
72  const W& win = exact(win_);
73 
74  mln_precondition(input.is_valid());
75  mln_precondition(! win.is_empty());
76  (void) input;
77  (void) win;
78  (void) k;
79  }
80 
81  } // end of namespace mln::morpho::internal
82 
83 
84 
85  // Implementations.
86 
87 
88  namespace impl
89  {
90 
91  namespace generic
92  {
93 
94  template <typename I, typename W>
95  inline
96  mln_concrete(I)
97  rank_filter(const Image<I>& input_, const Window<W>& win_, unsigned k)
98  {
99  mln_trace("morpho::impl::generic::rank_filter");
100 
101  internal::rank_filter_tests(input_, win_, k);
102 
103  const I& input = exact(input_);
104  const W& win = exact(win_);
105 
106  mln_concrete(I) output;
107  initialize(output, input);
108 
109  accu::stat::rank<mln_value(I)> accu(k);
110  extension::adjust_fill(input, geom::delta(win) + 1, accu);
111  mln_piter(I) p(input.domain());
112  mln_qiter(W) q(win, p);
113  for_all(p)
114  {
115  accu.init();
116  for_all(q)
117  if (input.has(q))
118  accu.take(input(q));
119  //else
120  // accu.take(mln_value(I)(literal::zero));
121  output(p) = accu;
122  }
123 
124  return output;
125  }
126 
127  } // end of namespace mln::morpho::impl::generic
128 
129 
130  template <typename I, typename W>
131  inline
132  mln_concrete(I)
133  rank_filter_line(const Image<I>& input, const Window<W>& win, unsigned k, unsigned dir)
134  {
135  mln_trace("morpho::impl::rank_filter_line");
136 
137  internal::rank_filter_tests(input, win, k);
138 
139  accu::stat::rank<mln_value(I)> accu(k);
140  extension::adjust_fill(input, geom::delta(win) + 1, accu);
141  mln_concrete(I) output = accu::transform_line(accu, input, exact(win).length(), dir);
142 
143  return output;
144  }
145 
146 
147  template <typename I, typename W>
148  inline
149  mln_concrete(I)
150  rank_filter_directional(const Image<I>& input, const Window<W>& win, unsigned k, unsigned dir)
151  {
152  mln_trace("morpho::impl::rank_filter_directional");
153 
154  internal::rank_filter_tests(input, win, k);
155 
156  accu::stat::rank<mln_value(I)> accu(k);
157  extension::adjust_fill(input, geom::delta(win) + 1, accu);
158  mln_concrete(I) output = accu::transform_directional(accu, input, win, dir);
159 
160  return output;
161  }
162 
163 
164  } // end of namespace mln::morpho::impl
165 
166 
167 
168  // Dispatch.
169 
170 
171  namespace internal
172  {
173 
174  template <typename I, typename M, unsigned i, typename C>
175  inline
176  mln_concrete(I)
177  rank_filter_dispatch(const Image<I>& input, const win::line<M, i, C>& win, unsigned k)
178  {
179  return impl::rank_filter_line(input, win, k, i);
180  }
181 
182  template <typename I>
183  inline
184  mln_concrete(I)
185  rank_filter_dispatch(const Image<I>& input, const win::rectangle2d& win, unsigned k)
186  {
187  if (win.height() <= 3 && win.width() <= 3)
188  return impl::generic::rank_filter(input, win, k);
189  else
190  if (win.height() < win.width())
191  return impl::rank_filter_directional(input, win, k, 1);
192  else
193  return impl::rank_filter_directional(input, win, k, 0);
194  }
195 
196  template <typename I, typename W>
197  inline
198  mln_concrete(I)
199  rank_filter_dispatch(const Image<I>& input, const Window<W>& win, unsigned k)
200  {
201  return impl::generic::rank_filter(input, win, k);
202  }
203 
204  } // end of namespace mln::morpho::internal
205 
206 
207 
208  // Facades.
209 
210 
211  template <typename I, typename W>
212  inline
213  mln_concrete(I)
214  rank_filter(const Image<I>& input, const Window<W>& win, unsigned k)
215  {
216  mln_trace("morpho::rank_filter");
217 
218  mln_precondition(exact(input).is_valid());
219  mln_precondition(! exact(win).is_empty());
220 
221  mln_concrete(I) output = internal::rank_filter_dispatch(exact(input), exact(win), k);
222 
223  return output;
224  }
225 
226 # endif // ! MLN_INCLUDE_ONLY
227 
228  } // end of namespace mln::morpho
229 
230 } // end of namespace mln
231 
232 
233 #endif // ! MLN_MORPHO_RANK_FILTER_HH