Milena (Olena)  User documentation 2.0a Id
 All Classes Namespaces Functions Variables Typedefs Enumerator Groups Pages
fast_median.hh
1 // Copyright (C) 2007, 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_DATA_FAST_MEDIAN_HH
27 # define MLN_DATA_FAST_MEDIAN_HH
28 
36 # include <mln/core/concept/image.hh>
37 # include <mln/core/alias/window2d.hh>
38 # include <mln/accu/stat/median_h.hh>
39 
40 # include <mln/win/shift.hh>
41 # include <mln/win/diff.hh>
42 
43 # include <mln/geom/min_col.hh>
44 # include <mln/geom/min_row.hh>
45 # include <mln/geom/max_col.hh>
46 # include <mln/geom/max_row.hh>
47 
48 
49 namespace mln
50 {
51 
52  namespace data
53  {
54 
64  template <typename I, typename W, typename O>
65  void fast_median(const Image<I>& input, const Window<W>& win,
66  Image<O>& output);
67 
68 
69 # ifndef MLN_INCLUDE_ONLY
70 
71 
72  namespace impl
73  {
74 
75  template <typename I, typename W, typename O>
76  inline
77  void fast_median(const I& input,
78  const W& win,
79  O& output)
80  {
81  mln_precondition(input.is_valid());
82  mln_precondition(output.is_valid());
83 
85  min_row = geom::min_row(input), max_row = geom::max_row(input),
86  min_col = geom::min_col(input), max_col = geom::max_col(input);
87 
88  window2d
89  win_fwd_plus = win - win::shift(win, left),
90  win_fwd_minus = win::shift(win, left) - win,
91  win_bkd_plus = win - win::shift(win, right),
92  win_bkd_minus = win::shift(win, right) - win,
93  win_bot = win - win::shift(win, up),
94  win_top = win::shift(win, up) - win;
95 
96  accu::stat::median_h<mln_value(I)> med;
97 
98  // initialization
99 
100  point2d p = input.domain().pmin() + up;
101 
102  mln_qixter(const I, window2d)
103  q_fp(input, win_fwd_plus, p), q_fm(input, win_fwd_minus, p),
104  q_bp(input, win_bkd_plus, p), q_bm(input, win_bkd_minus, p),
105  q_top(input, win_top, p), q_bot(input, win_bot, p);
106 
107  med.init();
108  {
109  mln_qixter(const I, W) q(input, win, p);
110  for_all(q)
111  med.take(q.val());
112  }
113 
114  def::coord& row = p.row();
115  def::coord& col = p.col();
116  bool fwd = true;
117 
118  mln_assertion(p.col() == min_col);
119  mln_assertion(p.row() == min_row - 1);
120 
121  for (row = min_row; row <= max_row; ++row)
122  {
123 
124  // "go down"
125  for_all(q_top)
126  med.untake(q_top.val());
127 
128  for_all(q_bot)
129  med.take(q_bot.val());
130 
131  output(p) = med;
132 
133  if (fwd)
134  // browse line fwd
135  while (col < max_col)
136  {
137  ++col;
138  for_all(q_fm)
139  med.untake(q_fm.val());
140  for_all(q_fp)
141  med.take(q_fp.val());
142  output(p) = med;
143  }
144  else
145  // browse line bkd
146  while (col > min_col)
147  {
148  --col;
149  for_all(q_bm)
150  med.untake(q_bm.val());
151  for_all(q_bp)
152  med.take(q_bp.val());
153  output(p) = med;
154  }
155  // change browsing
156  fwd = ! fwd;
157  }
158  }
159 
160  } // end of namespace mln::data::impl
161 
162 
163  // facade
164 
165  template <typename I, typename W, typename O>
166  inline
167  void fast_median(const Image<I>& input, const Window<W>& win,
168  Image<O>& output)
169  {
170  impl::fast_median(exact(input), exact(win), exact(output));
171  }
172 
173 # endif // ! MLN_INCLUDE_ONLY
174 
175  } // end of namespace mln::data
176 
177 } // end of namespace mln
178 
179 
180 #endif // ! MLN_DATA_FAST_MEDIAN_HH