$extrastylesheet
Olena  User documentation 2.1
An Image Processing Platform
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
text_in_doc_functor.hh
1 // Copyright (C) 2010, 2011 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_TOOLCHAIN_INTERNAL_TEXT_IN_DOC_FUNCTOR_HH
28 # define SCRIBO_TOOLCHAIN_INTERNAL_TEXT_IN_DOC_FUNCTOR_HH
29 
33 
34 # ifndef SCRIBO_NDEBUG
35 # include <mln/util/timer.hh>
36 # endif // ! SCRIBO_NDEBUG
37 
38 # include <scribo/core/def/lbl_type.hh>
39 
40 # include <scribo/primitive/extract/components.hh>
41 # include <scribo/primitive/extract/vertical_separators.hh>
42 # include <scribo/primitive/extract/separators_nonvisible.hh>
43 
44 # include <scribo/primitive/remove/separators.hh>
45 
46 # include <scribo/filter/object_links_bbox_h_ratio.hh>
47 # include <scribo/filter/objects_small.hh>
48 
49 # include <scribo/primitive/group/from_single_link.hh>
50 
51 # include <scribo/primitive/link/merge_double_link.hh>
52 # include <scribo/primitive/link/internal/dmax_width_and_height.hh>
53 # include <scribo/primitive/link/with_single_left_link_dmax_ratio.hh>
54 # include <scribo/primitive/link/with_single_right_link_dmax_ratio.hh>
55 
56 # include <scribo/preprocessing/denoise_fg.hh>
57 
58 # include <scribo/text/recognition.hh>
59 # include <scribo/text/merging.hh>
60 
61 # include <scribo/make/debug_filename.hh>
62 
63 # include <scribo/debug/logger.hh>
64 # include <scribo/debug/decision_image.hh>
65 # include <scribo/debug/bboxes_image.hh>
66 # include <scribo/debug/linked_bboxes_image.hh>
67 # include <scribo/debug/bboxes_enlarged_image.hh>
68 # include <scribo/debug/mean_and_base_lines_image.hh>
69 # include <scribo/debug/looks_like_a_text_line_image.hh>
70 
71 # include <scribo/toolchain/internal/toolchain_functor.hh>
72 
73 
74 namespace scribo
75 {
76 
77  namespace toolchain
78  {
79 
80  namespace internal
81  {
82 
86  template <typename I>
88  : public Toolchain_Functor
89  {
90  typedef scribo::def::lbl_type V;
91  typedef mln_ch_value(I,V) L;
92 
94 
95  virtual int nsteps() const;
96 
97  //===============
98  // Core function
99  //===============
100 
101  line_set<L> operator()(const Image<I>& input_);
102 
103 
104  //=========
105  // Options
106  //=========
107  bool enable_denoising;
108  bool enable_line_seps;
109  bool enable_whitespace_seps;
110 
111  //============
112  // Parameters
113  //============
114 
115  std::string ocr_language;
116 
117 
118 # ifndef SCRIBO_NDEBUG
119  //=============
120  // DEBUG TOOLS
121  //=============
122  virtual void on_start();
123  virtual void on_end();
124  virtual void on_progress();
125 
127  mln::util::timer gt;
128 # endif // ! SCRIBO_NDEBUG
129 
130 
131  // Results
132  line_set<L> output;
133  };
134 
135 
136 # ifndef MLN_INCLUDE_ONLY
137 
138  template <typename I>
140  : enable_denoising(true),
141  enable_line_seps(true),
142  enable_whitespace_seps(true),
143  ocr_language("eng")
144  {
145  verbose = false;
146  }
147 
148 
149  //===============
150  // Core function
151  //===============
152 
153  template <typename I>
155  text_in_doc_functor<I>::operator()(const Image<I>& input)
156  {
157  on_start();
158 
159  // Remove separators
160  mln_ch_value(I,bool)
161  separators,
162  input_cleaned = exact(input);
163 
164  if (enable_line_seps)
165  {
166  on_new_progress_label("Find vertical separators...");
167 
168  // Vertical separators
169  separators = primitive::extract::vertical_separators(input, 81);
170 
171  on_progress();
172 
173  on_new_progress_label("Remove separators...");
174 
175  input_cleaned = primitive::remove::separators(input, separators);
176 
177  on_progress();
178  }
179 
180  mln_ch_value(I,bool) whitespaces;
181  if (enable_whitespace_seps)
182  {
183  // Whitespace separators
184  on_new_progress_label("Find whitespace separators...");
185 
186  whitespaces = primitive::extract::separators_nonvisible(input);
187 
188  on_progress();
189  }
190 
191  if (debug::logger().is_enabled())
192  {
193  if (enable_whitespace_seps)
194  debug::logger().log_image(debug::AuxiliaryResults,
195  whitespaces, "whitespaces");
196 
197  if (enable_line_seps)
198  {
199  debug::logger().log_image(debug::AuxiliaryResults,
200  separators, "vseparators");
201 
202  debug::logger().log_image(debug::AuxiliaryResults,
203  input_cleaned, "input_wo_vseparators");
204  }
205  }
206 
207  // Denoise
208  if (enable_denoising)
209  {
210  on_new_progress_label("Denoise...");
211 
212  input_cleaned = preprocessing::denoise_fg(input_cleaned, c8(), 3);
213 
214  if (debug::logger().is_enabled())
215  debug::logger().log_image(debug::AuxiliaryResults,
216  input_cleaned, "denoised");
217 
218  on_progress();
219  }
220 
222  on_new_progress_label("Finding components...");
223 
224  V ncomponents;
225  component_set<L>
227  ncomponents);
228 
229  on_progress();
230 
232  if (enable_line_seps)
233  components.add_separators(separators);
234  if (enable_whitespace_seps)
235  components.add_separators(whitespaces);
236 
237  if (debug::logger().is_enabled())
238  debug::logger().log_image(debug::AuxiliaryResults,
239  components.separators(), "all_separators");
240 
241 
242  on_new_progress_label("Filtering components");
243 
244  components = scribo::filter::components_small(components, 3);
245 
246  on_progress();
247 
248 
250  on_new_progress_label("Linking objects...");
251 
252  object_links<L> left_link
254  primitive::link::internal::dmax_width_and_height(1),
255  anchor::MassCenter);
256  object_links<L> right_link
258  primitive::link::internal::dmax_width_and_height(1),
259  anchor::MassCenter);
260 
261  if (debug::logger().is_enabled())
262  {
264  debug::AuxiliaryResults,
266  left_link,
267  right_link,
268  literal::blue,
269  literal::cyan,
270  literal::yellow,
271  literal::green,
272  anchor::MassCenter),
273  "object_links");
274  }
275 
276 
277  // Validating left and right links.
278  object_links<L>
279  merged_links = primitive::link::merge_double_link(left_link,
280  right_link);
281 
282  on_progress();
283 
284 
285  on_new_progress_label("Filtering objects");
286 
287  // Remove links if bboxes have too different sizes.
288  object_links<L> hratio_filtered_links
289  = filter::object_links_bbox_h_ratio(merged_links, 2.5f);
290 
291 
292  if (debug::logger().is_enabled())
293  {
294  mln_ch_value(I,value::rgb8)
295  hratio_decision_image = scribo::debug::decision_image(input,
296  merged_links,
297  hratio_filtered_links,
298  anchor::MassCenter);
299  debug::logger().log_image(debug::AuxiliaryResults,
300  hratio_decision_image,
301  "hratio_links_decision_image");
302  }
303 
304  on_progress();
305 
306 
307  on_new_progress_label("Rebuilding lines");
308 
309  object_groups<L>
310  groups = primitive::group::from_single_link(hratio_filtered_links);
311 
312 
313 
314  // Construct a line set.
315  line_set<L>
316  lines = scribo::make::line_set(groups);
317 
318 
319  //===== DEBUG =====
320 
321  if (debug::logger().is_enabled())
322  {
323 
324  // Bboxes image.
325  debug::logger().log_image(debug::AuxiliaryResults,
326  scribo::debug::bboxes_image(input, lines),
327  "step1_bboxes");
328 
329  // Bboxes enlarged
330  debug::logger().log_image(debug::AuxiliaryResults,
332  "step1_bboxes_enlarged");
333 
334  // Looks like a text line
335  debug::logger().log_image(debug::AuxiliaryResults,
337  "step1_looks_like_a_text_line");
338 
339 
340  // mean and base lines.
341  debug::logger().log_image(debug::AuxiliaryResults,
343  "step1_x_height");
344 
345  }
346  //===== END OF DEBUG =====
347 
348 
349 
350  lines = scribo::text::merging(lines);
351 
352 
353 
354  //===== DEBUG =====
355 
356  if (debug::logger().is_enabled())
357  {
358 
359  // mean and base lines.
360  debug::logger().log_image(debug::AuxiliaryResults,
362  "step2_x_height");
363 
364  // Looks like a text line
365  debug::logger().log_image(debug::AuxiliaryResults,
367  "step2_looks_like_a_text_line");
368 
369  // Bboxes image.
370  debug::logger().log_image(debug::AuxiliaryResults,
371  scribo::debug::bboxes_image(input, lines),
372  "step2_bboxes");
373 
374  }
375 
376 
377 
378  if (debug::logger().is_enabled())
379  {
380  std::ofstream file(scribo::make::debug_filename("step2_bboxes_100p.txt").c_str());
381 
382  for_all_lines(l, lines)
383  if (lines(l).tag() != line::Merged
384  && lines(l).tag() != line::Ignored
385  && lines(l).tag() != line::Pathological)
386  {
387  file << lines(l).bbox().pmin().row() << " "
388  << lines(l).bbox().pmin().col() << " "
389  << lines(l).bbox().pmax().row() << " "
390  << lines(l).bbox().pmax().col() << " "
391  << lines(l).card() << " "
392  << lines(l).baseline() << " "
393  << lines(l).x_height() << " "
394  << lines(l).meanline() << " "
395  << lines(l).d_height() << " "
396  << lines(l).a_height() << " "
397  << lines(l).char_space() << " "
398  << lines(l).char_width() << std::endl;
399 
400  }
401 
402  file.close();
403  }
404 
405 
406  //===== END OF DEBUG =====
407 
408  on_progress();
409 
410  on_new_progress_label("Recognizing text");
411 
412  scribo::text::recognition(lines, ocr_language.c_str());
413 
414  on_progress();
415 
416  on_end();
417 
418  output = lines;
419  return output;
420  }
421 
422 
423 
424  template<typename I>
425  int
426  text_in_doc_functor<I>::nsteps() const
427  {
428  return 6 + enable_denoising + enable_line_seps
429  + enable_whitespace_seps;
430  }
431 
432 
433 # ifndef SCRIBO_NDEBUG
434 
435  template <typename I>
436  void
437  text_in_doc_functor<I>::on_start()
438  {
439  gt.start();
440  t.start();
441  }
442 
443  template <typename I>
444  void
445  text_in_doc_functor<I>::on_end()
446  {
447  gt.stop();
448  if (verbose)
449  std::cout << "Total time: " << gt << std::endl;
450  }
451 
452  template <typename I>
453  void
454  text_in_doc_functor<I>::on_progress()
455  {
456  t.stop();
457  if (verbose)
458  std::cout << t << std::endl;
459  t.restart();
460  }
461 
462 
463 # endif // ! SCRIBO_NDEBUG
464 
465 
466 # endif // ! MLN_INCLUDE_ONLY
467 
468 
469  } // end of namespace scribo::toolchain::internal
470 
471  } // end of namespace scribo::toolchain
472 
473 } // end of namespace scribo
474 
475 #endif // ! SCRIBO_TOOLCHAIN_INTERNAL_TEXT_IN_DOC_FUNCTOR_HH