$extrastylesheet
Olena  User documentation 2.1
An Image Processing Platform
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
otsu_threshold.hh
1 // Copyright (C) 2011, 2013 EPITA Research and Development Laboratory
2 // (LRDE)
3 //
4 // This file is part of Olena.
5 //
6 // Olena is free software: you can redistribute it and/or modify it under
7 // the terms of the GNU General Public License as published by the Free
8 // Software Foundation, version 2 of the License.
9 //
10 // Olena is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 // General Public License for more details.
14 //
15 // You should have received a copy of the GNU General Public License
16 // along with Olena. If not, see <http://www.gnu.org/licenses/>.
17 //
18 // As a special exception, you may use this file as part of a free
19 // software project without restriction. Specifically, if other files
20 // instantiate templates or use macros or inline functions from this
21 // file, or you compile this file and link it with other files to produce
22 // an executable, this file does not by itself cause the resulting
23 // executable to be covered by the GNU General Public License. This
24 // exception does not however invalidate any other reasons why the
25 // executable file might be covered by the GNU General Public License.
26 
27 #ifndef SCRIBO_BINARIZATION_OTSU_THRESHOLD_HH
28 # define SCRIBO_BINARIZATION_OTSU_THRESHOLD_HH
29 
30 # include <mln/core/concept/image.hh>
31 # include <mln/util/array.hh>
32 # include <mln/geom/nsites.hh>
33 # include <mln/geom/ncols.hh>
34 # include <mln/geom/nrows.hh>
35 # include <mln/histo/compute.hh>
36 
37 namespace scribo
38 {
39 
40  namespace binarization
41  {
42 
43  using namespace mln;
44 
56  template <typename I>
57  mln_value(I)
58  otsu_threshold(const Image<I>& input);
59 
60 
61 # ifndef MLN_INCLUDE_ONLY
62 
63 
64  template <typename I>
65  mln_value(I)
66  otsu_threshold(const Image<I>& input_)
67  {
68  mln_trace("scribo::binarization::otsu_threshold");
69 
70  const I& input = exact(input_);
71  mln_precondition(input.is_valid());
72  mlc_is_a(mln_value(I), value::Scalar)::check();
73  // FIXME: Check that input value is gray level.
74 
75  mln_value(I) maxval = mln_max(mln_value(I));
76  unsigned nsites = geom::nsites(input);
77 
78  /* Histogram generation */
79  histo::array<mln_value(I)> hist = mln::histo::compute(input);
80 
81 
82  /* calculation of probability density */
83  mln::util::array<double> pdf(hist.nvalues()); //probability distribution
84  for(unsigned i = 0; i< maxval; ++i)
85  pdf[i] = (double)hist[i] / nsites;
86 
87 
88  mln::util::array<double> cdf(hist.nvalues()); //cumulative probability distribution
89  mln::util::array<double> myu(hist.nvalues()); // mean value for separation
90 
91  /* cdf & myu generation */
92  cdf[0] = pdf[0];
93  myu[0] = 0.0; /* 0.0 times prob[0] equals zero */
94 
95  for(unsigned i = 1; i < maxval; ++i)
96  {
97  cdf[i] = cdf[i-1] + pdf[i];
98  myu[i] = myu[i-1] + i*pdf[i];
99  }
100 
101  /* sigma maximization
102  sigma stands for inter-class variance
103  and determines optimal threshold value */
104  mln_value(I) threshold = literal::zero;
105  double max_sigma = 0.0;
106  mln::util::array<double> sigma(hist.nvalues()); // inter-class variance
107 
108  for(int i = 0; i < (maxval - 1); ++i)
109  {
110  if(cdf[i] != 0.0 && cdf[i] != 1.0)
111  {
112  double p1p2 = cdf[i] * (1.0 - cdf[i]);
113  double mu1mu2diff = myu[maxval - 1] * cdf[i] - myu[i];
114  sigma[i] = mu1mu2diff * mu1mu2diff / p1p2;
115  }
116  else
117  sigma[i] = 0.0;
118  if(sigma[i] > max_sigma)
119  {
120  max_sigma = sigma[i];
121  threshold = i;
122  }
123  }
124 
125  return threshold;
126  }
127 
128 # endif // ! MLN_INCLUDE_ONLY
129 
130  } // end of namespace scribo::binarization
131 
132 } // end of namespace scribo
133 
134 #endif // ! SCRIBO_BINARIZATION_OTSU_THRESHOLD_HH