$extrastylesheet
Olena  User documentation 2.1
An Image Processing Platform
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
paragraphs_bbox_overlap.hh
1 // Copyright (C) 2011 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 SCRIBO_FILTER_PARAGRAPHS_BBOX_OVERLAP_HH
27 # define SCRIBO_FILTER_PARAGRAPHS_BBOX_OVERLAP_HH
28 
33 
34 
35 # include <mln/core/concept/image.hh>
36 # include <mln/core/concept/neighborhood.hh>
37 # include <mln/core/concept/function.hh>
38 # include <mln/core/image/dmorph/image_if.hh>
39 # include <mln/data/transform.hh>
40 # include <mln/util/array.hh>
41 # include <mln/draw/box_plain.hh>
42 
43 # include <scribo/core/paragraph_set.hh>
44 # include <scribo/util/box_intersection.hh>
45 
46 namespace scribo
47 {
48 
49  namespace filter
50  {
51 
52  using namespace mln;
53 
54 
63  //
64  template <typename L>
65  paragraph_set<L>
66  paragraphs_bbox_overlap(const paragraph_set<L>& parset);
67 
68 
69 # ifndef MLN_INCLUDE_ONLY
70 
71  namespace internal
72  {
73 
74  template <typename L>
75  struct order_paragraphs_id
76  {
77  order_paragraphs_id(const scribo::paragraph_set<L>& parset)
78  : parset_(parset)
79  {
80  }
81 
82  bool operator()(const scribo::paragraph_id_t& l1,
83  const scribo::paragraph_id_t& l2) const
84  {
85  const unsigned l1_nsites = parset_(l1).bbox().nsites();
86  const unsigned l2_nsites = parset_(l2).bbox().nsites();
87 
88  if (l1_nsites == l2_nsites)
89  return l1 > l2;
90  return l1_nsites > l2_nsites;
91  }
92 
94  };
95 
96  } // end of namespace scribo::filter::internal
97 
98 
99  template <typename L>
100  paragraph_set<L>
101  paragraphs_bbox_overlap(const paragraph_set<L>& parset)
102  {
103  mln_trace("scribo::filter::paragraphs_bbox_overlap");
104 
105  mln_precondition(parset.is_valid());
106 
107  mln_ch_value(L, paragraph_id_t) billboard;
108  initialize(billboard, parset.lines().components().labeled_image());
109  data::fill(billboard, 0);
110 
111  mln::util::array<bool> not_to_ignore(parset.nelements() + 1, true);
112  not_to_ignore(0) = false;
113 
114  paragraph_set<L> output = parset.duplicate();
115 
116  mln::util::array<paragraph_id_t> candidate;
117  candidate.reserve(parset.nelements());
118  for_all_paragraphs(cur_id, parset)
119  if (parset(cur_id).is_valid())
120  candidate.append(cur_id);
121 
122  std::sort(candidate.hook_std_vector_().begin(),
123  candidate.hook_std_vector_().end(),
124  internal::order_paragraphs_id<L>(parset));
125 
126  for_all_elements(e, candidate)
127  {
128  paragraph_id_t cur_id = candidate(e);
129 
130  const box2d& b_ = parset(cur_id).bbox();
131 
132  if (parset(cur_id).nlines() > 3)
133  {
134  mln::draw::box_plain(billboard, b_, cur_id);
135  continue;
136  }
137 
138  const unsigned mc = billboard.at_(b_.pcenter().row(), b_.pcenter().col());
139 
140  // Box is mostly in the background => do nothing.
141  if (mc == 0)
142  {
143  mln::draw::box_plain(billboard, b_, cur_id);
144  continue;
145  }
146  else // Bbox center is inside another box. Check if we can
147  // merge the current box with it.
148  {
149  // Consider other potential overlapping bboxes.
150  const unsigned tl = billboard(b_.pmin());
151  const unsigned tr = billboard.at_(b_.pmin().row(), b_.pmax().col());
152  const unsigned bl = billboard.at_(b_.pmax().row(), b_.pmin().col());
153  const unsigned br = billboard(b_.pmax());
154 
155  typedef std::set<unsigned> set_t;
156  set_t labels;
157  labels.insert(tl);
158  labels.insert(tr);
159  labels.insert(mc);
160  labels.insert(bl);
161  labels.insert(br);
162 
163  // FIXME: check that there are at least 3 points (including
164  // the center) in another paragraph.
165 
166  // The potential merged bbox is already ignored or the
167  // current bbox overlaps with several bboxes.
168  // => Ignore current bbox .
169  //
170  if (!not_to_ignore(mc)
171  || (labels.size() > 1 && labels.find(0) == labels.end()))
172  {
173  mln::draw::box_plain(billboard, b_, cur_id); // Really?
174  not_to_ignore(cur_id) = false;
175  continue;
176  }
177 
178  for (set_t::const_iterator it = labels.begin();
179  it != labels.end(); ++it)
180  if (*it)
181  {
182  mln_assertion(*it != mc);
183 
184  box2d b2 = output(*it).bbox();
185  box2d b_i = scribo::util::box_intersection(b_, b2);
186  volatile float
187  b_ratio = b_i.nsites() / (float)b_.nsites();
188 
189  // If the bbox is widely included in another box.
190  if (b_ratio > 0.8)
191  {
192  output(mc).fast_merge(output(cur_id));
193  mln::draw::box_plain(billboard, parset(mc).bbox(), mc);
194  }
195  else
196  mln::draw::box_plain(billboard, parset(cur_id).bbox(), cur_id);
197  break;
198  }
199 
200  }
201  }
202 
203  // if (not_to_ignore(*it))
204  // {
205  // box2d b2 = output(*it).bbox();
206  // box2d b_i = scribo::util::box_intersection(b_, b2);
207 
208  // volatile float
209  // b_ratio = b_i.nsites() / (float)b_.nsites(),
210  // b2_ratio = b_i.nsites() / (float)b2.nsites();
211 
212  // if (b2_ratio == 1)
213  // {
214  // // Merge paragraphs and redraw the new bbox.
215  // output(cur_id).fast_merge(output(*it));
216  // mln::draw::box_plain(billboard, output(cur_id).bbox(), cur_id);
217  // }
218  // else if (b_ratio == 1)
219  // {
220  // // Merge paragraphs and redraw the new bbox.
221  // output(*it).fast_merge(output(cur_id));
222  // mln::draw::box_plain(billboard, output(*it).bbox(), *it);
223  // }
224  // else if ((b_ratio > 0.4 || b2_ratio > 0.9))
225  // {
226  // // si b_ est inclus dans une boite dont le nombre de
227  // // comp > 4 => invalid juste b_ sinon => invalid b_ et
228  // // b2
229  // not_to_ignore(cur_id) = false;
230 
231  // if (parset(*it).nlines() < 4)
232  // not_to_ignore(*it) = false;
233  // }
234  // }
235 
236  // mln::draw::box_plain(billboard, b_, cur_id);
237  // }
238 
239  output.invalidate(not_to_ignore);
240 
241  for_all_paragraphs(p, output)
242  if (output(p).is_valid())
243  output(p).force_stats_update();
244 
245  return output;
246  }
247 
248 
249 # endif // ! MLN_INCLUDE_ONLY
250 
251  } // end of namespace scribo::filter
252 
253 } // end of namespace scribo
254 
255 #endif // ! SCRIBO_FILTER_PARAGRAPHS_BBOX_OVERLAP_HH