26 #ifndef MLN_DATA_WAS_MEDIAN_HH
27 # define MLN_DATA_WAS_MEDIAN_HH
34 # include <mln/win/shift.hh>
35 # include <mln/core/alias/window2d.hh>
37 # include <mln/geom/min_col.hh>
38 # include <mln/geom/max_col.hh>
39 # include <mln/geom/max_row.hh>
40 # include <mln/geom/min_row.hh>
42 # include <mln/win/diff.hh>
43 # include <mln/win/shift.hh>
45 # include <mln/data/median.hh>
46 # include <mln/win/hline2d.hh>
48 # include <mln/opt/at.hh>
62 template <
typename I,
typename W,
typename O>
63 void median_as_procedure(
const I& input,
67 mln_precondition(input.is_valid());
68 mln_precondition(output.is_valid());
75 win_fwd_plus = win - win::shift(win, left),
76 win_fwd_minus = win::shift(win, left) - win,
77 win_bkd_plus = win - win::shift(win, right),
78 win_bkd_minus = win::shift(win, right) - win,
79 win_bot = win - win::shift(win,
up),
80 win_top = win::shift(win,
up) - win;
84 q_fp(win_fwd_plus, p), q_fm(win_fwd_minus, p),
85 q_bp(win_bkd_plus, p), q_bm(win_bkd_minus, p),
86 q_top(win_top, p), q_bot(win_bot, p);
88 accu::stat::median_h<mln_vset(I)> med(input.values());
92 p = input.bbox().pmin() +
up;
95 mln_qiter(W) q(win, p);
96 for_all(q) if (input.has(q))
100 def::coord& row = p.row();
101 def::coord& col = p.col();
104 mln_assertion(p.col() == min_col);
105 mln_assertion(p.row() == min_row - 1);
107 for (row = min_row; row <= max_row; ++row)
110 for_all(q_top) if (input.has(q_top))
111 med.untake(input(q_top));
112 for_all(q_bot) if (input.has(q_bot))
113 med.take(input(q_bot));
118 while (col < max_col)
121 for_all(q_fm) if (input.has(q_fm))
122 med.untake(input(q_fm));
123 for_all(q_fp) if (input.has(q_fp))
124 med.take(input(q_fp));
129 while (col > min_col)
132 for_all(q_bm) if (input.has(q_bm))
133 med.untake(input(q_bm));
134 for_all(q_bp) if (input.has(q_bp))
135 med.take(input(q_bp));
148 template <typename I, typename O>
149 void hmedian(const I& input, const win::
hline2d& win, O& output)
155 const unsigned half = win.length() / 2;
161 accu::stat::median_h<mln_vset(I)> med(input.values());
163 for (row = min_row; row <= max_row; ++row)
169 for (ct = min_col; ct < min_col + half; ++ct)
170 med.take(
opt::at(input, row, ct));
173 for (col = min_col; col <= min_col + half; ++col, ++ct)
175 med.take(
opt::at(input, row, ct));
181 for (; col <= max_col - half; ++cu, ++col, ++ct)
183 med.take(
opt::at(input, row, ct));
184 med.untake(
opt::at(input, row, cu));
189 for (; col <= max_col; ++cu, ++col)
191 med.untake(
opt::at(input, row, cu));
207 #endif // ! MLN_DATA_WAS_MEDIAN_HH