$extrastylesheet
Olena  User documentation 2.1
An Image Processing Platform
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
sauvola_ms_debug.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 
27 #include <mln/core/image/image2d.hh>
28 #include <mln/value/int_u8.hh>
29 #include <mln/io/magick/load.hh>
30 #include <mln/io/pbm/save.hh>
31 #include <mln/data/transform.hh>
32 #include <mln/fun/v2v/rgb_to_luma.hh>
33 #include <mln/data/paste.hh>
34 #include <mln/pw/all.hh>
35 #include <mln/core/image/dmorph/sub_image.hh>
36 #include <mln/core/image/dmorph/image_if.hh>
37 #include <mln/data/convert.hh>
38 #include <mln/labeling/foreground.hh>
39 #include <mln/literal/colors.hh>
40 
41 #include <scribo/binarization/sauvola_ms.hh>
42 #include <scribo/debug/option_parser.hh>
43 #include <scribo/debug/logger.hh>
44 
45 static const scribo::debug::arg_data arg_desc[] =
46 {
47  { "input.*", "An image." },
48  { "output.pbm", "A binary image." },
49  {0, 0}
50 };
51 
52 
53 // --enable/disable-<name>
54 static const scribo::debug::toggle_data toggle_desc[] =
55 {
56  // name, description, default value
57  {0, 0, false}
58 };
59 
60 
61 // --<name> <args>
62 static const scribo::debug::opt_data opt_desc[] =
63 {
64  // name, description, arguments, check args function, number of args, default arg
65  { "debug-prefix", "Enable debug image outputs. Prefix image name with that "
66  "given prefix.", "<prefix>", 0, 1, 0 },
67  { "all-k", "Sauvola's formulae parameter", "<value>", 0, 1, "0.34" },
68 
69  { "k2", "Sauvola's formulae parameter", "<value>", 0, 1, 0 },
70  { "k3", "Sauvola's formulae parameter", "<value>", 0, 1, 0 },
71  { "k4", "Sauvola's formulae parameter", "<value>", 0, 1, 0 },
72 
73  { "s", "First subsampling ratio. Possible values: 2 or 3.", "ratio",
74  scribo::debug::check_sauvola_first_subsampling, 1, "3" },
75  { "verbose", "Enable verbose mode (mute, time, low, medium, full)",
76  "<mode>", scribo::debug::check_verbose_mode, 1, "mute" },
77  { "win-size", "Window size at scale 1", "<size>", 0, 1, "101" },
78  {0, 0, 0, 0, 0, 0}
79 };
80 
81 
82 namespace mln
83 {
84 
85  struct scale_to_color : public Function_v2v<scale_to_color>
86  {
87  typedef value::rgb8 result;
88 
89  value::rgb8 operator()(const value::int_u8& v) const
90  {
91  switch (v)
92  {
93  default:
94  case 0:
95  return literal::black;
96 
97  case 2:
98  return literal::green;
99 
100  case 3:
101  return literal::blue;
102 
103  case 4:
104  return literal::red;
105  }
106  }
107 
108  };
109 
110 }
111 
112 
113 int main(int argc, char *argv[])
114 {
115  using namespace mln;
116  using namespace scribo;
117 
118  scribo::debug::option_parser options(arg_desc, toggle_desc, opt_desc);
119 
120  if (!options.parse(argc, argv))
121  return 1;
122 
123  // Enable debug output.
124  if (options.is_set("debug-prefix"))
125  {
126  scribo::debug::logger().set_filename_prefix(options.opt_value("debug-prefix").c_str());
127  scribo::debug::logger().set_level(scribo::debug::All);
128  }
129 
130  mln_trace("main");
131 
132 
133  // Window size
134  unsigned w_1 = atoi(options.opt_value("win-size").c_str());
135  // First subsampling scale.
136  unsigned s = atoi(options.opt_value("s").c_str());
137 
138 
139  // Setting k parameter.
140  double k = atof(options.opt_value("all-k").c_str());
141  binarization::internal::k2 = k;
142  binarization::internal::k3 = k;
143  binarization::internal::k4 = k;
144 
145  // Override k parameter for specific scales.
146  if (options.is_set("k2"))
147  binarization::internal::k2 = atof(options.opt_value("k2").c_str());
148  if (options.is_set("k3"))
149  binarization::internal::k3 = atof(options.opt_value("k3").c_str());
150  if (options.is_set("k4"))
151  binarization::internal::k4 = atof(options.opt_value("k4").c_str());
152 
153  scribo::debug::logger() << "Using w_1=" << w_1 << " - s=" << s
154  << " - k2=" << binarization::internal::k2
155  << " - k3=" << binarization::internal::k3
156  << " - k4=" << binarization::internal::k4
157  << std::endl;
158 
159  scribo::binarization::internal::scale_image_output = "scale_image.pgm";
160  scribo::binarization::internal::threshold_image_output = "threshold_image.pbm";
161  scribo::binarization::internal::full_threshold_image_output = "full_threshold_image.pbm";
162  scribo::binarization::internal::scale_iz_image_output = "scale_iz.pgm";
163  scribo::binarization::internal::scale_proba_output = "scale_proba.dump";
164  const char *scale_bin_output = "scale_bin.ppm";
165 
166  // Load
167  image2d<value::rgb8> input_1;
168  io::magick::load(input_1, options.arg("input.*"));
169 
170  // Convert to Gray level image.
172  input_1_gl = data::transform(input_1,
174 
176 
177  // Binarize.
179  output = scribo::binarization::sauvola_ms(input_1_gl, w_1, s);
180 
181  scribo::debug::logger().stop_time_logging("Binarized in");
182 
183 # ifdef SCRIBO_LOCAL_THRESHOLD_DEBUG
184  {
185  image2d<value::int_u8> scale_bin;
186  initialize(scale_bin, output);
187 
188  for (unsigned i = 0; i < geom::nrows(output); ++i)
189  for (unsigned j = 0; j < geom::ncols(output); ++j)
190  if (output.at_(i,j))
191  scale_bin.at_(i,j) = binarization::internal::debug_e_2.at_(i/s, j/s);
192  else
193  scale_bin.at_(i,j) = 0;
194 
195  scale_to_color f;
196  io::ppm::save(data::transform(scale_bin, f),
197  mln::debug::filename(scale_bin_output));
198 
199  // Computing size of retrieved objects for each scale.
200  unsigned max = 15000;
201  std::ofstream ostr(mln::debug::filename("area_per_scale.tsv").c_str());
202  for (unsigned j = 0; j < max; ++j)
203  {
204  ostr << j;
205  for (unsigned i = 0; i < 3; ++i)
206  {
207  if (binarization::internal::area_histo[i].find(j) != binarization::internal::area_histo[i].end())
208  ostr << " " << binarization::internal::area_histo[i][j];
209  else
210  ostr << " " << 0;
211  }
212  ostr << std::endl;
213  }
214  ostr.close();
215 
216 
217  }
218 # endif // ! SCRIBO_LOCAL_THRESHOLD_DEBUG
219 
220 
221  io::pbm::save(output, options.arg("output.pbm"));
222 }
223 
224