$extrastylesheet
Olena  User documentation 2.1
An Image Processing Platform
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
sauvola_ms_split.cc
1 // Copyright (C) 2009, 2010, 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 #include <mln/core/image/image2d.hh>
28 #include <mln/value/rgb8.hh>
29 #include <mln/io/magick/load.hh>
30 #include <mln/io/pbm/save.hh>
31 
32 #include <scribo/binarization/sauvola_ms_split.hh>
33 #include <scribo/debug/option_parser.hh>
34 #include <scribo/debug/logger.hh>
35 
36 static const scribo::debug::arg_data arg_desc[] =
37 {
38  { "input.*", "An image." },
39  { "output.pbm", "A binary image." },
40  {0, 0}
41 };
42 
43 
44 // --enable/disable-<name>
45 static const scribo::debug::toggle_data toggle_desc[] =
46 {
47  // name, description, default value
48  {0, 0, false}
49 };
50 
51 
52 // --<name> <args>
53 static const scribo::debug::opt_data opt_desc[] =
54 {
55  // name, description, arguments, check args function, number of args, default arg
56  { "debug-prefix", "Enable debug image outputs. Prefix image name with that "
57  "given prefix.", "<prefix>", 0, 1, 0 },
58  { "all-k", "Sauvola's formulae parameter. Set it globally for all scales.",
59  "<value>", 0, 1, "0.34" },
60 
61  { "k2", "Sauvola's formulae parameter", "<value>", 0, 1, 0 },
62  { "k3", "Sauvola's formulae parameter", "<value>", 0, 1, 0 },
63  { "k4", "Sauvola's formulae parameter", "<value>", 0, 1, 0 },
64 
65  { "min-ntrue", "The number of components in which a site must be set to 'True' in"
66  " order to be set to 'True' in the output (Possible values: 1, 2, 3).",
67  "<num>", scribo::debug::check_sauvola_split_ntrue, 1, "2" },
68  { "s", "First subsampling ratio. Possible values: 2 or 3.", "ratio",
69  scribo::debug::check_sauvola_first_subsampling, 1, "3" },
70  { "verbose", "Enable verbose mode (mute, time, low, medium, full)",
71  "<mode>", scribo::debug::check_verbose_mode, 1, "mute" },
72  { "win-size", "Window size at scale 1", "<size>", 0, 1, "101" },
73  {0, 0, 0, 0, 0, 0}
74 };
75 
76 
77 int main(int argc, char *argv[])
78 {
79  using namespace mln;
80  using namespace scribo;
81 
82  scribo::debug::option_parser options(arg_desc, toggle_desc, opt_desc);
83 
84  if (!options.parse(argc, argv))
85  return 1;
86 
87  // Enable debug output.
88  if (options.is_set("debug-prefix"))
89  {
90  scribo::debug::logger().set_filename_prefix(options.opt_value("debug-prefix").c_str());
91  scribo::debug::logger().set_level(scribo::debug::All);
92  }
93 
94  mln_trace("main");
95 
96  // Window size
97  unsigned w_1 = atoi(options.opt_value("win-size").c_str()); // Scale 1
98 
99  // First subsampling scale.
100  unsigned s = atoi(options.opt_value("s").c_str());
101  unsigned min_ntrue = atoi(options.opt_value("min-ntrue").c_str());
102 
103 
104  // Setting k parameter.
105  double k = atof(options.opt_value("all-k").c_str());
106  binarization::internal::k2 = k;
107  binarization::internal::k3 = k;
108  binarization::internal::k4 = k;
109 
110  // Override k parameter for specific scales.
111  if (options.is_set("k2"))
112  binarization::internal::k2 = atof(options.opt_value("k2").c_str());
113  if (options.is_set("k3"))
114  binarization::internal::k3 = atof(options.opt_value("k3").c_str());
115  if (options.is_set("k4"))
116  binarization::internal::k4 = atof(options.opt_value("k4").c_str());
117 
118  scribo::debug::logger() << "Using w_1=" << w_1 << " - s=" << s
119  << " - k2=" << binarization::internal::k2
120  << " - k3=" << binarization::internal::k3
121  << " - k4=" << binarization::internal::k4
122  << std::endl;
123 
124  image2d<value::rgb8> input_1;
125  io::magick::load(input_1, options.arg("input.*"));
126 
128  output = scribo::binarization::sauvola_ms_split(input_1, w_1, s, min_ntrue);
129 
130  io::pbm::save(output, options.arg("output.pbm"));
131 }
132 
133