$extrastylesheet
Olena  User documentation 2.1
An Image Processing Platform
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
wolf_functor.hh
1 // Copyright (C) 2012, 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_INTERNAL_WOLF_FUNCTOR_HH
28 # define SCRIBO_BINARIZATION_INTERNAL_WOLF_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/wolf_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>
58  struct wolf_functor
59  {
60  typedef mln_value(I) V;
61 
62  // Moves in input and output images are made using "step"
63  // pixels. It corresponds to the scale ratio between the input
64  // image and the integral image used to give the statistics
65  // values.
66  enum { step = 1 };
67 
68  wolf_functor(const Image<I>& input, double K,
69  const mln_value(I)& global_min,
70  double global_max_stddev);
71 
72  void init();
73 
74  // Run every 4 pixels.
75  void exec(double mean, double stddev);
76 
77  void end_of_row(int);
78 
79  void finalize();
80 
81 
82  const I input;
83  mln_ch_value(I,bool) output;
84 
85  const mln_value(I)* pi;
86  bool* po;
87 
88  double K_;
89 
90  V global_min_;
91  double global_max_stddev_;
92 
94 
95  unsigned next_line;
96  };
97 
98 #ifndef MLN_INCLUDE_ONLY
99 
100  template <typename I>
102  double K,
103  const mln_value(I)& global_min,
104  double global_max_stddev)
105  : input(exact(input_)),
106  pi(&input(input.domain().pmin())),
107  K_(K),
108  global_min_(global_min),
109  global_max_stddev_(global_max_stddev)
110  {
111  mln_precondition(exact(input).is_valid());
112  mln_precondition(K > 0.);
113  }
114 
115  template <typename I>
116  void
117  wolf_functor<I>::init()
118  {
119  // This initialization MUST be done here since input image
120  // borders may have changed!
121  next_line = 2 * input.border();
122 
123  initialize(output, input);
124  po = &output(output.domain().pmin());
125  }
126 
127  template <typename I>
128  void
129  wolf_functor<I>::exec(double mean, double stddev)
130  {
131  mln_assertion(input.border() == output.border());
132 
133  double th = formula_(mean, stddev, K_,
134  global_max_stddev_, global_min_);
135 
136  *po++ = (*pi++ <= th);
137 
138 # ifdef SCRIBO_LOCAL_THRESHOLD_DEBUG
139  // Store local mean
140  unsigned index = pi - input.buffer() - 1;
141 
142  debug_mean.element(index) = mean * mean_debug_factor;
143  debug_stddev.element(index) = stddev * stddev_debug_factor;
144  debug_threshold.element(index) = th;
145 
146  double alpha = K_ * (1 - stddev / R_);
147  debug_alpham.element(index) = alpha * mean * alpham_debug_factor;
148  debug_alphacond.element(index) = (stddev < (alpha * mean / 2.));
149 # endif // ! SCRIBO_LOCAL_THRESHOLD_DEBUG
150 
151  }
152 
153  template <typename I>
154  void
155  wolf_functor<I>::end_of_row(int)
156  {
157  po += next_line;
158  pi += next_line;
159  }
160 
161  template <typename I>
162  void
163  wolf_functor<I>::finalize()
164  {
165  }
166 
167 #endif // ! MLN_INCLUDE_ONLY
168 
169  } // end of namespace scribo::binarization::internal
170 
171  } // end of namespace scribo::binarization
172 
173 } // end of namespace scribo
174 
175 #endif // SCRIBO_BINARIZATION_INTERNAL_WOLF_FUNCTOR_HH