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