$extrastylesheet
Olena  User documentation 2.1
An Image Processing Platform
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
sauvola_ms.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/int_u8.hh>
31 #include <mln/io/magick/all.hh>
32 #include <mln/data/transform.hh>
33 #include <mln/fun/v2v/rgb_to_luma.hh>
34 #include <mln/util/timer.hh>
35 #include <mln/logical/not.hh>
36 
37 #include <scribo/binarization/sauvola_ms.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.*", "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  { "negate", "Negate output image.", false},
54  {0, 0, false}
55 };
56 
57 
58 // --<name> <args>
59 static const scribo::debug::opt_data opt_desc[] =
60 {
61  // name, description, arguments, check args function, number of args, default arg
62  { "debug-prefix", "Enable debug image outputs. Prefix image name with that "
63  "given prefix.", "<prefix>", 0, 1, 0 },
64  { "all-k", "Sauvola's formulae parameter. Set it globally for all scales.",
65  "<value>", 0, 1, "0.34" },
66 
67  { "k2", "Force Sauvola's formulae parameter value for scale 2", "<value>", 0, 1, 0 },
68  { "k3", "Force Sauvola's formulae parameter value for scale 3", "<value>", 0, 1, 0 },
69  { "k4", "Force Sauvola's formulae parameter value for scale 4", "<value>", 0, 1, 0 },
70 
71 
72  { "s", "First subsampling ratio. Possible values: 2 or 3.", "ratio",
73  scribo::debug::check_sauvola_first_subsampling, 1, "3" },
74  { "verbose", "Enable verbose mode (mute, time, low, medium, full)",
75  "<mode>", scribo::debug::check_verbose_mode, 1, "mute" },
76  { "win-size", "Window size at scale 1", "<size>", 0, 1, "101" },
77  {0, 0, 0, 0, 0, 0}
78 };
79 
80 
81 
82 
83 
84 
85 int main(int argc, char *argv[])
86 {
87  using namespace mln;
88  using namespace scribo;
89 
90  scribo::debug::option_parser options(arg_desc, toggle_desc, opt_desc);
91 
92  if (!options.parse(argc, argv))
93  return 1;
94 
95  // Enable debug output.
96  if (options.is_set("debug-prefix"))
97  {
98  scribo::debug::logger().set_filename_prefix(options.opt_value("debug-prefix").c_str());
99  scribo::debug::logger().set_level(scribo::debug::All);
100  }
101 
102  mln_trace("main");
103 
104 
105  // Window size
106  unsigned w_1 = atoi(options.opt_value("win-size").c_str());
107  // First subsampling scale.
108  unsigned s = atoi(options.opt_value("s").c_str());
109 
110  // Setting k parameter.
111  double k = atof(options.opt_value("all-k").c_str());
112  binarization::internal::k2 = k;
113  binarization::internal::k3 = k;
114  binarization::internal::k4 = k;
115 
116  // Override k parameter for specific scales.
117  if (options.is_set("k2"))
118  binarization::internal::k2 = atof(options.opt_value("k2").c_str());
119  if (options.is_set("k3"))
120  binarization::internal::k3 = atof(options.opt_value("k3").c_str());
121  if (options.is_set("k4"))
122  binarization::internal::k4 = atof(options.opt_value("k4").c_str());
123 
124  scribo::debug::logger() << "Using w_1=" << w_1 << " - s=" << s
125  << " - k2=" << binarization::internal::k2
126  << " - k3=" << binarization::internal::k3
127  << " - k4=" << binarization::internal::k4
128  << std::endl;
129 
130 
131  // Load
132  image2d<value::rgb8> input_1;
133  io::magick::load(input_1, options.arg("input.*"));
134 
135  // Convert to Gray level image.
137  input_1_gl = data::transform(input_1,
139 
140 
142 
143  // Binarize
145  output = scribo::binarization::sauvola_ms(input_1_gl, w_1, s);
146 
147  scribo::debug::logger().stop_time_logging("Binarized in");
148 
149  if (options.is_enabled("negate"))
150  io::magick::save(logical::not_(output), options.arg("output.*"));
151  else
152  io::magick::save(output, options.arg("output.*"));
153 }