$extrastylesheet
Olena  User documentation 2.1
An Image Processing Platform
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
paragraphs_in_image.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_IN_IMAGE_HH
27 # define SCRIBO_FILTER_PARAGRAPHS_IN_IMAGE_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 
40 # include <mln/data/transform.hh>
41 # include <mln/draw/box_plain.hh>
42 
43 # include <mln/util/array.hh>
44 
45 # include <mln/pw/all.hh>
46 
47 # include <scribo/core/document.hh>
48 # include <scribo/core/paragraph_set.hh>
49 # include <scribo/fun/v2b/objects_small_filter.hh>
50 # include <scribo/primitive/extract/components.hh>
51 
52 
53 namespace scribo
54 {
55 
56  namespace filter
57  {
58 
59  using namespace mln;
60 
61 
70  //
71  template <typename L>
72  void
73  paragraphs_in_image(document<L>& doc);
74 
75 
76 # ifndef MLN_INCLUDE_ONLY
77 
78  template <typename L>
79  void
80  paragraphs_in_image(document<L>& doc)
81  {
82  mln_trace("scribo::filter::paragraphs_in_image");
83 
84  mln_precondition(doc.has_elements());
85  mln_precondition(doc.has_text());
86 
87  image2d<bool> billboard;
88  initialize(billboard, doc.lines().components().labeled_image());
89  data::fill(billboard, false);
90 
91  // Draw Image bboxes.
92  for_all_elements(e, doc.elements())
93  if (doc.elements()(e).is_valid()
94  && doc.elements()(e).type() == component::Image)
95  mln::draw::box_plain(billboard, doc.elements()(e).bbox(), true);
96 
97  const paragraph_set<L>& parset = doc.paragraphs();
98  mln::util::array<bool> not_to_ignore(parset.nelements() + 1, true);
99  not_to_ignore(0) = false;
100 
101  for_all_paragraphs(cur_id, parset)
102  {
103  const box2d& b_ = parset(cur_id).bbox();
104  const bool
105  tl = billboard(b_.pmin()),
106  tr = billboard.at_(b_.pmin().row(), b_.pmax().col()),
107  mc = billboard.at_(b_.pcenter().row(), b_.pcenter().col()),
108  bl = billboard.at_(b_.pmax().row(), b_.pmin().col()),
109  br = billboard(b_.pmax());
110 
111  typedef mln::util::set<int> set_t;
112  set_t s;
113  s.insert(tl);
114  s.insert(tr);
115  s.insert(mc);
116  s.insert(bl);
117  s.insert(br);
118 
119  if (s.nelements() > 2 || (s.nelements() == 2 && !s.has(0)))
120  continue;
121 
122  // The paragraph is fully included in an image.
123  for_all_elements(e, s)
124  if (s[e] != 0
125  && (mc != 0 && mc == s[e]
126  && ((tl == mc && bl == mc)
127  || (tr == mc && br == mc)
128  || (tl == mc && tr == mc)
129  || (bl == mc && br == mc))))
130  {
131 // if (tl && tr && ml && mc && mr && bl && br)
132  not_to_ignore(cur_id) = false;
133  break;
134  }
135  }
136 
137  paragraph_set<L> output = parset.duplicate();
138  output.invalidate(not_to_ignore);
139  doc.set_paragraphs(output);
140 
141  }
142 
143 
144 # endif // ! MLN_INCLUDE_ONLY
145 
146  } // end of namespace scribo::filter
147 
148 } // end of namespace scribo
149 
150 #endif // ! SCRIBO_FILTER_PARAGRAPHS_IN_IMAGE_HH