$extrastylesheet
Olena  User documentation 2.1
An Image Processing Platform
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
text_in_picture_dual.cc
1 // Copyright (C) 2010, 2011, 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 #include <libgen.h>
28 #include <iostream>
29 
30 #include <mln/io/magick/load.hh>
31 
32 #include <scribo/toolchain/text_in_picture.hh>
33 #include <scribo/debug/option_parser.hh>
34 #include <scribo/debug/logger.hh>
35 
36 #include <mln/world/rgb/invert.hh>
37 #include <scribo/primitive/merge/components.hh>
38 
39 static const scribo::debug::arg_data arg_desc[] =
40 {
41  { "input.*", "An image." },
42  { "output.ppm", "A color image where the text is highlighted." },
43  {0, 0}
44 };
45 
46 // --enable/disable-<name>
47 static const scribo::debug::toggle_data toggle_desc[] =
48 {
49  // name, description, default value
50  { "fg-extraction", "Detect and slit foreground/background components. (default: disabled)", false },
51  { "ms-bin", "Use a multi-scale binarization. (default: enabled)", true },
52  {0, 0, false}
53 };
54 
55 
56 // --<name> <args>
57 static const scribo::debug::opt_data opt_desc[] =
58 {
59  // name, description, arguments, check args function, number of args, default arg
60  { "debug-prefix", "Enable debug image outputs. Prefix image name with that "
61  "given prefix.", "<prefix>", 0, 1, 0 },
62  { "max-dim-size", "Set the maximum size of the largest image dimension.", "<size>", 0, 1, "1024" },
63  { "lambda", "Set the maximum area of the background objects. It is only useful if fg-extraction is enabled.", "<size>",
64  0, 1, "0" },
65  { "verbose", "Enable verbose mode", 0, 0, 0, 0 },
66  {0, 0, 0, 0, 0, 0}
67 };
68 
69 
70 int main(int argc, char* argv[])
71 {
72  using namespace scribo;
73  using namespace scribo::primitive;
74  using namespace mln;
75 
76  scribo::debug::option_parser options(arg_desc, toggle_desc, opt_desc);
77 
78  if (!options.parse(argc, argv))
79  return 1;
80 
81  if (options.is_set("debug-prefix"))
82  {
83  scribo::debug::logger().set_filename_prefix(options.opt_value("debug-prefix").c_str());
84  scribo::debug::logger().set_level(scribo::debug::All);
85  }
86 
87 
88  mln_trace("main");
89 
90  image2d<value::rgb8> input_rgb;
91  io::magick::load(input_rgb, argv[1]);
92 
93  bool verbose = options.is_set("verbose");
94  unsigned max_dim_size = atoi(options.opt_value("max-dim-size").c_str());
95  bool fg_extraction = options.is_enabled("fg-extraction");
96  bool multi_scale_bin = options.is_enabled("ms-bin");
97  unsigned lambda = atoi(options.opt_value("lambda").c_str());
98 
99  if (verbose)
100  std::cout << "Using max_dim_size = " << max_dim_size
101  << " - fg_extraction = " << fg_extraction
102  << " - multi_scale_bin = " << multi_scale_bin
103  << " - lambda = " << lambda << std::endl;
104 
105 
108  comps = toolchain::text_in_picture(input_rgb, fg_extraction, multi_scale_bin,
109  max_dim_size, lambda, verbose);
110 
111 
114  comps_neg = toolchain::text_in_picture(world::rgb::invert(input_rgb),
115  fg_extraction, multi_scale_bin,
116  max_dim_size, lambda, verbose);
117 
118 
119  component_set<L> merged_comps = primitive::merge::components(comps, comps_neg);
120 
121 
123  merged_comps.labeled_image(),
124  merged_comps.nelements()),
125  options.arg("output.ppm"));
126 
127 # ifndef SCRIBO_NDEBUG
129  {
130  scribo::debug::logger().log_image(scribo::debug::Results,
132  merged_comps),
133  "input_with_bboxes.ppm");
134  scribo::debug::logger().log_image(scribo::debug::Results,
136  merged_comps),
137  "out_text.ppm");
138  }
139 # endif // ! SCRIBO_NDEBUG
140 
141  std::cout << "# objects = " << merged_comps.nelements() << std::endl;
142 
143  return merged_comps.nelements() != 0;
144 }