last-svn-commit-31-g750fe21 Cleanup and avoid warnings in Sauvola related files.

* scribo/binarization/sauvola_ms.hh, * scribo/binarization/sauvola_threshold.hh, * scribo/canvas/integral_browsing.hh, * scribo/src/binarization/sauvola_ms.cc, * scribo/src/binarization/sauvola_pgm.cc,x * scribo/subsampling/integral_single_image.hh: Cleanup and avoir warnings. --- scribo/ChangeLog | 12 + scribo/binarization/sauvola_ms.hh | 845 ++++++++++++++------------- scribo/binarization/sauvola_threshold.hh | 5 - scribo/canvas/integral_browsing.hh | 69 +-- scribo/src/binarization/sauvola_ms.cc | 30 +- scribo/src/binarization/sauvola_pgm.cc | 10 +- scribo/subsampling/integral_single_image.hh | 4 +- 7 files changed, 494 insertions(+), 481 deletions(-) diff --git a/scribo/ChangeLog b/scribo/ChangeLog index 745540f..6eae06e 100644 --- a/scribo/ChangeLog +++ b/scribo/ChangeLog @@ -1,3 +1,15 @@ +2010-02-19 Guillaume Lazzara <z@lrde.epita.fr> + + Cleanup and avoid warnings in Sauvola related files. + + * scribo/binarization/sauvola_ms.hh, + * scribo/binarization/sauvola_threshold.hh, + * scribo/canvas/integral_browsing.hh, + * scribo/src/binarization/sauvola_ms.cc, + * scribo/src/binarization/sauvola_pgm.cc,x + * scribo/subsampling/integral_single_image.hh: Cleanup and avoir + warnings. + 2009-12-15 Guillaume Lazzara <z@lrde.epita.fr> Add a new test for object_image. diff --git a/scribo/binarization/sauvola_ms.hh b/scribo/binarization/sauvola_ms.hh index bc52620..fce5832 100644 --- a/scribo/binarization/sauvola_ms.hh +++ b/scribo/binarization/sauvola_ms.hh @@ -61,640 +61,648 @@ namespace scribo using value::int_u8; - unsigned my_find_root(image2d<unsigned>& parent, unsigned x) + namespace internal { - if (parent.element(x) == x) - return x; - return parent.element(x) = my_find_root(parent, - parent.element(x)); - } + template <typename V> + V my_find_root(image2d<V>& parent, const V& x) + { + if (parent.element(x) == x) + return x; + return parent.element(x) = my_find_root(parent, + parent.element(x)); + } - image2d<int_u8> - compute_t_n_and_e_2(const image2d<int_u8>& sub, image2d<int_u8>& e_2, - unsigned lambda_min, unsigned lambda_max, - unsigned s, - unsigned q, unsigned i, unsigned w, - const image2d<util::couple<double,double> >& integral_sum_sum_2) - { - typedef image2d<int_u8> I; - typedef point2d P; - unsigned ratio = std::pow(q, i - 2); // Ratio in comparison to e_2 + image2d<int_u8> + compute_t_n_and_e_2(const image2d<int_u8>& sub, image2d<int_u8>& e_2, + unsigned lambda_min, unsigned lambda_max, + unsigned s, + unsigned q, unsigned i, unsigned w, + const image2d<util::couple<double,double> >& integral_sum_sum_2) + { + typedef image2d<int_u8> I; + typedef point2d P; - unsigned - w_local = w * ratio, - w_local_h = w_local, - w_local_w = w_local; + unsigned ratio = std::pow(q, i - 2); // Ratio in comparison to e_2 - if (! (w_local % 2)) - { - --w_local_w; - ++w_local_h; - } + unsigned + w_local = w * ratio, + w_local_h = w_local, + w_local_w = w_local; - // 1st pass - scribo::binarization::internal::first_pass_functor< image2d<int_u8> > - f(sub); - scribo::canvas::integral_browsing(integral_sum_sum_2, - ratio, - w_local_w, w_local_h, - s, - f); + if (! (w_local % 2)) + { + --w_local_w; + ++w_local_h; + } - // 2nd pass - { - util::array<mln_value_(I) *> ptr(ratio); - unsigned nrows = geom::nrows(e_2); + // 1st pass + scribo::binarization::internal::first_pass_functor< image2d<int_u8> > + f(sub); + scribo::canvas::integral_browsing(integral_sum_sum_2, + ratio, + w_local_w, w_local_h, + s, + f); - mln_box_runend_piter_(I) sp(sub.domain()); // Backward. - unsigned ncols = sp.run_length(); - for_all(sp) + // 2nd pass { - unsigned p = &sub(sp) - sub.buffer(); // Offset - P site = sp; + util::array<mln_value_(I) *> ptr(ratio); + unsigned nrows = geom::nrows(e_2); + mln_box_runend_piter_(I) sp(sub.domain()); // Backward. + unsigned ncols = sp.run_length(); + for_all(sp) { - P tmp = site * ratio; - - // FIXME: to be removed! - if (tmp.row() + ratio >= nrows) - ptr.resize(nrows - tmp.row()); + unsigned p = &sub(sp) - sub.buffer(); // Offset + P site = sp; - ptr(0) = &e_2(tmp); - // FIXME: pointers could just be updated with an offset. - for (unsigned j = 1; j < ptr.size(); ++j) { - tmp[0] += 1; - ptr(j) = & e_2(tmp); - } - } + P tmp = site * ratio; - for (unsigned j = 0; j < ncols; ++j) - { - if (f.msk.element(p)) - { + // FIXME: to be removed! + if (tmp.row() + ratio >= nrows) + ptr.resize(nrows - tmp.row()); - mln_site_(I) sq = site * ratio; + ptr(0) = &e_2(tmp); + // FIXME: pointers could just be updated with an offset. + for (unsigned j = 1; j < ptr.size(); ++j) + { + tmp[0] += 1; + ptr(j) = & e_2(tmp); + } + } - if (f.parent.element(p) == p) + for (unsigned j = 0; j < ncols; ++j) + { + if (f.msk.element(p)) { - // test over the component cardinality - f.msk.element(p) = f.card.element(p) > lambda_min - && f.card.element(p) < lambda_max; - if (f.msk.element(p) && e_2(sq) == 0u) + mln_site_(I) sq = site * ratio; + + if (f.parent.element(p) == p) { - for (unsigned l = 0; l < ptr.size(); ++l) - std::memset(ptr(l), i, ratio * sizeof(mln_value_(I))); - } + // test over the component cardinality + f.msk.element(p) = f.card.element(p) > lambda_min + && f.card.element(p) < lambda_max; - } - else - { - // Propagation - f.msk.element(p) = f.msk.element(f.parent.element(p)); + if (f.msk.element(p) && e_2(sq) == 0u) + { + for (unsigned l = 0; l < ptr.size(); ++l) + std::memset(ptr(l), i, ratio * sizeof(mln_value_(I))); + } - if (f.msk.element(p) && e_2(sq) == 0u) - { - for (unsigned l = 0; l < ptr.size(); ++l) - std::memset(ptr(l), i, ratio * sizeof(mln_value_(I))); } + else + { + // Propagation + f.msk.element(p) = f.msk.element(f.parent.element(p)); + + if (f.msk.element(p) && e_2(sq) == 0u) + { + for (unsigned l = 0; l < ptr.size(); ++l) + std::memset(ptr(l), i, ratio * sizeof(mln_value_(I))); + } + } } - } - for (unsigned l = 0; l < ptr.size(); ++l) - ptr(l) -= ratio; + for (unsigned l = 0; l < ptr.size(); ++l) + ptr(l) -= ratio; - --site[1]; - --p; - } + --site[1]; + --p; + } - } - } // end of 2nd pass + } + } // end of 2nd pass - return f.t_sub; - } + return f.t_sub; + } - template <typename I, typename J, typename K> - mln_ch_value(I, bool) - multi_scale_binarization(const I& in, const J& e2, - const util::array<K>& t_ima, - unsigned s) - { - mln_ch_value(I,bool) out; - initialize(out, in); + template <typename I, typename J, typename K> + mln_ch_value(I, bool) + multi_scale_binarization(const I& in, const J& e2, + const util::array<K>& t_ima, + unsigned s) + { + mln_ch_value(I,bool) out; + initialize(out, in); - typedef const mln_value(K)* ptr_type; + typedef const mln_value(K)* ptr_type; - ptr_type ptr_t[5]; - ptr_t[2] = & t_ima[2].at_(0, 0); - ptr_t[3] = & t_ima[3].at_(0, 0); - ptr_t[4] = & t_ima[4].at_(0, 0); + ptr_type ptr_t[5]; + ptr_t[2] = & t_ima[2].at_(0, 0); + ptr_t[3] = & t_ima[3].at_(0, 0); + ptr_t[4] = & t_ima[4].at_(0, 0); - const mln_value(J)* ptr_e2 = & e2.at_(0, 0); - const mln_value(I)* ptr__in = & in.at_(0, 0); - bool* ptr__out = & out.at_(0, 0); + const mln_value(J)* ptr_e2 = & e2.at_(0, 0); + const mln_value(I)* ptr__in = & in.at_(0, 0); + bool* ptr__out = & out.at_(0, 0); - // Since we iterate from a smaller image in the largest ones and - // image at scale 1 does not always have a size which can be - // divided by (4*s), some sites in the border may not be processed - // and we must skip them. - int more_offset = - ((4 * s) - in.ncols() % (4 * s)); + // Since we iterate from a smaller image in the largest ones and + // image at scale 1 does not always have a size which can be + // divided by (4*s), some sites in the border may not be processed + // and we must skip them. + int more_offset = - ((4 * s) - in.ncols() % (4 * s)); - if (more_offset == - (static_cast<int>(4*s))) - more_offset = 0; // No offset needed. + if (more_offset == - (static_cast<int>(4*s))) + more_offset = 0; // No offset needed. - const int - nrows4 = t_ima[4].nrows(), ncols4 = t_ima[4].ncols(), + const int + nrows4 = t_ima[4].nrows(), ncols4 = t_ima[4].ncols(), - delta1 = in.delta_index(dpoint2d(+1, -(s - 1))), - delta1b = in.delta_index(dpoint2d(+1, -(s + s - 1))), - delta1c = in.delta_index(dpoint2d(-(s + s - 1), +1)), - delta1d = in.delta_index(dpoint2d(+1, -(s * 4 - 1))), - delta1e = in.delta_index(dpoint2d(-(s * 4 - 1), +1)), - delta1f = in.delta_index(dpoint2d(-(s - 1), +1)), + delta1 = in.delta_index(dpoint2d(+1, -(s - 1))), + delta1b = in.delta_index(dpoint2d(+1, -(s + s - 1))), + delta1c = in.delta_index(dpoint2d(-(s + s - 1), +1)), + delta1d = in.delta_index(dpoint2d(+1, -(s * 4 - 1))), + delta1e = in.delta_index(dpoint2d(-(s * 4 - 1), +1)), + delta1f = in.delta_index(dpoint2d(-(s - 1), +1)), - delta2 = t_ima[2].delta_index(dpoint2d(+1, -1)), - delta2b = t_ima[2].delta_index(dpoint2d(+1, -3)), - delta2c = t_ima[2].delta_index(dpoint2d(-3, +1)), + delta2 = t_ima[2].delta_index(dpoint2d(+1, -1)), + delta2b = t_ima[2].delta_index(dpoint2d(+1, -3)), + delta2c = t_ima[2].delta_index(dpoint2d(-3, +1)), - delta3 = t_ima[3].delta_index(dpoint2d(+1, -1)), + delta3 = t_ima[3].delta_index(dpoint2d(+1, -1)), - eor1 = in.delta_index(dpoint2d(+4 * s, - in.ncols())) + more_offset, - eor2 = t_ima[2].delta_index(dpoint2d(+4,- t_ima[2].ncols())), - eor3 = t_ima[3].delta_index(dpoint2d(+2,- t_ima[3].ncols())), - eor4 = t_ima[4].delta_index(dpoint2d(+1,- t_ima[4].ncols())); + eor1 = in.delta_index(dpoint2d(+4 * s, - in.ncols())) + more_offset, + eor2 = t_ima[2].delta_index(dpoint2d(+4,- t_ima[2].ncols())), + eor3 = t_ima[3].delta_index(dpoint2d(+2,- t_ima[3].ncols())), + eor4 = t_ima[4].delta_index(dpoint2d(+1,- t_ima[4].ncols())); - mln_value(J) threshold; - for (int row4 = 0; row4 < nrows4; ++row4) - { - for (int col4 = 0; col4 < ncols4; ++col4) + mln_value(J) threshold; + for (int row4 = 0; row4 < nrows4; ++row4) { - // top left 1 + for (int col4 = 0; col4 < ncols4; ++col4) { - threshold = *ptr_t[*ptr_e2]; + // top left 1 { - for (unsigned i = 1; i < s; ++i) + threshold = *ptr_t[*ptr_e2]; { - for (unsigned j = 1; j < s; ++j) + for (unsigned i = 1; i < s; ++i) { + for (unsigned j = 1; j < s; ++j) + { + *ptr__out = *ptr__in < threshold; + ++ptr__out; ++ptr__in; + } + *ptr__out = *ptr__in < threshold; - ++ptr__out; ++ptr__in; + ptr__out += delta1; ptr__in += delta1; } - *ptr__out = *ptr__in < threshold; - ptr__out += delta1; ptr__in += delta1; - } - - for (unsigned j = 1; j < s; ++j) - { - *ptr__out = *ptr__in < threshold; - ++ptr__out; ++ptr__in; - } - *ptr__out = *ptr__in < threshold; - ptr__out += delta1f; ptr__in += delta1f; - } - - ++ptr_t[2]; ++ptr_e2; - threshold = *ptr_t[*ptr_e2]; - { - for (unsigned i = 1; i < s; ++i) - { for (unsigned j = 1; j < s; ++j) { *ptr__out = *ptr__in < threshold; ++ptr__out; ++ptr__in; } - *ptr__out = *ptr__in < threshold; - ptr__out += delta1; ptr__in += delta1; + ptr__out += delta1f; ptr__in += delta1f; } - for (unsigned j = 1; j < s; ++j) + ++ptr_t[2]; ++ptr_e2; + threshold = *ptr_t[*ptr_e2]; { - *ptr__out = *ptr__in < threshold; - ++ptr__out; ++ptr__in; - } - *ptr__out = *ptr__in < threshold; - ptr__out += delta1b; ptr__in += delta1b; - } + for (unsigned i = 1; i < s; ++i) + { + for (unsigned j = 1; j < s; ++j) + { + *ptr__out = *ptr__in < threshold; + ++ptr__out; ++ptr__in; + } + + *ptr__out = *ptr__in < threshold; + ptr__out += delta1; ptr__in += delta1; + } - ptr_t[2] += delta2; ptr_e2 += delta2; - threshold = *ptr_t[*ptr_e2]; - { - for (unsigned i = 1; i < s; ++i) - { for (unsigned j = 1; j < s; ++j) { *ptr__out = *ptr__in < threshold; ++ptr__out; ++ptr__in; } - *ptr__out = *ptr__in < threshold; - ptr__out += delta1; ptr__in += delta1; + ptr__out += delta1b; ptr__in += delta1b; } - for (unsigned j = 1; j < s; ++j) + ptr_t[2] += delta2; ptr_e2 += delta2; + threshold = *ptr_t[*ptr_e2]; { - *ptr__out = *ptr__in < threshold; - ++ptr__out; ++ptr__in; - } - *ptr__out = *ptr__in < threshold; - ptr__out += delta1f; ptr__in += delta1f; + for (unsigned i = 1; i < s; ++i) + { + for (unsigned j = 1; j < s; ++j) + { + *ptr__out = *ptr__in < threshold; + ++ptr__out; ++ptr__in; + } - } + *ptr__out = *ptr__in < threshold; + ptr__out += delta1; ptr__in += delta1; + } - ++ptr_t[2]; ++ptr_e2; - threshold = *ptr_t[*ptr_e2]; - { - for (unsigned i = 1; i < s; ++i) - { for (unsigned j = 1; j < s; ++j) { *ptr__out = *ptr__in < threshold; ++ptr__out; ++ptr__in; } - *ptr__out = *ptr__in < threshold; - ptr__out += delta1; ptr__in += delta1; + ptr__out += delta1f; ptr__in += delta1f; + } - for (unsigned j = 1; j < s; ++j) + ++ptr_t[2]; ++ptr_e2; + threshold = *ptr_t[*ptr_e2]; { - *ptr__out = *ptr__in < threshold; - ++ptr__out; ++ptr__in; - } - *ptr__out = *ptr__in < threshold; - ptr__out += delta1c; ptr__in += delta1c; - } + for (unsigned i = 1; i < s; ++i) + { + for (unsigned j = 1; j < s; ++j) + { + *ptr__out = *ptr__in < threshold; + ++ptr__out; ++ptr__in; + } - ptr_t[2] -= delta2; ptr_e2 -= delta2; - } + *ptr__out = *ptr__in < threshold; + ptr__out += delta1; ptr__in += delta1; + } - // top right 1 - ptr_t[3] += 1; - { - threshold = *ptr_t[*ptr_e2]; - { - for (unsigned i = 1; i < s; ++i) - { for (unsigned j = 1; j < s; ++j) { *ptr__out = *ptr__in < threshold; ++ptr__out; ++ptr__in; } - *ptr__out = *ptr__in < threshold; - ptr__out += delta1; ptr__in += delta1; + ptr__out += delta1c; ptr__in += delta1c; } - for (unsigned j = 1; j < s; ++j) - { - *ptr__out = *ptr__in < threshold; - ++ptr__out; ++ptr__in; - } - *ptr__out = *ptr__in < threshold; - ptr__out += delta1f; ptr__in += delta1f; + ptr_t[2] -= delta2; ptr_e2 -= delta2; } - ++ptr_t[2]; ++ptr_e2; - threshold = *ptr_t[*ptr_e2]; + // top right 1 + ptr_t[3] += 1; { - for (unsigned i = 1; i < s; ++i) + threshold = *ptr_t[*ptr_e2]; { + for (unsigned i = 1; i < s; ++i) + { + for (unsigned j = 1; j < s; ++j) + { + *ptr__out = *ptr__in < threshold; + ++ptr__out; ++ptr__in; + } + + *ptr__out = *ptr__in < threshold; + ptr__out += delta1; ptr__in += delta1; + } + for (unsigned j = 1; j < s; ++j) { *ptr__out = *ptr__in < threshold; ++ptr__out; ++ptr__in; } - *ptr__out = *ptr__in < threshold; - ptr__out += delta1; ptr__in += delta1; + ptr__out += delta1f; ptr__in += delta1f; } - for (unsigned j = 1; j < s; ++j) + ++ptr_t[2]; ++ptr_e2; + threshold = *ptr_t[*ptr_e2]; { - *ptr__out = *ptr__in < threshold; - ++ptr__out; ++ptr__in; - } - *ptr__out = *ptr__in < threshold; - ptr__out += delta1b; ptr__in += delta1b; - } + for (unsigned i = 1; i < s; ++i) + { + for (unsigned j = 1; j < s; ++j) + { + *ptr__out = *ptr__in < threshold; + ++ptr__out; ++ptr__in; + } + + *ptr__out = *ptr__in < threshold; + ptr__out += delta1; ptr__in += delta1; + } - ptr_t[2] += delta2; ptr_e2 += delta2; - threshold = *ptr_t[*ptr_e2]; - { - for (unsigned i = 1; i < s; ++i) - { for (unsigned j = 1; j < s; ++j) { *ptr__out = *ptr__in < threshold; ++ptr__out; ++ptr__in; } - *ptr__out = *ptr__in < threshold; - ptr__out += delta1; ptr__in += delta1; + ptr__out += delta1b; ptr__in += delta1b; } - for (unsigned j = 1; j < s; ++j) + ptr_t[2] += delta2; ptr_e2 += delta2; + threshold = *ptr_t[*ptr_e2]; { - *ptr__out = *ptr__in < threshold; - ++ptr__out; ++ptr__in; - } - *ptr__out = *ptr__in < threshold; - ptr__out += delta1f; ptr__in += delta1f; - } + for (unsigned i = 1; i < s; ++i) + { + for (unsigned j = 1; j < s; ++j) + { + *ptr__out = *ptr__in < threshold; + ++ptr__out; ++ptr__in; + } + + *ptr__out = *ptr__in < threshold; + ptr__out += delta1; ptr__in += delta1; + } - ++ptr_t[2]; ++ptr_e2; - threshold = *ptr_t[*ptr_e2]; - { - for (unsigned i = 1; i < s; ++i) - { for (unsigned j = 1; j < s; ++j) { *ptr__out = *ptr__in < threshold; ++ptr__out; ++ptr__in; } - *ptr__out = *ptr__in < threshold; - ptr__out += delta1; ptr__in += delta1; + ptr__out += delta1f; ptr__in += delta1f; } - for (unsigned j = 1; j < s; ++j) + ++ptr_t[2]; ++ptr_e2; + threshold = *ptr_t[*ptr_e2]; { - *ptr__out = *ptr__in < threshold; - ++ptr__out; ++ptr__in; - } - *ptr__out = *ptr__in < threshold; - ptr__out += delta1d; ptr__in += delta1d; - } + for (unsigned i = 1; i < s; ++i) + { + for (unsigned j = 1; j < s; ++j) + { + *ptr__out = *ptr__in < threshold; + ++ptr__out; ++ptr__in; + } - ptr_t[2] += delta2b; ptr_e2 += delta2b; - } + *ptr__out = *ptr__in < threshold; + ptr__out += delta1; ptr__in += delta1; + } - // bot left 1 - ptr_t[3] += delta3; - { - threshold = *ptr_t[*ptr_e2]; - { - for (unsigned i = 1; i < s; ++i) - { for (unsigned j = 1; j < s; ++j) { *ptr__out = *ptr__in < threshold; ++ptr__out; ++ptr__in; } - *ptr__out = *ptr__in < threshold; - ptr__out += delta1; ptr__in += delta1; + ptr__out += delta1d; ptr__in += delta1d; } - for (unsigned j = 1; j < s; ++j) - { - *ptr__out = *ptr__in < threshold; - ++ptr__out; ++ptr__in; - } - *ptr__out = *ptr__in < threshold; - ptr__out += delta1f; ptr__in += delta1f; + ptr_t[2] += delta2b; ptr_e2 += delta2b; } - ++ptr_t[2]; ++ptr_e2; - threshold = *ptr_t[*ptr_e2]; + // bot left 1 + ptr_t[3] += delta3; { - for (unsigned i = 1; i < s; ++i) + threshold = *ptr_t[*ptr_e2]; { + for (unsigned i = 1; i < s; ++i) + { + for (unsigned j = 1; j < s; ++j) + { + *ptr__out = *ptr__in < threshold; + ++ptr__out; ++ptr__in; + } + + *ptr__out = *ptr__in < threshold; + ptr__out += delta1; ptr__in += delta1; + } + for (unsigned j = 1; j < s; ++j) { *ptr__out = *ptr__in < threshold; ++ptr__out; ++ptr__in; } - *ptr__out = *ptr__in < threshold; - ptr__out += delta1; ptr__in += delta1; + ptr__out += delta1f; ptr__in += delta1f; } - for (unsigned j = 1; j < s; ++j) + ++ptr_t[2]; ++ptr_e2; + threshold = *ptr_t[*ptr_e2]; { - *ptr__out = *ptr__in < threshold; - ++ptr__out; ++ptr__in; - } - *ptr__out = *ptr__in < threshold; - ptr__out += delta1b; ptr__in += delta1b; - } + for (unsigned i = 1; i < s; ++i) + { + for (unsigned j = 1; j < s; ++j) + { + *ptr__out = *ptr__in < threshold; + ++ptr__out; ++ptr__in; + } + + *ptr__out = *ptr__in < threshold; + ptr__out += delta1; ptr__in += delta1; + } - ptr_t[2] += delta2; ptr_e2 += delta2; - threshold = *ptr_t[*ptr_e2]; - { - for (unsigned i = 1; i < s; ++i) - { for (unsigned j = 1; j < s; ++j) { *ptr__out = *ptr__in < threshold; ++ptr__out; ++ptr__in; } - *ptr__out = *ptr__in < threshold; - ptr__out += delta1; ptr__in += delta1; + ptr__out += delta1b; ptr__in += delta1b; } - for (unsigned j = 1; j < s; ++j) + ptr_t[2] += delta2; ptr_e2 += delta2; + threshold = *ptr_t[*ptr_e2]; { - *ptr__out = *ptr__in < threshold; - ++ptr__out; ++ptr__in; - } - *ptr__out = *ptr__in < threshold; - ptr__out += delta1f; ptr__in += delta1f; - } + for (unsigned i = 1; i < s; ++i) + { + for (unsigned j = 1; j < s; ++j) + { + *ptr__out = *ptr__in < threshold; + ++ptr__out; ++ptr__in; + } + + *ptr__out = *ptr__in < threshold; + ptr__out += delta1; ptr__in += delta1; + } - ++ptr_t[2]; ++ptr_e2; - threshold = *ptr_t[*ptr_e2]; - { - for (unsigned i = 1; i < s; ++i) - { for (unsigned j = 1; j < s; ++j) { *ptr__out = *ptr__in < threshold; ++ptr__out; ++ptr__in; } - *ptr__out = *ptr__in < threshold; - ptr__out += delta1; ptr__in += delta1; + ptr__out += delta1f; ptr__in += delta1f; } - for (unsigned j = 1; j < s; ++j) + ++ptr_t[2]; ++ptr_e2; + threshold = *ptr_t[*ptr_e2]; { - *ptr__out = *ptr__in < threshold; - ++ptr__out; ++ptr__in; - } - *ptr__out = *ptr__in < threshold; - ptr__out += delta1c; ptr__in += delta1c; - } + for (unsigned i = 1; i < s; ++i) + { + for (unsigned j = 1; j < s; ++j) + { + *ptr__out = *ptr__in < threshold; + ++ptr__out; ++ptr__in; + } - ptr_t[2] -= delta2; ptr_e2 -= delta2; - } + *ptr__out = *ptr__in < threshold; + ptr__out += delta1; ptr__in += delta1; + } - // bot right 1 - ptr_t[3] += 1; - { - threshold = *ptr_t[*ptr_e2]; - { - for (unsigned i = 1; i < s; ++i) - { for (unsigned j = 1; j < s; ++j) { *ptr__out = *ptr__in < threshold; ++ptr__out; ++ptr__in; } - *ptr__out = *ptr__in < threshold; - ptr__out += delta1; ptr__in += delta1; + ptr__out += delta1c; ptr__in += delta1c; } - for (unsigned j = 1; j < s; ++j) - { - *ptr__out = *ptr__in < threshold; - ++ptr__out; ++ptr__in; - } - *ptr__out = *ptr__in < threshold; - ptr__out += delta1f; ptr__in += delta1f; + ptr_t[2] -= delta2; ptr_e2 -= delta2; } - ++ptr_t[2]; ++ptr_e2; - threshold = *ptr_t[*ptr_e2]; + // bot right 1 + ptr_t[3] += 1; { - for (unsigned i = 1; i < s; ++i) + threshold = *ptr_t[*ptr_e2]; { + for (unsigned i = 1; i < s; ++i) + { + for (unsigned j = 1; j < s; ++j) + { + *ptr__out = *ptr__in < threshold; + ++ptr__out; ++ptr__in; + } + + *ptr__out = *ptr__in < threshold; + ptr__out += delta1; ptr__in += delta1; + } + for (unsigned j = 1; j < s; ++j) { *ptr__out = *ptr__in < threshold; ++ptr__out; ++ptr__in; } - *ptr__out = *ptr__in < threshold; - ptr__out += delta1; ptr__in += delta1; + ptr__out += delta1f; ptr__in += delta1f; } - for (unsigned j = 1; j < s; ++j) + ++ptr_t[2]; ++ptr_e2; + threshold = *ptr_t[*ptr_e2]; { - *ptr__out = *ptr__in < threshold; - ++ptr__out; ++ptr__in; - } - *ptr__out = *ptr__in < threshold; - ptr__out += delta1b; ptr__in += delta1b; - } + for (unsigned i = 1; i < s; ++i) + { + for (unsigned j = 1; j < s; ++j) + { + *ptr__out = *ptr__in < threshold; + ++ptr__out; ++ptr__in; + } + + *ptr__out = *ptr__in < threshold; + ptr__out += delta1; ptr__in += delta1; + } - ptr_t[2] += delta2; ptr_e2 += delta2; - threshold = *ptr_t[*ptr_e2]; - { - for (unsigned i = 1; i < s; ++i) - { for (unsigned j = 1; j < s; ++j) { *ptr__out = *ptr__in < threshold; ++ptr__out; ++ptr__in; } - *ptr__out = *ptr__in < threshold; - ptr__out += delta1; ptr__in += delta1; + ptr__out += delta1b; ptr__in += delta1b; } - for (unsigned j = 1; j < s; ++j) + ptr_t[2] += delta2; ptr_e2 += delta2; + threshold = *ptr_t[*ptr_e2]; { - *ptr__out = *ptr__in < threshold; - ++ptr__out; ++ptr__in; - } - *ptr__out = *ptr__in < threshold; - ptr__out += delta1f; ptr__in += delta1f; - } + for (unsigned i = 1; i < s; ++i) + { + for (unsigned j = 1; j < s; ++j) + { + *ptr__out = *ptr__in < threshold; + ++ptr__out; ++ptr__in; + } + + *ptr__out = *ptr__in < threshold; + ptr__out += delta1; ptr__in += delta1; + } - ++ptr_t[2]; ++ptr_e2; - threshold = *ptr_t[*ptr_e2]; - { - for (unsigned i = 1; i < s; ++i) - { for (unsigned j = 1; j < s; ++j) { *ptr__out = *ptr__in < threshold; ++ptr__out; ++ptr__in; } - *ptr__out = *ptr__in < threshold; - ptr__out += delta1; ptr__in += delta1; + ptr__out += delta1f; ptr__in += delta1f; } - for (unsigned j = 1; j < s; ++j) + ++ptr_t[2]; ++ptr_e2; + threshold = *ptr_t[*ptr_e2]; { + for (unsigned i = 1; i < s; ++i) + { + for (unsigned j = 1; j < s; ++j) + { + *ptr__out = *ptr__in < threshold; + ++ptr__out; ++ptr__in; + } + + *ptr__out = *ptr__in < threshold; + ptr__out += delta1; ptr__in += delta1; + } + + for (unsigned j = 1; j < s; ++j) + { + *ptr__out = *ptr__in < threshold; + ++ptr__out; ++ptr__in; + } *ptr__out = *ptr__in < threshold; - ++ptr__out; ++ptr__in; + ptr__out += delta1e; ptr__in += delta1e; } - *ptr__out = *ptr__in < threshold; - ptr__out += delta1e; ptr__in += delta1e; } + + // bot right -> next top left + ptr_t[2] += delta2c; ptr_e2 += delta2c; + ptr_t[3] = ptr_t[3] - delta3; + ptr_t[4] += 1; } - // bot right -> next top left - ptr_t[2] += delta2c; ptr_e2 += delta2c; - ptr_t[3] = ptr_t[3] - delta3; - ptr_t[4] += 1; + // eof -> next bof + ptr__out += eor1; ptr__in += eor1; + ptr_t[2] += eor2; ptr_e2 += eor2; + ptr_t[3] += eor3; + ptr_t[4] += eor4; } - // eof -> next bof - ptr__out += eor1; ptr__in += eor1; - ptr_t[2] += eor2; ptr_e2 += eor2; - ptr_t[3] += eor3; - ptr_t[4] += eor4; + return out; } - return out; - } + unsigned sub(unsigned nbr, unsigned down_scaling) + { + return (nbr + down_scaling - 1) / down_scaling; + } - unsigned sub(unsigned nbr, unsigned down_scaling) - { - return (nbr + down_scaling - 1) / down_scaling; - } + // Compute domains of subsampled images and make sure they can be + // divided by 2. + template <typename I> + util::array<util::couple<mln_domain(I), unsigned> > + compute_sub_domains(const I& ima, unsigned n_scales, unsigned s) + { + util::array<util::couple<unsigned, unsigned> > n(n_scales + 2); + + n(1) = make::couple(ima.nrows(), ima.ncols()); + n(2) = make::couple(sub(n(1).first(), s), + sub(n(1).second(), s)); + for (unsigned i = 3; i <= n_scales + 1; ++i) + n(i) = make::couple(sub(n(i - 1).first(), 2), + sub(n(i - 1).second(), 2)); - // Compute domains of subsampled images and make sure they can be - // divided by 2. - template <typename I> - util::array<util::couple<mln_domain(I), unsigned> > - compute_sub_domains(const I& ima, unsigned n_scales, unsigned s) - { - util::array<util::couple<unsigned, unsigned> > n(n_scales + 2); - n(1) = make::couple(ima.nrows(), ima.ncols()); - n(2) = make::couple(sub(n(1).first(), s), - sub(n(1).second(), s)); - for (unsigned i = 3; i <= n_scales + 1; ++i) - n(i) = make::couple(sub(n(i - 1).first(), 2), - sub(n(i - 1).second(), 2)); + util::array<util::couple<mln_domain(I), unsigned> > out(n.size()); + out(0) = make::couple(make::box2d(1,1), 1u); + out(1) = make::couple(make::box2d(ima.nrows(), ima.ncols()), 2u); + out(n_scales + 1) = make::couple(make::box2d(n(n_scales + 1).first(), + n(n_scales + 1).second()), + 1u); + for (unsigned i = n_scales; i > 1; --i) + out(i) = make::couple(make::box2d(2 * out(i + 1).first().nrows(), + 2 * out(i + 1).first().ncols()), + 2 * out(i + 1).second()); - util::array<util::couple<mln_domain(I), unsigned> > out(n.size()); - out(0) = make::couple(make::box2d(1,1), 1u); - out(1) = make::couple(make::box2d(ima.nrows(), ima.ncols()), 2u); - out(n_scales + 1) = make::couple(make::box2d(n(n_scales + 1).first(), - n(n_scales + 1).second()), - 1u); + out(1).second() = std::max(out(2).first().ncols() * s - ima.ncols(), + out(2).first().nrows() * s - ima.nrows()); - for (unsigned i = n_scales; i > 1; --i) - out(i) = make::couple(make::box2d(2 * out(i + 1).first().nrows(), - 2 * out(i + 1).first().ncols()), - 2 * out(i + 1).second()); + return out; + } + + } // end of namespace scribo::binarization::internal - out(1).second() = std::max(out(2).first().ncols() * s - ima.ncols(), - out(2).first().nrows() * s - ima.nrows()); - return out; - } template <typename I> mln_ch_value(I,bool) @@ -743,7 +751,7 @@ namespace scribo } util::array<util::couple<box2d, unsigned> > - sub_domains = compute_sub_domains(input_1, nb_subscale, s); + sub_domains = internal::compute_sub_domains(input_1, nb_subscale, s); border::adjust(input_1, sub_domains(1).second()); border::mirror(input_1); @@ -776,11 +784,12 @@ namespace scribo { int i = sub_ima.size() - 1; unsigned ratio = std::pow(q, i - 2); // Ratio compared to e_2 - t_ima[i] = compute_t_n_and_e_2(sub_ima[i], e_2, - lambda_min_2 / ratio, - mln_max(unsigned), - s, - q, i, w_work, integral_sum_sum_2); + t_ima[i] = internal::compute_t_n_and_e_2(sub_ima[i], e_2, + lambda_min_2 / ratio, + mln_max(unsigned), + s, + q, i, w_work, + integral_sum_sum_2); } // Other scales -> maximum and minimum component size. @@ -788,18 +797,21 @@ namespace scribo for (int i = sub_ima.size() - 2; i > 2; --i) { unsigned ratio = std::pow(q, i - 2); // Ratio compared to e_2 - t_ima[i] = compute_t_n_and_e_2(sub_ima[i], e_2, - lambda_min_2 / ratio, - lambda_max_2 / ratio, - s, - q, i, w_work, integral_sum_sum_2); + t_ima[i] = internal::compute_t_n_and_e_2(sub_ima[i], e_2, + lambda_min_2 / ratio, + lambda_max_2 / ratio, + s, + q, i, w_work, + integral_sum_sum_2); } } // Lowest scale -> no minimum component size. { - t_ima[2] = compute_t_n_and_e_2(sub_ima[2], e_2, 0, lambda_max_2, - s, 1, 2, w_work, integral_sum_sum_2); + t_ima[2] = internal::compute_t_n_and_e_2(sub_ima[2], e_2, 0, + lambda_max_2, + s, 1, 2, w_work, + integral_sum_sum_2); } @@ -808,7 +820,8 @@ namespace scribo // Binarize - image2d<bool> output = multi_scale_binarization(input_1, e_2, t_ima, s); + image2d<bool> + output = internal::multi_scale_binarization(input_1, e_2, t_ima, s); trace::exiting("scribo::binarization::sauvola_ms"); return output; diff --git a/scribo/binarization/sauvola_threshold.hh b/scribo/binarization/sauvola_threshold.hh index 20abc50..01bd1b0 100644 --- a/scribo/binarization/sauvola_threshold.hh +++ b/scribo/binarization/sauvola_threshold.hh @@ -48,9 +48,6 @@ # include <scribo/core/init_integral_image.hh> - -#include <mln/io/pgm/save.hh> - namespace scribo { @@ -476,8 +473,6 @@ namespace scribo exact(simple), exact(squared)); -// std::cout << std::endl << " ------- " << std::endl; - io::pgm::save(output, "ref_2_t.pgm"); trace::exiting("scribo::text::ppm2pbm"); return output; } diff --git a/scribo/canvas/integral_browsing.hh b/scribo/canvas/integral_browsing.hh index 44d73a0..45ff38d 100644 --- a/scribo/canvas/integral_browsing.hh +++ b/scribo/canvas/integral_browsing.hh @@ -63,7 +63,11 @@ namespace scribo // std::cout << "(" << mean << " - " << stddev << " - " << n << "),"; // unbias version: - stddev = std::sqrt((sum_2 - sum * sum / n) / (n - 1)); + double num = (sum_2 - sum * sum / n); + if (num > 0) + stddev = std::sqrt(num / (n - 1)); + else + stddev = 0; } @@ -83,18 +87,21 @@ namespace scribo typedef const V* Ptr; Ptr a_ima, b_ima, c_ima, d_ima; - const unsigned +// mln_precondition((h/2) < ima.nrows()); +// mln_precondition((w/2) < ima.ncols()); + + const int nrows = ima.nrows(), ncols = ima.ncols(), row_0 = step / 2, col_0 = step / 2; - const unsigned + const int offset_down = ima.delta_index(dpoint2d(step, 0)), offset_ante = ima.delta_index(dpoint2d(0, -w)), offset_below = ima.delta_index(dpoint2d(+h, 0)); - const unsigned + const int max_row_top = h/2, max_row_mid = nrows - 1 - h/2, max_col_left = w/2, @@ -105,10 +112,11 @@ namespace scribo h_top = row_0 + h/2 + 1, w_left = col_0 + w/2 + 1; - unsigned row, col; + + int row, col; for (col = col_0; col <= max_col_mid; col += step) ; - unsigned w_right = ncols - col + w/2; + int w_right = ncols - col + w/2; Ptr d_tl_start, d_tr_start, @@ -119,6 +127,18 @@ namespace scribo unsigned s_2 = s * s; + // Make sure the window fits in the image domain. + if (w >= static_cast<const unsigned>(ncols)) + { + w = ncols - 1; + trace::warning("integral_browsing - Adjusting window width since it was larger than image width."); + } + if (h >= static_cast<const unsigned>(nrows)) + { + h = nrows - 1; + trace::warning("integral_browsing - Adjusting window height since it was larger than image height."); + } + // ------------------------------- // T (top) @@ -128,7 +148,7 @@ namespace scribo delta_start_left = step * w_left, delta_start_right = step * w_right, step_w = step * w; - unsigned + int size_tl_start = h_top * w_left, size_tl, delta_size_tl = h_top * step, @@ -209,7 +229,6 @@ namespace scribo } - // ------------------------------- // (M) middle // ------------------------------- @@ -266,25 +285,6 @@ namespace scribo { // D + A - B - C -// if (row == 3 && col == 3) -// std::cout << "p(" << row << "," << col << ") - " - -// << "A" << ima.point_at_index(a_ima - ima.buffer()) -// << "=" << a_ima->first() << " - " - -// << "B" << ima.point_at_index(b_ima - ima.buffer()) -// << "=" << b_ima->first() << " - " - -// << "C" << ima.point_at_index(c_ima - ima.buffer()) -// << "=" << c_ima->first() << " - " - -// << "D" << ima.point_at_index(d_ima - ima.buffer()) -// << "=" << d_ima->first() << " - " - -// << "n =" << size_mc << " - " -// << "n*s_2 =" << size_mc * s_2 -// << std::endl; - internal::compute_stats((d_ima->first() - b_ima->first()) + (a_ima->first() - c_ima->first()), (d_ima->second() - b_ima->second()) + (a_ima->second() - c_ima->second()), size_mc * s_2, @@ -292,13 +292,6 @@ namespace scribo functor.exec(mean, stddev); -// std::cout << " - " << mean -// << " - " << stddev -// << " - " << (d_ima->first() - b_ima->first()) + (a_ima->first() - c_ima->first()) -// << " - " << (d_ima->second() - b_ima->second()) + (a_ima->second() - c_ima->second()) -// << std::endl; - - a_ima += step; b_ima += step; c_ima += step; @@ -332,7 +325,6 @@ namespace scribo } - // ------------------------------- // B (bottom) // ------------------------------- @@ -342,6 +334,7 @@ namespace scribo size_bl, delta_size_bl = (nrows - row + h/2) * step, size_bc = (nrows - row + h/2) * w, + size_br_start = (nrows - row + h/2) * w_right, delta_size_br = (nrows - row + h/2) * step, size_br; @@ -392,12 +385,6 @@ namespace scribo (d_ima->second() - b_ima->second()) + (a_ima->second() - c_ima->second()), size_bc * s_2, mean, stddev); -// std::cout << (d_ima->second() - b_ima->second()) + (a_ima->second() - c_ima->second()) << std::endl; - -// std::cout << d_ima->second() << " - " << b_ima->second() << " - " -// << a_ima->second() << " - " << c_ima->second() << std::endl; -// std::cout << d_ima->first() << " - " << b_ima->first() << " - " -// << a_ima->first() << " - " << c_ima->first() << std::endl; functor.exec(mean, stddev); a_ima += step; b_ima += step; diff --git a/scribo/src/binarization/sauvola_ms.cc b/scribo/src/binarization/sauvola_ms.cc index b3b4791..61887af 100644 --- a/scribo/src/binarization/sauvola_ms.cc +++ b/scribo/src/binarization/sauvola_ms.cc @@ -31,7 +31,6 @@ #include <scribo/binarization/sauvola_ms.hh> #include <scribo/debug/usage.hh> - bool check_args(int argc, char * argv[]) { if (argc < 5 || argc > 6) @@ -88,21 +87,20 @@ int main(int argc, char *argv[]) image2d<value::int_u8> input_1; io::pgm::load(input_1, argv[1]); - { - unsigned max_dim = math::max(input_1.ncols(), - input_1.nrows()); - if (w_1 > max_dim) - { - std::cout << "------------------" << std::endl; - std::cout << "The window is too large! Image size is only " - << input_1.nrows() << "x" << input_1.ncols() - << std::endl - << "Window size must not exceed " << max_dim - << std::endl; - return 1; - } - } - +// { +// unsigned max_dim = math::min(input_1.ncols() / s, +// input_1.nrows() / s); +// if ((w_1 / s * 4) > max_dim) +// { +// std::cout << "------------------" << std::endl; +// std::cout << "The window is too large! Image size is only " +// << input_1.nrows() << "x" << input_1.ncols() +// << std::endl +// << "Window size must not exceed " << max_dim * s / 4 +// << std::endl; +// return 1; +// } +// } image2d<bool> output = scribo::binarization::sauvola_ms(input_1, w_1, s, lambda_min_1); diff --git a/scribo/src/binarization/sauvola_pgm.cc b/scribo/src/binarization/sauvola_pgm.cc index f05ed5f..380d369 100644 --- a/scribo/src/binarization/sauvola_pgm.cc +++ b/scribo/src/binarization/sauvola_pgm.cc @@ -29,6 +29,7 @@ #include <scribo/binarization/sauvola.hh> #include <scribo/debug/usage.hh> +#include <mln/util/timer.hh> const char *args_desc[][2] = { @@ -56,7 +57,14 @@ int main(int argc, char *argv[]) image2d<int_u8> input; io::pgm::load(input, argv[1]); - io::pbm::save(scribo::binarization::sauvola(input, w), argv[3]); + util::timer t; + t.start(); + image2d<bool> out = scribo::binarization::sauvola(input, w); + t.stop(); + std::cout << t << std::endl; + + + io::pbm::save(out, argv[3]); trace::exiting("main"); diff --git a/scribo/subsampling/integral_single_image.hh b/scribo/subsampling/integral_single_image.hh index 6ed1cc6..cd25fc1 100644 --- a/scribo/subsampling/integral_single_image.hh +++ b/scribo/subsampling/integral_single_image.hh @@ -123,7 +123,7 @@ namespace scribo integral_sum_sum_2.init_(output_domain, border_thickness); V2* p_integ = integral_sum_sum_2.buffer(); - const unsigned up = sub.delta_index(dpoint2d(-1, 0)); + const int up = sub.delta_index(dpoint2d(-1, 0)); const unsigned nrows = 3 * output_domain.nrows(); const unsigned ncols = 3 * output_domain.ncols(); @@ -242,7 +242,7 @@ namespace scribo integral_sum_sum_2.init_(output_domain, border_thickness); V2* p_integ = integral_sum_sum_2.buffer(); - const unsigned up = sub.delta_index(dpoint2d(-1, 0)); + const int up = sub.delta_index(dpoint2d(-1, 0)); const unsigned nrows = 2 * output_domain.nrows(); const unsigned ncols = 2 * output_domain.ncols(); -- 1.5.6.5
participants (1)
-
Guillaume Lazzara