$extrastylesheet
Olena  User documentation 2.1
An Image Processing Platform
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
content_in_hdoc.cc
1 // Copyright (C) 2010, 2011, 2012, 2013 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 #include <libgen.h>
29 #include <fstream>
30 #include <iostream>
31 
32 #include <mln/core/image/image2d.hh>
33 
34 #include <mln/io/magick/load.hh>
35 
36 #include <scribo/toolchain/content_in_hdoc.hh>
37 #include <scribo/toolchain/text_in_doc_preprocess.hh>
38 
39 #include <scribo/core/document.hh>
40 
41 #include <scribo/debug/usage.hh>
42 #include <scribo/debug/logger.hh>
43 
44 #include <scribo/preprocessing/crop_without_localization.hh>
45 #include <scribo/preprocessing/crop.hh>
46 
47 #include <scribo/io/xml/save.hh>
48 #include <scribo/io/img/save.hh>
49 
50 
51 #include <mln/core/alias/neighb2d.hh>
52 #include <mln/labeling/compute.hh>
53 #include <mln/labeling/foreground.hh>
54 #include <mln/util/timer.hh>
55 
56 
57 
58 const char *args_desc[][2] =
59 {
60  { "input.*", "An image." },
61  { "out.xml", "Result of the document analysis." },
62  { "denoise_enabled", "1 enables denoising, 0 disables it. (enabled by default)" },
63  { "pmin_row", "Row index of the top left corner of the Region of interest." },
64  { "pmin_col", "Col index of the top left corner of the Region of interest." },
65  { "pmax_row", "Row index of the bottom right corner of the Region of interest." },
66  { "pmax_col", "Col index of the bottom right corner of the Region of interest." },
67  { "language", "Language to be used for the text recognition. [eng|fra] (Default: eng)."
68  "An empty language will disable OCR." },
69  { "find_lines", "Find vertical lines. (Default 1)" },
70  { "find_whitespaces", "Find whitespaces separators. (Default 1)" },
71  { "K", "Sauvola's binarization threshold parameter. (Default: 0.34)" },
72  { "debug_dir", "Output directory for debug image" },
73  {0, 0}
74 };
75 
76 
77 
78 int main(int argc, char* argv[])
79 {
80  using namespace scribo;
81  using namespace mln;
82 
83  if (argc < 3 || argc > 14)
84  return scribo::debug::usage(argv,
85  "Find text lines and elements in a document",
86  "input.* out.xml <denoise_enabled> [<pmin_row> <pmin_col> <pmax_row> <pmax_col>] [language] [find_lines] [find_whitespaces] [K] [debug_dir]",
87  args_desc);
88 
89  // Enable debug output.
90  if (argc == 9 || argc == 13)
91  {
93  scribo::debug::logger().set_level(scribo::debug::All);
94  scribo::make::internal::debug_filename_prefix = argv[argc - 1];
95  }
96 
97  mln_trace("main");
98 
100  t.start();
101 
103  image2d<value::rgb8> input;
104  mln::io::magick::load(input, argv[1]);
105 
106  // Preprocess document
107  image2d<bool> input_preproc;
108  {
109  double K = 0.34;
110  if (argc == 8 || argc >= 12)
111  {
112  if (argc == 8)
113  K = atof(argv[7]);
114  else
115  K = atof(argv[argc - 2]);
116  std::cout << "Using K = " << K << std::endl;
117  }
118 
119  input_preproc = toolchain::text_in_doc_preprocess(input, false, 0, K,
120  false, false);
121 
122  // Cleanup components on borders
123  {
124  typedef scribo::def::lbl_type V;
125  V nlabels;
126  image2d<V> lbl = labeling::foreground(input_preproc, c8(), nlabels);
128  bbox = labeling::compute(accu::shape::bbox<point2d>(), lbl, nlabels);
129 
130  const box2d& b = input.domain();
131  for_all_ncomponents(e, nlabels)
132  if (bbox(e).pmin().row() == b.pmin().row()
133  || bbox(e).pmax().row() == b.pmax().row()
134  || bbox(e).pmin().col() == b.pmin().col()
135  || bbox(e).pmax().col() == b.pmax().col())
136  data::fill(((input_preproc | bbox(e)).rw() | (pw::value(lbl) == pw::cst(e))).rw(), false);
137  }
138  }
139 
140  // Optional Cropping
141  point2d crop_shift = literal::origin;
142  if (argc >= 12)
143  {
145  minr = atoi(argv[4]),
146  minc = atoi(argv[5]),
147  maxr = atoi(argv[6]),
148  maxc = atoi(argv[7]);
149 
150  std::cout << "> Image cropped from (" << minr << "," << minc << ")"
151  << " to (" << maxr << "," << maxc << ")" << std::endl;
152 
153  box2d roi = mln::make::box2d(minr, minc, maxr, maxc);
154  input_preproc = preprocessing::crop_without_localization(input_preproc, roi);
155  crop_shift = point2d(minr, minc);
156 
157  scribo::debug::logger().log_image(scribo::debug::Results, input_preproc,
158  "input_preproc_cropped.pbm");
159  }
160 
161  bool denoise = (argc > 3 && atoi(argv[3]) != 0);
162 
163  std::string language = "eng";
164  if (argc >= 5 && argc < 13)
165  language = argv[4];
166  else if (argc >= 12)
167  language = argv[8];
168 
169  bool find_line_seps = true;
170  if (argc >= 6 && argc < 13)
171  find_line_seps = (atoi(argv[5]) != 0);
172  else if (argc >= 12)
173  find_line_seps = (atoi(argv[9]) != 0);
174 
175  bool find_whitespace_seps = true;
176  if (argc >= 7 && argc < 13)
177  find_whitespace_seps = (atoi(argv[6]) != 0);
178  else if (argc >= 12)
179  find_whitespace_seps = (atoi(argv[10]) != 0);
180 
181  std::cout << "Running with the following options :"
182  << " ocr_language = " << language
183  << " | find_lines_seps = " << find_line_seps
184  << " | find_whitespace_seps = " << find_whitespace_seps
185  << " | debug = " << scribo::debug::logger().is_enabled()
186  << std::endl;
187 
188  // Run document toolchain.
189 
190  // Text
191  std::cout << "Analysing document..." << std::endl;
193  doc = scribo::toolchain::content_in_hdoc(input, input_preproc, denoise,
194  find_line_seps, find_whitespace_seps,
195  !language.empty(), language);
196 
197  doc.set_filename(basename(argv[1]));
198 
199  // Saving results
200  std::cout << "Saving results..." << std::endl;
201  scribo::io::xml::save(doc, "page.xml", scribo::io::xml::Page);
202 
203  std::cout << "End of process - " << t << std::endl;
204 
205 // scribo::io::xml::save(doc, argv[2], scribo::io::xml::PageExtended);
206 
207  // scribo::io::img::save(doc, "debug.png", scribo::io::img::DebugWoImage);
208  // std::cout << "Debug image saved " << t << std::endl;
209  // scribo::io::img::save(doc, "full.png", scribo::io::img::DebugWithImage);
210  // std::cout << "Full Debug image saved " << t << std::endl;
211  // scribo::io::img::save(doc, "full_hd.png", scribo::io::img::Full);
212 
213 // sleep(10);
214 
215 }