$extrastylesheet
Olena  User documentation 2.1
An Image Processing Platform
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
sauvola_threshold_functor.hh
1 // Copyright (C) 2011, 2012, 2013 EPITA Research and Development
2 // Laboratory (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_INTERNAL_SAUVOLA_THRESHOLD_FUNCTOR_HH
28 # define SCRIBO_BINARIZATION_INTERNAL_SAUVOLA_THRESHOLD_FUNCTOR_HH
29 
33 
34 # include <mln/core/image/image2d.hh>
35 # include <mln/core/alias/neighb2d.hh>
36 # include <mln/extension/fill.hh>
37 
38 # include <scribo/binarization/internal/sauvola_formula.hh>
39 
40 # ifdef SCRIBO_LOCAL_THRESHOLD_DEBUG
41 # include <scribo/binarization/internal/local_threshold_debug.hh>
42 # endif // ! SCRIBO_LOCAL_THRESHOLD_DEBUG
43 
44 
45 namespace scribo
46 {
47 
48  namespace binarization
49  {
50 
51  namespace internal
52  {
53 
54  using namespace mln;
55 
56 
57  template <typename I>
59  {
60  // Moves in input and output images are made using "step"
61  // pixels. It corresponds to the scale ratio between the input
62  // image and the integral image used to give the statistics
63  // values.
64  enum { step = 3 };
65 
67  double K, double R);
68 
69  void init();
70 
71  // Run every 4 pixels.
72  void exec(double mean, double stddev);
73 
74  void end_of_row(int);
75 
76  void finalize();
77 
78  typedef mln_concrete(I) th_t;
79  th_t output;
80  mln_concrete(I) input;
81 
82  mln_value(I)* po;
83 
84  double K_;
85  double R_;
86 
88 
89  unsigned next_line3;
90  unsigned offset1;
91  unsigned offset2;
92  };
93 
94 #ifndef MLN_INCLUDE_ONLY
95 
96  template <typename I>
98  double K, double R)
99  : input(input),
100  K_(K),
101  R_(R)
102  {
103  mln_precondition(exact(input).is_valid());
104  mln_precondition(K > 0.);
105  mln_precondition(R > 0.);
106  }
107 
108  template <typename I>
109  void
110  sauvola_threshold_functor<I>::init()
111  {
112  // This initialization MUST be done here since input image
113  // borders may have changed!
114 
115  next_line3 = input.delta_offset(dpoint2d(+2,0)) + 2 * input.border() - 1;
116 
117  offset1 = input.delta_offset(dpoint2d(+1,0));
118  offset2 = input.delta_offset(dpoint2d(+2,0));
119 
120  initialize(output, input);
121  po = &output(output.domain().pmin());
122  }
123 
124  template <typename I>
125  void
126  sauvola_threshold_functor<I>::exec(double mean, double stddev)
127  {
128  mln_assertion(input.border() == output.border());
129  static point2d p(0,0);
130 
131  typedef mln_value(I) V;
132  V th = static_cast<V>(formula_(mean, stddev, K_, R_));
133 
134  for (int i = 0; i < step; ++i, ++po)
135  {
136  *po = th;
137  *(po + offset1) = th;
138  *(po + offset2) = th;
139  }
140 
141 # ifdef SCRIBO_LOCAL_THRESHOLD_DEBUG
142  // Store local mean
143  unsigned index = po - output.buffer();
144 
145  internal::debug_mean.element(index) = mean * internal::mean_debug_factor;
146  internal::debug_stddev.element(index) = stddev * internal::stddev_debug_factor;
147  internal::debug_threshold.element(index) = t;
148 
149  double alpha = K * (1 - stddev / R);
150  internal::debug_alpham.element(index) = alpha * mean * internal::alpham_debug_factor;
151  internal::debug_alphacond.element(index) = (stddev < (alpha * mean / 2.));
152 # endif // ! SCRIBO_LOCAL_THRESHOLD_DEBUG
153 
154  }
155 
156  template <typename I>
157  void
158  sauvola_threshold_functor<I>::end_of_row(int)
159  {
160  po += next_line3;
161  }
162 
163  template <typename I>
164  void
165  sauvola_threshold_functor<I>::finalize()
166  {
167  }
168 
169 #endif // ! MLN_INCLUDE_ONLY
170 
171  } // end of namespace scribo::binarization::internal
172 
173  } // end of namespace scribo::binarization
174 
175 } // end of namespace scribo
176 
177 #endif // SCRIBO_BINARIZATION_INTERNAL_SAUVOLA_THRESHOLD_FUNCTOR_HH