olena-2.0-72-ga07c8c6 Rename skewness directory and share code between implementations.

* z/skewness/skewness.cc, * z/skewness/skewness2.cc: Move... * z/sauvola_ms_rv/skewness.cc, * z/sauvola_ms_rv/skewness/skewness2.cc: ... Here. * z/sauvola_ms_rv/skewness/integral_browsing_rv.hh: Share code between the two implementations. --- scribo/sandbox/ChangeLog | 13 + .../z/{skewness => sauvola_ms_rv}/skewness.cc | 0 .../skewness/integral_browsing_rv.hh} | 135 ++++++------ .../sandbox/z/sauvola_ms_rv/skewness/skewness2.cc | 230 ++++++++++++++++++++ scribo/sandbox/z/skewness/skewness2.cc | 154 ------------- 5 files changed, 314 insertions(+), 218 deletions(-) rename scribo/sandbox/z/{skewness => sauvola_ms_rv}/skewness.cc (100%) copy scribo/{scribo/canvas/integral_browsing.hh => sandbox/z/sauvola_ms_rv/skewness/integral_browsing_rv.hh} (72%) create mode 100644 scribo/sandbox/z/sauvola_ms_rv/skewness/skewness2.cc delete mode 100644 scribo/sandbox/z/skewness/skewness2.cc diff --git a/scribo/sandbox/ChangeLog b/scribo/sandbox/ChangeLog index c87381c..8cfe592 100644 --- a/scribo/sandbox/ChangeLog +++ b/scribo/sandbox/ChangeLog @@ -1,5 +1,18 @@ 2012-08-23 Guillaume Lazzara <z@lrde.epita.fr> + Rename skewness directory and share code between implementations. + + * z/skewness/skewness.cc, + * z/skewness/skewness2.cc: Move... + + * z/sauvola_ms_rv/skewness.cc, + * z/sauvola_ms_rv/skewness/skewness2.cc: ... Here. + + * z/sauvola_ms_rv/skewness/integral_browsing_rv.hh: Share code + between the two implementations. + +2012-08-23 Guillaume Lazzara <z@lrde.epita.fr> + * z/skewness/skewness2.cc: New. 2012-08-23 Guillaume Lazzara <z@lrde.epita.fr> diff --git a/scribo/sandbox/z/skewness/skewness.cc b/scribo/sandbox/z/sauvola_ms_rv/skewness.cc similarity index 100% rename from scribo/sandbox/z/skewness/skewness.cc rename to scribo/sandbox/z/sauvola_ms_rv/skewness.cc diff --git a/scribo/scribo/canvas/integral_browsing.hh b/scribo/sandbox/z/sauvola_ms_rv/skewness/integral_browsing_rv.hh similarity index 72% copy from scribo/scribo/canvas/integral_browsing.hh copy to scribo/sandbox/z/sauvola_ms_rv/skewness/integral_browsing_rv.hh index 0ec3d83..79d5e34 100644 --- a/scribo/scribo/canvas/integral_browsing.hh +++ b/scribo/sandbox/z/sauvola_ms_rv/skewness/integral_browsing_rv.hh @@ -25,8 +25,8 @@ // executable file might be covered by the GNU General Public License. -#ifndef SCRIBO_CANVAS_INTEGRAL_BROWSING_HH -# define SCRIBO_CANVAS_INTEGRAL_BROWSING_HH +#ifndef SCRIBO_CANVAS_INTEGRAL_BROWSING_RV_HH +# define SCRIBO_CANVAS_INTEGRAL_BROWSING_RV_HH # include <mln/core/image/image2d.hh> # include <mln/util/couple.hh> @@ -40,10 +40,10 @@ namespace scribo template <typename F> - void integral_browsing(const image2d<mln::util::couple<double, double> >& ima, - unsigned step, - unsigned w, unsigned h, - F& functor); + void integral_browsing_rv(const image2d<mln::util::couple<double, mln::util::couple<double, double> > >& ima, + unsigned step, + unsigned w, unsigned h, + F& functor); # ifndef MLN_INCLUDE_ONLY @@ -54,19 +54,15 @@ namespace scribo inline void compute_stats(// in - double sum, double sum_2, unsigned n, + double sum, double sum_2, double sum_3, unsigned n, // out - double& mean, double& stddev) + double& skewness) { - mean = sum / n; - - // unbias version: - double num = (sum_2 - sum * sum / n); - if (num > 0) - stddev = std::sqrt(num / (n - 1)); - else - stddev = 0; + // Mean + double mean = sum / n; + // Skewness + skewness = (sum_3 - 3. * mean * sum_2) / (double) n + 2. * std::pow(mean, 3); } } // end of namespace scribo::canvas::internal @@ -75,13 +71,13 @@ namespace scribo template <typename F> - void integral_browsing(const image2d<mln::util::couple<double, double> >& ima, - unsigned step, - unsigned w, unsigned h, - unsigned s, - F& functor) + void integral_browsing_rv(const image2d<mln::util::couple<double, mln::util::couple<double, double> > >& ima, + unsigned w, unsigned h, + F& functor) { - typedef mln::util::couple<double, double> V; + unsigned step = 1; + + typedef mln::util::couple<double, mln::util::couple<double, double> > V; typedef const V* Ptr; Ptr a_ima, b_ima, c_ima, d_ima; @@ -121,9 +117,8 @@ namespace scribo b_ml_start = 0, d_ml_start = 0, b_mr_start = 0, d_mr_start = 0, b_bl_start = 0, d_bl_start = 0, b_br_start = 0, d_br = 0; - double mean, stddev; + double skewness; - unsigned s_2 = s * s; // ------------------------------- // T (top) @@ -157,10 +152,11 @@ namespace scribo { // D internal::compute_stats(d_ima->first(), - d_ima->second(), - size_tl * s_2, - mean, stddev); - functor.exec(mean, stddev); + d_ima->second().first(), + d_ima->second().second(), + size_tl, + skewness); + functor.exec(skewness); d_ima += step; size_tl += delta_size_tl; } @@ -177,10 +173,11 @@ namespace scribo { // D - C internal::compute_stats(d_ima->first() - c_ima->first(), - d_ima->second() - c_ima->second(), - size_tc * s_2, - mean, stddev); - functor.exec(mean, stddev); + d_ima->second().first() - c_ima->second().first(), + d_ima->second().second() - c_ima->second().second(), + size_tc, + skewness); + functor.exec(skewness); c_ima += step; d_ima += step; } @@ -192,17 +189,19 @@ namespace scribo d_ima = d_tr_start; double d_sum = d_ima->first(), - d_sum_2 = d_ima->second(); + d_sum_2 = d_ima->second().first(), + d_sum_3 = d_ima->second().second(); size_tr = size_tr_start; for (; col < ncols; col += step) { // D* - C internal::compute_stats(d_sum - c_ima->first(), - d_sum_2 - c_ima->second(), - size_tr * s_2, - mean, stddev); - functor.exec(mean, stddev); + d_sum_2 - c_ima->second().first(), + d_sum_3 - c_ima->second().second(), + size_tr, + skewness); + functor.exec(skewness); c_ima += step; size_tr -= delta_size_tr; } @@ -249,10 +248,11 @@ namespace scribo { // D - B internal::compute_stats(d_ima->first() - b_ima->first(), - d_ima->second() - b_ima->second(), - size_ml * s_2, - mean, stddev); - functor.exec(mean, stddev); + d_ima->second().first() - b_ima->second().first(), + d_ima->second().second() - b_ima->second().second(), + size_ml, + skewness); + functor.exec(skewness); b_ima += step; d_ima += step; size_ml += h_step; @@ -271,11 +271,12 @@ namespace scribo // D + A - B - C 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, - mean, stddev); + (d_ima->second().first() - b_ima->second().first()) + (a_ima->second().first() - c_ima->second().first()), + (d_ima->second().second() - b_ima->second().second()) + (a_ima->second().second() - c_ima->second().second()), + size_mc, + skewness); - functor.exec(mean, stddev); + functor.exec(skewness); a_ima += step; b_ima += step; @@ -290,16 +291,18 @@ namespace scribo d_ima = d_mr_start; double d_b_sum = d_ima->first() - b_ima->first(), - d_b_sum_2 = d_ima->second() - b_ima->second(); + d_b_sum_2 = d_ima->second().first() - b_ima->second().first(), + d_b_sum_3 = d_ima->second().second() - b_ima->second().second(); for (; col < ncols; col += step) { // D* + A - B* - C internal::compute_stats(d_b_sum + (a_ima->first() - c_ima->first()), - d_b_sum_2 + (a_ima->second() - c_ima->second()), - size_mr * s_2, - mean, stddev); - functor.exec(mean, stddev); + d_b_sum_2 + (a_ima->second().first() - c_ima->second().first()), + d_b_sum_3 + (a_ima->second().second() - c_ima->second().second()), + size_mr, + skewness); + functor.exec(skewness); a_ima += step; c_ima += step; size_mr -= h_step; @@ -345,10 +348,11 @@ namespace scribo { // D* - B internal::compute_stats(d_ima->first() - b_ima->first(), - d_ima->second() - b_ima->second(), - size_bl * s_2, - mean, stddev); - functor.exec(mean, stddev); + d_ima->second().first() - b_ima->second().first(), + d_ima->second().second() - b_ima->second().second(), + size_bl, + skewness); + functor.exec(skewness); b_ima += step; d_ima += step; size_bl += delta_size_bl; @@ -367,10 +371,11 @@ namespace scribo { // D* + A - B - C* 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_bc * s_2, - mean, stddev); - functor.exec(mean, stddev); + (d_ima->second().first() - b_ima->second().first()) + (a_ima->second().first() - c_ima->second().first()), + (d_ima->second().second() - b_ima->second().second()) + (a_ima->second().second() - c_ima->second().second()), + size_bc, + skewness); + functor.exec(skewness); a_ima += step; b_ima += step; c_ima += step; @@ -386,16 +391,18 @@ namespace scribo d_ima = d_br; double d_b_sum = d_ima->first() - b_ima->first(), - d_b_sum_2 = d_ima->second() - b_ima->second(); + d_b_sum_2 = d_ima->second().first() - b_ima->second().first(), + d_b_sum_3 = d_ima->second().second() - b_ima->second().second(); for (; col < ncols; col += step) { // D* + A - B* - C* internal::compute_stats(d_b_sum + (a_ima->first() - c_ima->first()), - d_b_sum_2 + (a_ima->second() - c_ima->second()), - size_br * s_2, - mean, stddev); - functor.exec(mean, stddev); + d_b_sum_2 + (a_ima->second().first() - c_ima->second().first()), + d_b_sum_3 + (a_ima->second().second() - c_ima->second().second()), + size_br, + skewness); + functor.exec(skewness); a_ima += step; c_ima += step; size_br -= delta_size_br; @@ -417,4 +424,4 @@ namespace scribo } // end of namespace mln -#endif // ! SCRIBO_CANVAS_INTEGRAL_BROWSING_HH +#endif // ! SCRIBO_CANVAS_INTEGRAL_BROWSING_RV_HH diff --git a/scribo/sandbox/z/sauvola_ms_rv/skewness/skewness2.cc b/scribo/sandbox/z/sauvola_ms_rv/skewness/skewness2.cc new file mode 100644 index 0000000..043e0ad --- /dev/null +++ b/scribo/sandbox/z/sauvola_ms_rv/skewness/skewness2.cc @@ -0,0 +1,230 @@ +#include <mln/io/pgm/all.hh> +#include <mln/io/pbm/all.hh> +#include <mln/core/image/dmorph/image_if.hh> +#include <mln/core/image/dmorph/sub_image.hh> +#include <mln/pw/all.hh> +#include <mln/value/int_u8.hh> +#include <mln/core/image/image2d.hh> +#include <mln/core/neighb.hh> +#include <mln/win/rectangle2d.hh> +#include <mln/extension/adjust.hh> +#include <mln/border/mirror.hh> +#include <mln/extension/duplicate.hh> +#include <mln/core/alias/window2d.hh> + +#include <mln/data/convert.hh> +#include <mln/data/stretch.hh> +#include <mln/debug/println.hh> +#include <mln/data/transform.hh> +#include <mln/io/dump/save.hh> +#include <mln/accu/math/sumpow.hh> +#include <mln/accu/stat/mean.hh> +#include <mln/data/compute_in_window.hh> +#include <mln/core/var.hh> +#include <mln/data/saturate.hh> +#include <mln/fun/v2b/threshold_le.hh> +#include <mln/arith/revert.hh> +#include <mln/util/timer.hh> + +mln::image2d<double> skewness; + +#include <scribo/binarization/sauvola_ms.hh> +#include "integral_browsing_rv.hh" + +mln::image2d<bool> skewness_pbm; +std::string prefix; + + +namespace scribo +{ + using namespace mln; + + template <typename I, typename J> + void + integral_rv(const Image<I>& input_, + Image<J>& integral_sum_sum_2_sum_3_) + { + trace::entering("subsampling::impl::integral_2"); + + const unsigned scale = 1; + + const I& input = exact(input_); + J& integral_sum_sum_2_sum_3 = exact(integral_sum_sum_2_sum_3_); + + typedef mln_value(I) V; + typedef mln_sum(V) S; + typedef mln_site(I) P; + typedef mln_value(J) V2; + + mlc_bool(P::dim == 2)::check(); + mln_precondition(input.is_valid()); + mln_precondition(input.domain().pmin() == literal::origin); + + initialize(integral_sum_sum_2_sum_3, input); + V2* p_integ = integral_sum_sum_2_sum_3.buffer(); + + const int up = integral_sum_sum_2_sum_3.delta_index(dpoint2d(-1, 0)); + + const unsigned nrows = input.nrows(); + const unsigned ncols = input.ncols(); + + unsigned border_thickness = input.border(); + unsigned b_offset = integral_sum_sum_2_sum_3.delta_index(dpoint2d(border_thickness, + border_thickness)); + p_integ += b_offset; + + unsigned row = 0; + { + S h_sum = 0, h_sum_2 = 0, h_sum_3 = 0; + const V* ptr1 = & input.at_(row, 0); + for (unsigned col = 0; col < ncols; ++col) + { + V v11 = *ptr1++; + h_sum += v11; + h_sum_2 += v11 * v11; + h_sum_3 += v11 * v11 * v11; + + // exception + p_integ->first() = h_sum; + p_integ->second().first() = h_sum_2; + p_integ->second().second() = h_sum_3; + + ++p_integ; + } + } + + unsigned b_next = 2 * border_thickness; + + p_integ += b_next; + + for (row += scale; row < nrows; ++row) + { + S h_sum = 0, h_sum_2 = 0, h_sum_3 = 0; + const V* ptr1 = & input.at_(row, 0); + for (unsigned col = 0; col < ncols; ++col) + { + V v11 = *ptr1++; + h_sum += v11; + h_sum_2 += v11 * v11; + h_sum_3 += v11 * v11 * v11; + + p_integ->first() = h_sum + (p_integ + up)->first(); + p_integ->second().first() = h_sum_2 + (p_integ + up)->second().first(); + p_integ->second().second() = h_sum_3 + (p_integ + up)->second().second(); + + ++p_integ; + } + + p_integ += b_next; + } + + trace::exiting("subsampling::impl::integral_2"); + } + +} // end of namespace scribo + + +namespace mln +{ + + template <typename I> + struct invert_on_skewness + { + I input; + image2d<double> skewness_; + mln_fwd_pixter(I) pxl; + + invert_on_skewness(const I& input_) + : input(duplicate(input_)), + pxl(input) + { + pxl.start(); + initialize(skewness_, input); + } + + void exec(double skewness) + { + skewness_.element(pxl.offset()) = skewness; + + if (skewness > 1000.) + pxl.val() = 255 - pxl.val(); + + pxl.next(); // next pixel + } + + void finalize() + { + } + }; + +} + +int main(int argc, char *argv[]) +{ + using namespace mln; + + if (argc != 4) + { + std::cerr << "Usage: " << argv[0] << " <input.pgm> <prefix> <win_size>" << std::endl; + return 1; + } + + util::timer tt; + tt.start(); + util::timer t; + + t.start(); + image2d<value::int_u8> input; + io::pgm::load(input, argv[1]); + + prefix = argv[2]; + + int win_size = atoi(argv[3]); + t.stop(); + std::cout << "Initialization - " << t << std::endl; + + // invert data if skewness > 0 + t.restart(); + + image2d<util::couple<double, util::couple<double, double> > > integral_sum_sum_2_sum_3; + scribo::integral_rv(input, integral_sum_sum_2_sum_3); + + invert_on_skewness<image2d<value::int_u8> > f(input); + scribo::canvas::integral_browsing_rv(integral_sum_sum_2_sum_3, win_size, win_size, f); + + t.stop(); + std::cout << "invert on skewness - " << t << std::endl; + + io::dump::save(f.skewness_, prefix + "skewness.dump"); + + t.restart(); + image2d<bool> bin = scribo::binarization::sauvola_ms(f.input, 101, 2); + std::cout << "sauvola_ms - " << t << std::endl; + + + std::cout << "Total time : " << tt << std::endl; + // prefix += "i_"; + + // image2d<bool> bin_i = scribo::binarization::sauvola(arith::revert(input), 51); + + // prefix = argv[2]; + + io::pbm::save(bin, prefix + "bin.pbm"); + // io::pbm::save(bin_i, prefix + "bin_i.pbm"); + + // image2d<bool> out; + // initialize(out, bin); + // mln_piter_(image2d<bool>) p(out.domain()); + // for_all(p) + // if (skewness_pbm(p)) + // out(p) = bin(p); + // else + // out(p) = bin_i(p); + + // io::pbm::save(out, prefix + "output.pbm"); + + // io::dump::save(scribo::binarization::internal::debug_stddev, prefix + "stddev.dump"); + // io::dump::save(scribo::binarization::internal::debug_mean, prefix + "mean.dump"); + // io::dump::save(scribo::binarization::internal::debug_threshold, prefix + "threshold.dump"); + +} diff --git a/scribo/sandbox/z/skewness/skewness2.cc b/scribo/sandbox/z/skewness/skewness2.cc deleted file mode 100644 index 642ae3f..0000000 --- a/scribo/sandbox/z/skewness/skewness2.cc +++ /dev/null @@ -1,154 +0,0 @@ -#include <mln/io/pgm/all.hh> -#include <mln/io/pbm/all.hh> -#include <mln/core/image/dmorph/image_if.hh> -#include <mln/core/image/dmorph/sub_image.hh> -#include <mln/pw/all.hh> -#include <mln/value/int_u8.hh> -#include <mln/core/image/image2d.hh> -#include <mln/core/neighb.hh> -#include <mln/win/rectangle2d.hh> -#include <mln/extension/adjust.hh> -#include <mln/border/mirror.hh> -#include <mln/extension/duplicate.hh> -#include <mln/core/alias/window2d.hh> - -#include <mln/data/convert.hh> -#include <mln/data/stretch.hh> -#include <mln/debug/println.hh> -#include <mln/data/transform.hh> -#include <mln/io/dump/save.hh> -#include <mln/accu/math/sumpow.hh> -#include <mln/accu/stat/mean.hh> -#include <mln/data/compute_in_window.hh> -#include <mln/core/var.hh> -#include <mln/data/saturate.hh> -#include <mln/fun/v2b/threshold_le.hh> -#include <mln/arith/revert.hh> -#include <mln/util/timer.hh> - -mln::image2d<double> skewness; - -#include <scribo/binarization/sauvola_ms.hh> - -mln::image2d<bool> skewness_pbm; -std::string prefix; - - -int main(int argc, char *argv[]) -{ - using namespace mln; - - if (argc != 4) - { - std::cerr << "Usage: " << argv[0] << " <input.pgm> <prefix> <win_size>" << std::endl; - return 1; - } - - util::timer tt; - tt.start(); - util::timer t; - - t.start(); - image2d<value::int_u8> input; - io::pgm::load(input, argv[1]); - - prefix = argv[2]; - - int win_size = atoi(argv[3]); - win::rectangle2d wr = win::rectangle2d(win_size,win_size); - window2d win; - win.insert(wr); - - accu::pair<accu::stat::mean<value::int_u8,double>, - accu::pair<accu::math::sumpow<2,value::int_u8,double>, - accu::math::sumpow<3,value::int_u8,double> > > accu; - t.stop(); - std::cout << "initialization - " << t << std::endl; - - - t.restart(); - mln_VAR(res, data::compute_in_window(accu, input, win)); - t.stop(); - std::cout << "compute in window - " << t << std::endl; - - initialize(skewness, res); - data::fill(skewness, 0); - - - point2d pc = input.domain().pcenter(); - std::cout << pc << " - sum_2 = " << res(pc).second.first << " - sum_3 = " << res(pc).second.second << " - mean = " << res(pc).first << std::endl; - - t.restart(); - { - int n; - mln_pixter_(image2d<double>) p(skewness); - for_all(p) - { - n = win.size(); - mln_VAR(d, res.element(p.offset())); - double m = d.first; - double sum_3 = d.second.second; - double sum_2 = d.second.first; - p.val() = (sum_3 - 3. * m * sum_2) / (double) n + 2. * std::pow(m, 3); - } - } - t.stop(); - std::cout << "Compute unskew - " << t << std::endl; - - - // t.restart(); - // skewness_pbm = data::transform(skewness, fun::v2b::threshold_le<double>(0)); - // t.stop(); - // std::cout << "binarize - " << t << std::endl; - - // { - // io::dump::save(skewness, prefix + "skewness.dump"); - // io::pgm::save(data::convert(value::int_u8(), skewness), prefix + "skewness_u8.pgm"); - // io::pgm::save(data::stretch(value::int_u8(), skewness), prefix + "skewness_stretch.pgm"); - // io::pbm::save(skewness_pbm, prefix + "skewness.pbm"); - // } - - t.restart(); - { - border::resize(skewness, input.border()); - mln_pixter_(image2d<value::int_u8>) p(input); - for_all(p) - { - if (skewness.element(p.offset()) > 0) - p.val() = mln_max(value::int_u8) - p.val(); - } - } - t.stop(); - std::cout << "Negate parts of input image - " << t << std::endl; - - t.restart(); - image2d<bool> bin = scribo::binarization::sauvola_ms(input, 101, 2); - std::cout << "sauvola_ms - " << t << std::endl; - - - std::cout << "Total time : " << tt << std::endl; - // prefix += "i_"; - - // image2d<bool> bin_i = scribo::binarization::sauvola(arith::revert(input), 51); - - // prefix = argv[2]; - - io::pbm::save(bin, prefix + "bin.pbm"); - // io::pbm::save(bin_i, prefix + "bin_i.pbm"); - - // image2d<bool> out; - // initialize(out, bin); - // mln_piter_(image2d<bool>) p(out.domain()); - // for_all(p) - // if (skewness_pbm(p)) - // out(p) = bin(p); - // else - // out(p) = bin_i(p); - - // io::pbm::save(out, prefix + "output.pbm"); - - // io::dump::save(scribo::binarization::internal::debug_stddev, prefix + "stddev.dump"); - // io::dump::save(scribo::binarization::internal::debug_mean, prefix + "mean.dump"); - // io::dump::save(scribo::binarization::internal::debug_threshold, prefix + "threshold.dump"); - -} -- 1.7.2.5
participants (1)
-
Guillaume Lazzara