* scribo/binarization/internal/local_threshold_core.hh,
* scribo/binarization/internal/local_threshold_debug.hh,
* scribo/binarization/internal/sauvola_ms_functor.hh,
* scribo/binarization/sauvola_ms.hh,
* src/binarization/sauvola_ms_debug.cc: Here.
---
scribo/ChangeLog | 10 ++
.../binarization/internal/local_threshold_core.hh | 12 +-
.../binarization/internal/local_threshold_debug.hh | 1 +
.../binarization/internal/sauvola_ms_functor.hh | 10 ++
scribo/scribo/binarization/sauvola_ms.hh | 23 +++-
scribo/src/binarization/sauvola_ms_debug.cc | 119 +++++++++-----------
6 files changed, 100 insertions(+), 75 deletions(-)
diff --git a/scribo/ChangeLog b/scribo/ChangeLog
index 545889d..d5671d3 100644
--- a/scribo/ChangeLog
+++ b/scribo/ChangeLog
@@ -1,5 +1,15 @@
2012-08-23 Guillaume Lazzara <z(a)lrde.epita.fr>
+ Fix and improve sauvola_ms debug.
+
+ * scribo/binarization/internal/local_threshold_core.hh,
+ * scribo/binarization/internal/local_threshold_debug.hh,
+ * scribo/binarization/internal/sauvola_ms_functor.hh,
+ * scribo/binarization/sauvola_ms.hh,
+ * src/binarization/sauvola_ms_debug.cc: Here.
+
+2012-08-23 Guillaume Lazzara <z(a)lrde.epita.fr>
+
Fix invalid line offset due to input image size.
* scribo/binarization/internal/niblack_functor.hh,
diff --git a/scribo/scribo/binarization/internal/local_threshold_core.hh
b/scribo/scribo/binarization/internal/local_threshold_core.hh
index 9ea81f9..7979da3 100644
--- a/scribo/scribo/binarization/internal/local_threshold_core.hh
+++ b/scribo/scribo/binarization/internal/local_threshold_core.hh
@@ -48,6 +48,7 @@
# include <mln/io/pgm/save.hh>
# include <mln/io/pbm/save.hh>
# include <mln/data/saturate.hh>
+# include <mln/debug/filename.hh>
# endif // ! SCRIBO_LOCAL_THRESHOLD_DEBUG
# include <mln/util/timer.hh>
@@ -226,19 +227,20 @@ namespace scribo
# ifdef SCRIBO_LOCAL_THRESHOLD_DEBUG
if (stddev_image_output)
io::pgm::save(data::saturate(value::int_u8(), debug_stddev),
- stddev_image_output);
+ mln::debug::filename(stddev_image_output));
if (mean_image_output)
io::pgm::save(data::saturate(value::int_u8(), debug_mean),
- mean_image_output);
+ mln::debug::filename(mean_image_output));
if (threshold_image_output)
io::pgm::save(data::saturate(value::int_u8(), debug_threshold),
- threshold_image_output);
+ mln::debug::filename(threshold_image_output));
if (alpham_image_output)
io::pgm::save(data::saturate(value::int_u8(), debug_alpham),
- alpham_image_output);
+ mln::debug::filename(alpham_image_output));
if (alphacond_image_output)
- io::pbm::save(debug_alphacond, alphacond_image_output);
+ io::pbm::save(debug_alphacond,
+ mln::debug::filename(alphacond_image_output));
# endif // ! SCRIBO_LOCAL_THRESHOLD_DEBUG
trace::exiting("scribo::binarization::internal::local_threshold_core");
diff --git a/scribo/scribo/binarization/internal/local_threshold_debug.hh
b/scribo/scribo/binarization/internal/local_threshold_debug.hh
index f728df3..6b2263f 100644
--- a/scribo/scribo/binarization/internal/local_threshold_debug.hh
+++ b/scribo/scribo/binarization/internal/local_threshold_debug.hh
@@ -55,6 +55,7 @@ namespace scribo
const char* stddev_image_output = 0;
const char* mean_image_output = 0;
const char* threshold_image_output = 0;
+ const char* full_threshold_image_output = 0;
const char* scale_image_output = 0;
const char* scale_iz_image_output = 0;
diff --git a/scribo/scribo/binarization/internal/sauvola_ms_functor.hh
b/scribo/scribo/binarization/internal/sauvola_ms_functor.hh
index 9a23aa4..0775f1c 100644
--- a/scribo/scribo/binarization/internal/sauvola_ms_functor.hh
+++ b/scribo/scribo/binarization/internal/sauvola_ms_functor.hh
@@ -67,6 +67,10 @@ namespace scribo
image2d<bool> msk;
image2d<value::int_u8> t_sub;
+# ifdef SCRIBO_LOCAL_THRESHOLD_DEBUG
+ image2d<bool> full_msk;
+# endif // ! SCRIBO_LOCAL_THRESHOLD_DEBUG
+
unsigned n_nbhs;
mln::util::array<int> dp;
@@ -111,6 +115,8 @@ namespace scribo
initialize(msk, input);
# ifdef SCRIBO_LOCAL_THRESHOLD_DEBUG
+ initialize(full_msk, input);
+ mln::extension::fill(full_msk, false);
initialize(debug_mean, input);
initialize(debug_stddev, input);
# endif // ! SCRIBO_LOCAL_THRESHOLD_DEBUG
@@ -137,6 +143,10 @@ namespace scribo
mln::convert::from_to(formula_(mean, stddev, K_, R_), t_p);
msk.element(p) = input.element(p) < t_p;
+# ifdef SCRIBO_LOCAL_THRESHOLD_DEBUG
+ full_msk.element(p) = msk.element(p);
+# endif // ! SCRIBO_LOCAL_THRESHOLD_DEBUG
+
t_sub.element(p) = t_p;
if (! msk.element(p))
{
diff --git a/scribo/scribo/binarization/sauvola_ms.hh
b/scribo/scribo/binarization/sauvola_ms.hh
index fe9f1f0..70964ea 100644
--- a/scribo/scribo/binarization/sauvola_ms.hh
+++ b/scribo/scribo/binarization/sauvola_ms.hh
@@ -72,7 +72,7 @@
# ifdef SCRIBO_LOCAL_THRESHOLD_DEBUG
# include <scribo/binarization/internal/local_threshold_debug.hh>
# include <mln/io/pgm/save.hh>
-# include <scribo/make/debug_filename.hh>
+# include <mln/debug/filename.hh>
# endif // ! SCRIBO_LOCAL_THRESHOLD_DEBUG
# include <mln/util/timer.hh>
@@ -228,8 +228,11 @@ namespace scribo
if (f.parent.element(p) == p)
{
// test over the component cardinality
- f.msk.element(p) = f.card.element(p) > lambda_min
- && f.card.element(p) < lambda_max;
+ f.msk.element(p) = f.card.element(p) > lambda_min;
+// && f.card.element(p) < lambda_max;
+# ifdef SCRIBO_LOCAL_THRESHOLD_DEBUG
+ f.full_msk.element(p) = true;
+# endif // ! SCRIBO_LOCAL_THRESHOLD_DEBUG
if (f.msk.element(p) && e_2(sq) == 0u)
{
@@ -242,6 +245,9 @@ namespace scribo
{
// Propagation
f.msk.element(p) = f.msk.element(f.parent.element(p));
+# ifdef SCRIBO_LOCAL_THRESHOLD_DEBUG
+ f.full_msk.element(p) = f.full_msk.element(f.parent.element(p));
+# endif // ! SCRIBO_LOCAL_THRESHOLD_DEBUG
if (f.msk.element(p) && e_2(sq) == 0u)
{
@@ -266,7 +272,10 @@ namespace scribo
# ifdef SCRIBO_LOCAL_THRESHOLD_DEBUG
if (internal::threshold_image_output)
io::pbm::save(f.msk,
- scribo::make::debug_filename(internal::threshold_image_output).c_str());
+ mln::debug::filename(internal::threshold_image_output));
+ if (internal::full_threshold_image_output)
+ io::pbm::save(f.full_msk,
+ mln::debug::filename(internal::full_threshold_image_output));
# endif // ! SCRIBO_LOCAL_THRESHOLD_DEBUG
return f.t_sub;
@@ -906,7 +915,8 @@ namespace scribo
# ifdef SCRIBO_LOCAL_THRESHOLD_DEBUG
if (internal::scale_image_output)
- io::pgm::save(e_2, internal::scale_image_output);
+ io::pgm::save(e_2,
+ mln::debug::filename(internal::scale_image_output));
# endif // ! SCRIBO_LOCAL_THRESHOLD_DEBUG
// Propagate scale values.
@@ -919,7 +929,8 @@ namespace scribo
# ifdef SCRIBO_LOCAL_THRESHOLD_DEBUG
if (internal::scale_iz_image_output)
- io::pgm::save(e_2, internal::scale_iz_image_output);
+ io::pgm::save(e_2,
+ mln::debug::filename(internal::scale_iz_image_output));
# endif // ! SCRIBO_LOCAL_THRESHOLD_DEBUG
// Binarize
diff --git a/scribo/src/binarization/sauvola_ms_debug.cc
b/scribo/src/binarization/sauvola_ms_debug.cc
index 839a3f7..63fbefe 100644
--- a/scribo/src/binarization/sauvola_ms_debug.cc
+++ b/scribo/src/binarization/sauvola_ms_debug.cc
@@ -32,50 +32,38 @@
#include <mln/fun/v2v/rgb_to_luma.hh>
#include <scribo/binarization/sauvola_ms.hh>
-#include <scribo/debug/usage.hh>
+#include <scribo/debug/option_parser.hh>
+#include <scribo/debug/logger.hh>
-bool check_args(int argc, char * argv[])
+static const scribo::debug::arg_data arg_desc[] =
{
- if (argc < 3 || argc > 7)
- return false;
-
- if (argc >= 6)
- {
- int s = atoi(argv[5]);
-
- if (s < 1 || s > 3)
- {
- std::cout << "s must be set to 2 or 3."
- << std::endl;
- return false;
- }
- }
-
- return true;
-}
+ { "input.*", "An image." },
+ { "output.pbm", "A binary image." },
+ {0, 0}
+};
-const char *args_desc[][2] =
+// --enable/disable-<name>
+static const scribo::debug::toggle_data toggle_desc[] =
{
- { "input.*", "An image." },
- { "out.pbm", "A binary image." },
- { "scale.pgm", "Image of scales used for binarization." },
- { "w", "Window size at scale 1. (default: 101)" },
- { "s", "First subsampling ratio (default: 3)." },
- { "k", "Sauvola's formuale parameter (default: 0.34)" },
- {0, 0}
+ // name, description, default value
+ {0, 0, false}
};
-namespace scribo
+
+// --<name> <args>
+static const scribo::debug::opt_data opt_desc[] =
{
- namespace binarization
- {
- namespace internal
- {
- extern image2d<value::int_u8> debug_k;
- }
- }
-}
+ // name, description, arguments, check args function, number of args, default arg
+ { "debug-prefix", "Enable debug image outputs. Prefix image name with
that "
+ "given prefix.", "<prefix>", 0, 1, 0 },
+ { "k", "Sauvola's formulae parameter",
"<value>", 0, 1, "0.34" },
+ { "s", "First subsampling ratio. Possible values: 2 or 3.",
"ratio",
+ scribo::debug::check_sauvola_first_subsampling, 1, "3" },
+ { "verbose", "Enable verbose mode", 0, 0, 0, 0 },
+ { "win-size", "Window size at scale 1", "<size>",
0, 1, "101" },
+ {0, 0, 0, 0, 0, 0}
+};
@@ -85,59 +73,62 @@ int main(int argc, char *argv[])
using namespace mln;
using namespace scribo;
- if (!check_args(argc, argv))
- return scribo::debug::usage(argv,
- "Multi-Scale Binarization based on Sauvola's algorithm.",
- "input.* output.pbm <scale.pgm> <w> <s> <k>",
- args_desc);
+ scribo::debug::option_parser options(arg_desc, toggle_desc, opt_desc);
- Magick::InitializeMagick(0);
+ if (!options.parse(argc, argv))
+ return 1;
+
+ // Enable debug output.
+ if (options.is_set("debug-prefix"))
+ {
+
scribo::debug::logger().set_filename_prefix(options.opt_value("debug-prefix").c_str());
+ scribo::debug::logger().set_level(scribo::debug::All);
+ }
+
+ Magick::InitializeMagick(*argv);
trace::entering("main");
- // Window size
- unsigned w_1;
- if (argc >= 5)
- w_1 = atoi(argv[4]); // Scale 1
- else
- w_1 = 101u;
+ bool verbose = options.is_set("verbose");
+ // Window size
+ unsigned w_1 = atoi(options.opt_value("win-size").c_str());
// First subsampling scale.
- unsigned s;
- if (argc >= 6)
- s = atoi(argv[5]);
- else
- s = 3u;
-
- double k;
- if (argc >= 7)
- k = atof(argv[6]);
- else
- k = 0.34f;
+ unsigned s = atoi(options.opt_value("s").c_str());
+ double k = atof(options.opt_value("k").c_str());
- if (argc >= 4)
- scribo::binarization::internal::scale_image_output = argv[3];
+ if (verbose)
+ std::cout << "Using w_1=" << w_1 << " - s="
<< s
+ << " - k=" << k << std::endl;
scribo::binarization::internal::scale_image_output = "scale_image.pgm";
scribo::binarization::internal::threshold_image_output =
"threshold_image.pbm";
+ scribo::binarization::internal::full_threshold_image_output =
"full_threshold_image.pbm";
scribo::binarization::internal::scale_iz_image_output = "scale_iz.pgm";
// Load
image2d<value::rgb8> input_1;
- io::magick::load(input_1, argv[1]);
-
+ io::magick::load(input_1, options.arg("input.*"));
// Convert to Gray level image.
image2d<value::int_u8>
input_1_gl = data::transform(input_1,
mln::fun::v2v::rgb_to_luma<value::int_u8>());
+ mln::util::timer t;
+ t.start();
// Binarize.
image2d<bool>
output = scribo::binarization::sauvola_ms(input_1_gl, w_1, s, k);
- io::pbm::save(output, argv[2]);
+ if (verbose)
+ {
+ t.stop();
+ std::cout << "binarized in " << t << "s"
<< std::endl;
+ }
+
+ io::pbm::save(output, options.arg("output.pbm"));
}
--
1.7.2.5