$extrastylesheet
Olena  User documentation 2.1
An Image Processing Platform
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
repair_lines.hh
1 // Copyright (C) 2009, 2011, 2014 EPITA Research and Development Laboratory
2 // (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 SCRIBO_TABLE_INTERNAL_REPAIR_LINES_HH
28 # define SCRIBO_TABLE_INTERNAL_REPAIR_LINES_HH
29 
33 
34 # include <vector>
35 
36 # include <mln/core/image/dmorph/image_if.hh>
37 # include <mln/core/image/dmorph/extension_val.hh>
38 
39 # include <mln/pw/all.hh>
40 
41 # include <mln/literal/zero.hh>
42 
43 # include <mln/core/site_set/box.hh>
44 
45 # include <mln/core/routine/extend.hh>
46 
47 # include <mln/data/fill.hh>
48 
49 # include <mln/util/couple.hh>
50 # include <mln/util/array.hh>
51 # include <mln/util/ord.hh>
52 
53 # include <mln/win/line.hh>
54 
55 # include <mln/labeling/colorize.hh>
56 
57 # include <mln/value/rgb8.hh>
58 # include <mln/value/label_16.hh>
59 
60 # include <scribo/make/debug_filename.hh>
61 
62 # include <scribo/core/central_sites.hh>
63 # include <scribo/core/macros.hh>
64 # include <scribo/table/internal/repair_lines.hh>
65 
66 
67 namespace scribo
68 {
69 
70  namespace table
71  {
72 
73  namespace internal
74  {
75 
76  using namespace mln;
77 
85  //
86  template <unsigned axis, typename I>
87  void
88  repair_lines(const Image<I>& input,
89  mln::util::array<box<mln_site(I)> >& tableboxes,
90  unsigned max_discontinuity);
91 
92 
93 # ifndef MLN_INCLUDE_ONLY
94 
95 
96  template <unsigned axis, typename I>
97  void
98  repair_lines(const Image<I>& input_,
99  mln::util::array<box<mln_site(I)> >& tableboxes,
100  unsigned max_discontinuity)
101  {
102  mln_trace("scribo::table::internal::repair_lines");
103 
104  const I& input = exact(input_);
105  mln_precondition(input.is_valid());
106 
107  typedef mln_site(I) P;
108  typedef win::line<mln_grid(P), axis, mln_coord(P)> line_t;
109 
110  // Initialization
111  typedef mln_ch_value(I,value::label_16) L;
112  L l(input.domain());
113  data::fill(l, literal::zero);
114  for_all_comps(i, tableboxes)
115  {
116  mln::util::couple<P,P> cp = central_sites(tableboxes[i], axis);
117  l(cp.first()) = i;
118  l(cp.second()) = i;
119  }
120 
121  // Repair
122  extension_val<L> l_ext(l, literal::zero);
123 
124  mln::util::array<box<P> > result;
125  std::vector<bool> to_keep(tableboxes.nelements(), true);
126 
127  typedef
129  const image_if<L,
131  pw::cst_<literal::zero_t> > >, const L> tbb_ima_t;
132  tbb_ima_t tbb_ima = extend(l | (pw::value(l) != pw::cst(literal::zero)), l);
133 
134  //FIXME: use a half window, just the bottom of the vertical line.
135  line_t vl(max_discontinuity);
136  mln_piter(tbb_ima_t) p(tbb_ima.domain());
137  mln_qiter(line_t) q(vl, p);
138  for_all(p)
139  {
140  mln::util::couple<P,P> cp_p = central_sites(tableboxes[l_ext(p)], axis);
141  for_all(q)
142  if (l_ext(q) != literal::zero && l_ext(q) != l_ext(p))
143  {
144  if (mln::util::ord_strict(tableboxes[l_ext(p)].pmax(),
145  tableboxes[l_ext(q)].pmax()))
146  {
147  tableboxes[l_ext(p)].pmax() = tableboxes[l_ext(q)].pmax();
148  to_keep[l_ext(q)] = false;
149  }
150 
151  if (mln::util::ord_strict(tableboxes[l_ext(q)].pmin(),
152  tableboxes[l_ext(p)].pmin()))
153  {
154  tableboxes[l_ext(p)].pmin() = tableboxes[l_ext(q)].pmin();
155  to_keep[l_ext(q)] = false;
156  }
157 
158  if (!to_keep[l_ext(q)])
159  {
160  mln::util::couple<P,P> cp_q = central_sites(tableboxes[l_ext(q)], axis);
161  l_ext(cp_q.first()) = literal::zero;
162  l_ext(cp_q.second()) = literal::zero;
163 
164  unsigned p_i = l_ext(p);
165  l_ext(cp_p.first()) = literal::zero;
166  l_ext(cp_p.second()) = literal::zero;
167 
168  mln::util::couple<P,P> new_cp_p = central_sites(tableboxes[p_i], axis);
169  l_ext(new_cp_p.first()) = p_i;
170  l_ext(new_cp_p.second()) = p_i;
171  }
172  }
173  }
174 
175 
176  // Remove merged boxes.
177  for_all_elements(i, tableboxes)
178  if (to_keep[i])
179  result.append(tableboxes[i]);
180 
181  tableboxes = result;
182  }
183 
184 
185 # endif // ! MLN_INCLUDE_ONLY
186 
187  } // end of namespace scribo::table::internal
188 
189  } // end of namespace scribo::table
190 
191 } // end of namespace scribo
192 
193 
194 #endif // ! SCRIBO_TABLE_INTERNAL_REPAIR_LINES_HH