$extrastylesheet
Olena  User documentation 2.1
An Image Processing Platform
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
split_bg_fg.hh
1 // Copyright (C) 2009, 2010, 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 
28 #ifndef SCRIBO_PREPROCESSING_SPLIT_BG_FG_HH
29 # define SCRIBO_PREPROCESSING_SPLIT_BG_FG_HH
30 
34 
35 # include <mln/core/macros.hh>
36 # include <mln/core/image/image2d.hh>
37 # include <mln/core/alias/neighb2d.hh>
38 # include <mln/core/routine/duplicate.hh>
39 
40 # include <mln/core/image/dmorph/image_if.hh>
41 # include <mln/pw/all.hh>
42 
43 # include <mln/data/fill.hh>
44 # include <mln/data/transform.hh>
45 
46 # include <mln/value/int_u8.hh>
47 # include <mln/value/rgb8.hh>
48 
49 # include <mln/io/ppm/load.hh>
50 # include <mln/io/ppm/save.hh>
51 # include <mln/io/pgm/save.hh>
52 # include <mln/io/pbm/save.hh>
53 
54 # include <mln/math/diff_abs.hh>
55 # include <mln/math/min.hh>
56 
57 # include <mln/morpho/closing/area.hh>
58 # include <mln/morpho/opening/area.hh>
59 # include <mln/morpho/elementary/dilation.hh>
60 
61 # include <mln/labeling/blobs.hh>
62 # include <mln/labeling/colorize.hh>
63 # include <mln/labeling/compute.hh>
64 # include <mln/accu/stat/mean.hh>
65 
66 # include <mln/util/couple.hh>
67 
68 
69 
70 
71 namespace scribo
72 {
73 
74  namespace preprocessing
75  {
76 
77  using namespace mln;
78 
79 
90  template <typename I>
92  split_bg_fg(const Image<I>& input, unsigned lambda, unsigned delta);
93 
94 
95 
96 # ifndef MLN_INCLUDE_ONLY
97 
98 
99 
100  // Internal routines.
101 
102  namespace internal
103  {
104 
105  using value::rgb8;
106 
107 
108  template <unsigned n>
109  void
110  split(const image2d< value::rgb<n> >& input,
111  // out:
112  image2d< value::int_u<n> >& r,
113  image2d< value::int_u<n> >& g,
114  image2d< value::int_u<n> >& b)
115  {
116  mln_trace("scribo::preprocessing::internal::split");
117  initialize(r, input);
118  initialize(g, input);
119  initialize(b, input);
120 
121  // Generic version:
122 
123  // mln_piter(box2d) p(input.domain());
124  // for_all(p)
125  // {
126  // r(p) = input(p).red();
127  // g(p) = input(p).green();
128  // b(p) = input(p).blue();
129  // }
130 
131  typedef image2d< value::rgb<n> > I;
132  typedef image2d< value::int_u<n> > O;
133 
134  mln_pixter(const I) p_i(input);
135  mln_pixter(O) p_r(r);
136  mln_pixter(O) p_g(g);
137  mln_pixter(O) p_b(b);
138  for (p_i.start(), p_r.start(), p_g.start(), p_b.start();
139  p_i.is_valid();
140  p_i.next(), p_r.next(), p_g.next(), p_b.next())
141  {
142  const value::rgb<n>& c = p_i.val();
143  p_r.val() = c.red();
144  p_g.val() = c.green();
145  p_b.val() = c.blue();
146  }
147 
148  }
149 
150 
151  template <unsigned n>
153  merge(const image2d< value::int_u<n> >& r,
154  const image2d< value::int_u<n> >& g,
155  const image2d< value::int_u<n> >& b)
156  {
157  mln_trace("scribo::preprocessing::internal::merge");
158  image2d< value::rgb<n> > output(r.domain());
159 
160  // mln_piter(box2d) p(output.domain());
161  // for_all(p)
162  // {
163  // value::rgb<n>& c = output(p);
164  // c.red() = r(p);
165  // c.green() = g(p);
166  // c.blue() = b(p);
167  // }
168 
169  typedef image2d< value::int_u<n> > I;
170  mln_pixter(const I) p_r(r);
171  mln_pixter(const I) p_g(g);
172  mln_pixter(const I) p_b(b);
173  typedef image2d< value::rgb<n> > O;
174  mln_pixter(O) p_o(output);
175 
176  for (p_o.start(), p_r.start(), p_g.start(), p_b.start();
177  p_o.is_valid();
178  p_o.next(), p_r.next(), p_g.next(), p_b.next())
179  {
180  value::rgb<n>& c = p_o.val();
181  c.red() = p_r.val();
182  c.green() = p_g.val();
183  c.blue() = p_b.val();
184  }
185 
186  return output;
187  }
188 
189 
190  inline
192  diff_abs(const image2d< value::rgb8 >& input1,
193  const image2d< value::rgb8 >& input2)
194  {
195  image2d< value::rgb8 > output(input1.domain());
196  mln_piter_(box2d) p(input1.domain());
197  for_all(p)
198  {
199  value::rgb8& c = output(p);
200  c.red() = math::diff_abs(input1(p).red(), input2(p).red());
201  c.green() = math::diff_abs(input1(p).green(), input2(p).green());
202  c.blue() = math::diff_abs(input1(p).blue(), input2(p).blue());
203  }
204  return output;
205  }
206 
207 
208  inline
210  inverted_diff_abs(const image2d< value::rgb8 >& input1,
211  const image2d< value::rgb8 >& input2)
212  {
213  image2d< value::rgb8 > output(input1.domain());
214  mln_piter_(box2d) p(input1.domain());
215  for_all(p)
216  {
217  value::rgb8& c = output(p);
218 // c.red() = 255 - math::diff_abs(input1(p).red(), input2(p).red());
219 // c.green() = 255 - math::diff_abs(input1(p).green(), input2(p).green());
220 // c.blue() = 255 - math::diff_abs(input1(p).blue(), input2(p).blue());
221 
222  c.red() = 255 - math::min(2 * math::diff_abs(input1(p).red(), input2(p).red()), 255);
223  c.green() = 255 - math::min(2 * math::diff_abs(input1(p).green(), input2(p).green()), 255);
224  c.blue() = 255 - math::min(2 * math::diff_abs(input1(p).blue(), input2(p).blue()), 255);
225  }
226  return output;
227  }
228 
229 
230  inline
231  unsigned dist(const rgb8& c1, const rgb8& c2)
232  {
233  unsigned d = 0;
234  d += std::abs(c1.red() - c2.red());
235  d += std::abs(c1.green() - c2.green());
236  d += std::abs(c1.blue() - c2.blue());
237  return d;
238  }
239 
240 
241  inline
243  background_analyze(const image2d<rgb8>& input,
244  unsigned lambda, unsigned delta)
245  {
246  mln_trace("scribo::preprocessing::internal::background_analyze");
247 
248  image2d<value::int_u8> r, g, b;
249  split(input, r, g, b);
250  image2d<rgb8> closed, opened;
251  closed = merge(morpho::closing::area(r, c4(), lambda),
252  morpho::closing::area(g, c4(), lambda),
253  morpho::closing::area(b, c4(), lambda));
254  opened = merge(morpho::opening::area(r, c4(), lambda),
255  morpho::opening::area(g, c4(), lambda),
256  morpho::opening::area(b, c4(), lambda));
257 
258  image2d<bool> mask(input.domain());
259  mln_piter_(box2d) p(input.domain());
260  for_all(p)
261  mask(p) = (dist(closed(p), opened(p)) >= delta);
262 
263  image2d<rgb8> output = duplicate(input);
264  data::fill((output | pw::value(mask)).rw(), rgb8(255,255,0));
265 
266 // {
267 // io::ppm::save(output, "temp_output.ppm");
268 // io::pbm::save(mask, "temp_mask.pbm");
269 // }
270 
271  unsigned nblobs;
272  image2d<unsigned> lab, con;
273  lab = labeling::blobs(mask, c4(), nblobs);
274 
275 // {
276 // io::ppm::save(labeling::colorize(rgb8(),
277 // lab,
278 // nblobs),
279 // "temp_blobs.ppm");
280 // }
281 
282  con = morpho::elementary::dilation(lab, c8());
283  data::fill((con | (pw::value(lab) != pw::cst(0u))).rw(),
284  0u);
285 
286 // {
287 // io::ppm::save(labeling::colorize(rgb8(),
288 // con,
289 // nblobs),
290 // "temp_con.ppm");
291 // }
292 
293  typedef accu::stat::mean< rgb8, algebra::vec<3,float>, rgb8 > A;
294  mln::util::array<rgb8> m = labeling::compute(A(), input, con, nblobs);
295 
296  data::fill((output | pw::value(mask)).rw(),
297  data::transform(lab, m));
298 
299  return output;
300  }
301 
302 
303  } // end of namespace scribo::internal
304 
305 
306 
307 
308  // Facade
309 
310  template <typename I>
311  mln::util::couple<mln_concrete(I), mln_concrete(I)>
312  split_bg_fg(const Image<I>& input_, unsigned lambda, unsigned delta)
313  {
314  mln_trace("scribo::preprocessing::split_bg_fg");
315 
316  using namespace mln;
317 
318  const I& input = exact(input_);
319 
320  mln_precondition(input.is_valid());
321  mln_precondition(lambda >= 1);
322  mln_precondition(delta >= 1);
323 
325  bg = internal::background_analyze(input, lambda, delta);
326  image2d<value::rgb8> fg = internal::inverted_diff_abs(input, bg);
327 
328  return mln::make::couple(bg, fg);
329  }
330 
331 
332 # endif // ! MLN_INCLUDE_ONLY
333 
334 
335  } // end of namespace scribo::preprocessing
336 
337 } // end of namespace scribo
338 
339 #endif // ! SCRIBO_PREPROCESSING_SPLIT_BG_FG_HH