$extrastylesheet
Olena  User documentation 2.1
An Image Processing Platform
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
wolf_functor_fast.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_FAST_HH
28 # define SCRIBO_BINARIZATION_INTERNAL_WOLF_FUNCTOR_FAST_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>
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 = 3 };
67 
68  wolf_functor_fast(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_line3;
96  unsigned offset1;
97  unsigned offset2;
98  };
99 
100 #ifndef MLN_INCLUDE_ONLY
101 
102  template <typename I>
104  double K,
105  const mln_value(I)& global_min,
106  double global_max_stddev)
107  : input(exact(input_)),
108  pi(&input(input.domain().pmin())),
109  K_(K),
110  global_min_(global_min),
111  global_max_stddev_(global_max_stddev)
112  {
113  mln_precondition(exact(input).is_valid());
114  mln_precondition(K > 0.);
115  }
116 
117  template <typename I>
118  void
119  wolf_functor_fast<I>::init()
120  {
121  // This initialization MUST be done here since input image
122  // borders may have changed!
123 
124  // Since we iterate from a smaller image in the largest ones
125  // and image at scale 1 does not always have a size which can
126  // be divided by 3, some sites in the border may not be
127  // processed and we must skip them.
128  int more_offset = - (3 - input.ncols() % 3);
129  if (more_offset == - 3)
130  more_offset = 0; // No offset needed.
131 
132  next_line3 = input.delta_offset(dpoint2d(+2,0))
133  + 2 * input.border() + more_offset;
134 
135  offset1 = input.delta_offset(dpoint2d(+1,0));
136  offset2 = input.delta_offset(dpoint2d(+2,0));
137 
138  initialize(output, input);
139  po = &output(output.domain().pmin());
140  }
141 
142  template <typename I>
143  void
144  wolf_functor_fast<I>::exec(double mean, double stddev)
145  {
146  mln_assertion(input.border() == output.border());
147 
148  double th = formula_(mean, stddev, K_,
149  global_max_stddev_, global_min_);
150 
151  for (int i = 0; i < step; ++i, ++po, ++pi)
152  {
153  *po = (*pi <= th);
154  *(po + offset1) = (*(pi + offset1) <= th);
155  *(po + offset2) = (*(pi + offset2) <= th);
156  }
157 
158 # ifdef SCRIBO_LOCAL_THRESHOLD_DEBUG
159  // Store local mean
160  unsigned index = pi - input.buffer();
161 
162  debug_mean.element(index) = mean * mean_debug_factor;
163  debug_stddev.element(index) = stddev * stddev_debug_factor;
164  debug_threshold.element(index) = th;
165 
166  double alpha = K_ * (1 - stddev / R_);
167  debug_alpham.element(index) = alpha * mean * alpham_debug_factor;
168  debug_alphacond.element(index) = (stddev < (alpha * mean / 2.));
169 # endif // ! SCRIBO_LOCAL_THRESHOLD_DEBUG
170 
171  }
172 
173  template <typename I>
174  void
175  wolf_functor_fast<I>::end_of_row(int)
176  {
177  po += next_line3;
178  pi += next_line3;
179  }
180 
181  template <typename I>
182  void
183  wolf_functor_fast<I>::finalize()
184  {
185  }
186 
187 #endif // ! MLN_INCLUDE_ONLY
188 
189  } // end of namespace scribo::binarization::internal
190 
191  } // end of namespace scribo::binarization
192 
193 } // end of namespace scribo
194 
195 #endif // SCRIBO_BINARIZATION_INTERNAL_WOLF_FUNCTOR_FAST_HH