
* scribo/binarization/otsu.hh, * scribo/binarization/otsu_threshold.hh: Move threshold computation in a separate routine. --- scribo/ChangeLog | 8 +++ scribo/scribo/binarization/otsu.hh | 56 +------------------- .../binarization/{otsu.hh => otsu_threshold.hh} | 31 +++++------ 3 files changed, 23 insertions(+), 72 deletions(-) copy scribo/scribo/binarization/{otsu.hh => otsu_threshold.hh} (84%) diff --git a/scribo/ChangeLog b/scribo/ChangeLog index 0923b28..6381a60 100644 --- a/scribo/ChangeLog +++ b/scribo/ChangeLog @@ -1,5 +1,13 @@ 2011-10-17 Guillaume Lazzara <z@lrde.epita.fr> + Move Otsu's threshold computation in a separate routine. + + * scribo/binarization/otsu.hh, + * scribo/binarization/otsu_threshold.hh: Move threshold + computation in a separate routine. + +2011-10-17 Guillaume Lazzara <z@lrde.epita.fr> + Add Niblack's binarization algorithm. * scribo/binarization/all.hh: Update includes. diff --git a/scribo/scribo/binarization/otsu.hh b/scribo/scribo/binarization/otsu.hh index c96a207..6b9d492 100644 --- a/scribo/scribo/binarization/otsu.hh +++ b/scribo/scribo/binarization/otsu.hh @@ -27,12 +27,8 @@ # define SCRIBO_BINARIZATION_OTSU_HH # include <mln/core/concept/image.hh> -# include <mln/util/array.hh> -# include <mln/geom/nsites.hh> -# include <mln/geom/ncols.hh> -# include <mln/geom/nrows.hh> -# include <mln/histo/compute.hh> +# include <scribo/binarization/otsu_threshold.hh> # include <scribo/binarization/global_threshold.hh> namespace scribo @@ -72,55 +68,7 @@ namespace scribo mlc_is_a(mln_value(I), value::Scalar)::check(); // FIXME: Check that input value is gray level. - mln_value(I) maxval = mln_max(mln_value(I)); - unsigned nsites = geom::nsites(input); - - /* Histogram generation */ - histo::array<mln_value(I)> hist = mln::histo::compute(input); - - - /* calculation of probability density */ - util::array<double> pdf(hist.nvalues()); //probability distribution - for(int i = 0; i< maxval; ++i) - pdf[i] = (double)hist[i] / nsites; - - - util::array<double> cdf(hist.nvalues()); //cumulative probability distribution - util::array<double> myu(hist.nvalues()); // mean value for separation - - /* cdf & myu generation */ - cdf[0] = pdf[0]; - myu[0] = 0.0; /* 0.0 times prob[0] equals zero */ - - for(int i = 1; i < maxval; ++i) - { - cdf[i] = cdf[i-1] + pdf[i]; - myu[i] = myu[i-1] + i*pdf[i]; - } - - /* sigma maximization - sigma stands for inter-class variance - and determines optimal threshold value */ - int threshold = 0; - double max_sigma = 0.0; - util::array<double> sigma(hist.nvalues()); // inter-class variance - - for(int i = 0; i < maxval - 1; ++i) - { - if(cdf[i] != 0.0 && cdf[i] != 1.0) - { - double p1p2 = cdf[i] * (1.0 - cdf[i]); - double mu1mu2diff = myu[maxval - 1] * cdf[i] - myu[i]; - sigma[i] = mu1mu2diff * mu1mu2diff / p1p2; - } - else - sigma[i] = 0.0; - if(sigma[i] > max_sigma) - { - max_sigma = sigma[i]; - threshold = i; - } - } + mln_value(I) threshold = otsu_threshold(input); // Computing final result. mln_ch_value(I,bool) output = global_threshold(input, threshold); diff --git a/scribo/scribo/binarization/otsu.hh b/scribo/scribo/binarization/otsu_threshold.hh similarity index 84% copy from scribo/scribo/binarization/otsu.hh copy to scribo/scribo/binarization/otsu_threshold.hh index c96a207..ab663d0 100644 --- a/scribo/scribo/binarization/otsu.hh +++ b/scribo/scribo/binarization/otsu_threshold.hh @@ -23,8 +23,8 @@ // exception does not however invalidate any other reasons why the // executable file might be covered by the GNU General Public License. -#ifndef SCRIBO_BINARIZATION_OTSU_HH -# define SCRIBO_BINARIZATION_OTSU_HH +#ifndef SCRIBO_BINARIZATION_OTSU_THRESHOLD_HH +# define SCRIBO_BINARIZATION_OTSU_THRESHOLD_HH # include <mln/core/concept/image.hh> # include <mln/util/array.hh> @@ -33,8 +33,6 @@ # include <mln/geom/nrows.hh> # include <mln/histo/compute.hh> -# include <scribo/binarization/global_threshold.hh> - namespace scribo { @@ -43,29 +41,28 @@ namespace scribo using namespace mln; - /*! An implementation of Otsu's binarization algorithm. + /*! Compute a global binarization threshold using Otsu's algorithm. It is based on Ocropus's implementation. http://code.google.com/p/ocropus/ \param[in] input A gray-scale image. - \return A binary image. True for foreground, False for - background. + \return A global threshold value. */ template <typename I> - mln_ch_value(I,bool) - otsu(const Image<I>& input); + mln_value(I) + otsu_threshold(const Image<I>& input); # ifndef MLN_INCLUDE_ONLY template <typename I> - mln_ch_value(I,bool) - otsu(const Image<I>& input_) + mln_value(I) + otsu_threshold(const Image<I>& input_) { - trace::entering("scribo::binarization::otsu"); + trace::entering("scribo::binarization::otsu_threshold"); const I& input = exact(input_); mln_precondition(input.is_valid()); @@ -101,7 +98,7 @@ namespace scribo /* sigma maximization sigma stands for inter-class variance and determines optimal threshold value */ - int threshold = 0; + mln_value(I) threshold = literal::zero; double max_sigma = 0.0; util::array<double> sigma(hist.nvalues()); // inter-class variance @@ -122,10 +119,8 @@ namespace scribo } } - // Computing final result. - mln_ch_value(I,bool) output = global_threshold(input, threshold); - - return output; + trace::exiting("scribo::binarization::otsu_threshold"); + return threshold; } # endif // ! MLN_INCLUDE_ONLY @@ -134,4 +129,4 @@ namespace scribo } // end of namespace scribo -#endif // ! SCRIBO_BINARIZATION_OTSU_HH +#endif // ! SCRIBO_BINARIZATION_OTSU_THRESHOLD_HH -- 1.7.2.5