---
scribo/ChangeLog | 4 ++
scribo/sauvola_fast.cc | 85 +++++++++++++++++++++++++++++++----------------
2 files changed, 60 insertions(+), 29 deletions(-)
diff --git a/scribo/ChangeLog b/scribo/ChangeLog
index 5d4003e..cf29967 100644
--- a/scribo/ChangeLog
+++ b/scribo/ChangeLog
@@ -1,5 +1,9 @@
2012-08-23 Guillaume Lazzara <z(a)lrde.epita.fr>
+ * sauvola_fast.cc: Fix bugs. Stable version.
+
+2012-08-23 Guillaume Lazzara <z(a)lrde.epita.fr>
+
* sauvola_fast.cc: New implementation.
2012-08-23 Guillaume Lazzara <z(a)lrde.epita.fr>
diff --git a/scribo/sauvola_fast.cc b/scribo/sauvola_fast.cc
index 8018135..95e49b2 100644
--- a/scribo/sauvola_fast.cc
+++ b/scribo/sauvola_fast.cc
@@ -17,43 +17,63 @@ namespace mln
typedef mln_ch_value(I,bool) bin_t;
bin_t bin;
- mln_fwd_pixter(const I) pi;
- mln_fwd_pixter(bin_t) po;
+ image2d<double> th_;
+
+ const mln_value(I)* pi;
+ bool* po;
double K_;
double R_;
scribo::binarization::internal::sauvola_formula formula_;
- unsigned count_;
+ int step_;
+ unsigned next_line1;
+ unsigned next_line2;
+ unsigned next_line3;
+ unsigned offset1;
+ unsigned offset2;
sauvola_fast_functor(const I& input_, mln_ch_value(I,bool)& bin_, double K,
double R)
: input(input_),
bin(bin_),
- pi(input),
- po(bin),
+ pi(&input(input.domain().pmin())),
+ po(&bin(bin.domain().pmin())),
K_(K),
R_(R)
{
- pi.start();
- po.start();
- count_ = 0;
+ step_ = 3;
+ next_line1 = 2 * input.border();
+ next_line2 = input.delta_index(dpoint2d(+1,0)) + next_line1;
+ next_line3 = input.delta_index(dpoint2d(+2,0)) + next_line1;
+
+ offset1 = input.delta_index(dpoint2d(+1,0));
+ offset2 = input.delta_index(dpoint2d(+2,0));
}
void exec(double mean, double stddev)
{
static point2d p(0,0);
- po.val() = (pi.val() <= formula_(p, mean, stddev, K_, R_));
- pi.next(); // next pixel
- po.next(); // next pixel
+ double th = formula_(p, mean, stddev, K_, R_);
+
+ for (int i = 0; i < step_; ++i, ++po, ++pi)
+ {
+ *po = (*pi <= th);
+ *(po + offset1) = (*(pi + offset1) <= th);
+ *(po + offset2) = (*(pi + offset2) <= th);
+ }
+ }
- ++count_;
+ void end_of_row(int)
+ {
+ po += next_line3;
+ pi += next_line3;
}
+
void finalize()
{
- std::cout << input.domain().nsites() << " - " <<
input.domain().nrows() * input.domain().ncols() << " - " << count_
<< std::endl;
}
};
@@ -64,7 +84,7 @@ namespace mln
template <typename I, typename J>
void
init_integral(const Image<I>& input_,
- Image<J>& integral_sum_sum_2_)
+ Image<J>& integral_sum_sum_2_)
{
trace::entering("subsampling::impl::integral_3");
@@ -90,23 +110,23 @@ namespace mln
unsigned row = 0;
unsigned b_offset = input.delta_index(dpoint2d(input.border(),
- input.border()));
+ input.border()));
p_integ += b_offset;
{
S h_sum = 0, h_sum_2 = 0;
const V* ptr1 = & input.at_(row, 0);
for (unsigned col = 0; col < ncols; ++col)
{
- V v = *ptr1++;
+ V v = *ptr1++;
- h_sum += v;
- h_sum_2 += v * v;
+ h_sum += v;
+ h_sum_2 += v * v;
- // exception
- p_integ->first() = h_sum;
- p_integ->second() = h_sum_2;
+ // exception
+ p_integ->first() = h_sum;
+ p_integ->second() = h_sum_2;
- ++p_integ;
+ ++p_integ;
}
}
@@ -120,15 +140,15 @@ namespace mln
const V* ptr1 = & input.at_(row, 0);
for (unsigned col = 0; col < ncols; ++col)
{
- V v = *ptr1++;
+ V v = *ptr1++;
- h_sum += v;
- h_sum_2 += v * v;
+ h_sum += v;
+ h_sum_2 += v * v;
- p_integ->first() = h_sum + (p_integ + up)->first();
- p_integ->second() = h_sum_2 + (p_integ + up)->second();
+ p_integ->first() = h_sum + (p_integ + up)->first();
+ p_integ->second() = h_sum_2 + (p_integ + up)->second();
- ++p_integ;
+ ++p_integ;
}
p_integ += b_next;
@@ -142,13 +162,20 @@ namespace mln
image2d<bool>
sauvola_fast(const image2d<value::int_u8>& input, unsigned win)
{
+ util::timer t;
+ t.start();
image2d<util::couple<double,double> > integral;
init_integral(input, integral);
+ t.stop();
+ std::cout << "image integrale - " << t << std::endl;
+ t.restart();
image2d<bool> output;
initialize(output, input);
sauvola_fast_functor<image2d<value::int_u8> > f(input, output,
SCRIBO_DEFAULT_SAUVOLA_K, SCRIBO_DEFAULT_SAUVOLA_R);
- scribo::canvas::integral_browsing(integral, 1, win, win, 1, f);
+ scribo::canvas::integral_browsing(integral, 3, win, win, 1, f);
+ t.stop();
+ std::cout << "Binarization - " << t << std::endl;
return f.bin;
}
--
1.7.2.5