$extrastylesheet
Olena  User documentation 2.1
An Image Processing Platform
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
sauvola_ms_fg.cc
1 // Copyright (C) 2009, 2010, 2011, 2012, 2013 EPITA Research and
2 // Development 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 
28 
29 #include <mln/core/image/image2d.hh>
30 #include <mln/value/rgb8.hh>
31 #include <mln/io/magick/load.hh>
32 #include <mln/io/pbm/save.hh>
33 #include <mln/data/transform.hh>
34 #include <mln/fun/v2v/rgb_to_luma.hh>
35 
36 #include <scribo/binarization/sauvola_ms.hh>
37 #include <scribo/preprocessing/split_bg_fg.hh>
38 #include <scribo/debug/option_parser.hh>
39 #include <scribo/debug/logger.hh>
40 
41 static const scribo::debug::arg_data arg_desc[] =
42 {
43  { "input.*", "An image." },
44  { "output.pbm", "A binary image." },
45  {0, 0}
46 };
47 
48 
49 // --enable/disable-<name>
50 static const scribo::debug::toggle_data toggle_desc[] =
51 {
52  // name, description, default value
53  {0, 0, false}
54 };
55 
56 
57 // --<name> <args>
58 static const scribo::debug::opt_data opt_desc[] =
59 {
60  // name, description, arguments, check args function, number of args, default arg
61  { "debug-prefix", "Enable debug image outputs. Prefix image name with that "
62  "given prefix.", "<prefix>", 0, 1, 0 },
63  { "all-k", "Sauvola's formulae parameter", "<value>", 0, 1, "0.34" },
64 
65  { "k2", "Sauvola's formulae parameter", "<value>", 0, 1, "0.20" },
66  { "k3", "Sauvola's formulae parameter", "<value>", 0, 1, "0.30" },
67  { "k4", "Sauvola's formulae parameter", "<value>", 0, 1, "0.50" },
68 
69  { "lambda", "Set the maximum area of the background objects. It is only "
70  "useful if fg-extraction is enabled.", "<size>", 0, 1, "1024" },
71  { "s", "First subsampling ratio. Possible values: 2 or 3.", "ratio",
72  scribo::debug::check_sauvola_first_subsampling, 1, "3" },
73  { "verbose", "Enable verbose mode (mute, time, low, medium, full)",
74  "<mode>", scribo::debug::check_verbose_mode, 1, "mute" },
75  { "win-size", "Window size at scale 1", "<size>", 0, 1, "101" },
76  {0, 0, 0, 0, 0, 0}
77 };
78 
79 
80 
81 
82 int main(int argc, char *argv[])
83 {
84  using namespace mln;
85  using namespace scribo;
86 
87  scribo::debug::option_parser options(arg_desc, toggle_desc, opt_desc);
88 
89  if (!options.parse(argc, argv))
90  return 1;
91 
92  // Enable debug output.
93  if (options.is_set("debug-prefix"))
94  {
95  scribo::debug::logger().set_filename_prefix(options.opt_value("debug-prefix").c_str());
96  scribo::debug::logger().set_level(scribo::debug::All);
97  }
98 
99  mln_trace("main");
100 
101  unsigned lambda = atoi(options.opt_value("lambda").c_str());
102 
103  // Window size
104  unsigned w_1 = atoi(options.opt_value("win-size").c_str()); // Scale 1
105 
106  // First subsampling scale.
107  unsigned s = atoi(options.opt_value("s").c_str());
108 
109  // Setting k parameter.
110  double k = atof(options.opt_value("all-k").c_str());
111  binarization::internal::k2 = k;
112  binarization::internal::k3 = k;
113  binarization::internal::k4 = k;
114 
115  // Override k parameter for specific scales.
116  if (options.is_set("k2"))
117  binarization::internal::k2 = atof(options.opt_value("k2").c_str());
118  if (options.is_set("k3"))
119  binarization::internal::k3 = atof(options.opt_value("k3").c_str());
120  if (options.is_set("k4"))
121  binarization::internal::k4 = atof(options.opt_value("k4").c_str());
122 
123  scribo::debug::logger() << "Using w_1=" << w_1 << " - s=" << s
124  << " - k2=" << binarization::internal::k2
125  << " - k3=" << binarization::internal::k3
126  << " - k4=" << binarization::internal::k4
127  << std::endl;
128 
129  // Load
130  image2d<value::rgb8> input_1;
131  io::magick::load(input_1, options.arg("input.*"));
132 
133  // Split foreground/background
135  fg = scribo::preprocessing::split_bg_fg(input_1, lambda, 32).first();
136 
137  // Convert to Gray level image.
140 
141  // Binarize
143  output = scribo::binarization::sauvola_ms(fg_gl, w_1, s);
144 
145  io::pbm::save(output, options.arg("output.pbm"));
146 }
147 
148