$extrastylesheet
Olena  User documentation 2.1
An Image Processing Platform
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
local_threshold_core.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_LOCAL_THRESHOLD_CORE_HH
28 # define SCRIBO_BINARIZATION_INTERNAL_LOCAL_THRESHOLD_CORE_HH
29 
33 
34 # include <mln/core/concept/image.hh>
35 # include <mln/data/transform.hh>
36 # include <mln/value/int_u8.hh>
37 
38 # include <mln/border/mirror.hh>
39 # include <mln/border/adjust.hh>
40 
41 # include <scribo/canvas/integral_browsing.hh>
42 
43 # include <scribo/util/init_integral_image.hh>
44 # include <scribo/util/integral_sum_sum2_functor.hh>
45 # include <scribo/util/compute_sub_domains.hh>
46 
47 # include <scribo/debug/logger.hh>
48 # ifdef SCRIBO_LOCAL_THRESHOLD_DEBUG
49 # include <scribo/binarization/internal/local_threshold_debug.hh>
50 # include <mln/io/pgm/save.hh>
51 # include <mln/io/pbm/save.hh>
52 # include <mln/data/saturate.hh>
53 # include <mln/debug/filename.hh>
54 # endif // ! SCRIBO_LOCAL_THRESHOLD_DEBUG
55 
56 
57 namespace scribo
58 {
59 
60  namespace binarization
61  {
62 
63  namespace internal
64  {
65 
66  using namespace mln;
67 
68 
77  template <typename I, typename F>
78  void
79  local_threshold_core(const Image<I>& input, F& f,
80  unsigned window_size);
81 
82  template <typename I, typename F>
83  void
84  local_threshold_core(const Image<I>& input, F& f,
85  unsigned window_size);
86 
87 
90  //
91  template <typename I, typename F>
92  void
93  local_threshold_core(const Image<I>& input, F& f);
94 
95 
96 # ifndef MLN_INCLUDE_ONLY
97 
98 
99  // Implementations.
100 
101  namespace impl
102  {
103 
104  namespace generic
105  {
106 
107  using namespace mln;
108 
109  template <typename I, typename F>
110  void
111  local_threshold_core(const Image<I>& /* input */,
112  unsigned /* window_size */)
113  {
114  mln_trace("scribo::binarization::internal::impl::generic::local_threshold_core");
115 
116  // Not implemented
117  mlc_abort(I)::check();
118 
119  }
120 
121  } // end of namespace scribo::binarization::internal::impl::generic
122 
123 
124  template <typename I, typename F>
125  void
126  local_threshold_core_fastest(const Image<I>& input, F& f, unsigned window_size)
127  {
128  mln_trace("scribo::binarization::internal::impl::generic::local_threshold_core");
129  mln_precondition(exact(input).is_valid());
130 
132 
133  int integral_scale_ratio = F::step;
134 
135  // Make sure the image sizes are a multiple of
136  // integral_scale_ratio in each dimension. (browsing while
137  // binarizing relies on that property).
139  sub_domains = scribo::util::compute_sub_domains(input, 1,
140  integral_scale_ratio);
141 
142  border::adjust(input, sub_domains(1).second());
143  border::mirror(input);
144 
147  integral = scribo::util::init_integral_image(input, integral_scale_ratio,
148  fi,
149  sub_domains[2].first(),
150  sub_domains[2].second());
151 
152  scribo::debug::logger().stop_time_logging("image integrale -");
153 
154 # ifdef SCRIBO_LOCAL_THRESHOLD_DEBUG
155  initialize(internal::debug_mean, input);
156  initialize(internal::debug_stddev, input);
157 
158  initialize(internal::debug_threshold, input);
159  initialize(internal::debug_alpham, input);
160  initialize(internal::debug_alphacond, input);
161 # endif // ! SCRIBO_LOCAL_THRESHOLD_DEBUG
162 
164 
165  window_size /= integral_scale_ratio;
166  if (window_size % 2)
167  window_size += 2;
168  else
169  window_size += 1;
170 
171  scribo::canvas::integral_browsing(integral, 1, window_size,
172  window_size, integral_scale_ratio,
173  f);
174 
175  scribo::debug::logger().stop_time_logging("Binarization -");
176 
177  }
178 
179 
180  } // end of namespace scribo::binarization::internal::impl
181 
182 
183 
184  // Dispatch
185 
186  namespace internal
187  {
188 
189  template <typename I, typename F>
190  void
191  local_threshold_core_dispatch(const mln_value(I)&,
192  const Image<I>& input, F& f,
193  unsigned window_size)
194  {
195  impl::generic::local_threshold_core(input, f, window_size);
196  }
197 
198  template <typename I, typename F>
199  void
200  local_threshold_core_dispatch(mln::trait::image::speed::fastest,
201  const mln_value(I)&,
202  const Image<I>& input, F& f,
203  unsigned window_size)
204  {
205  impl::local_threshold_core_fastest(input, f, window_size);
206  }
207 
208 
209  template <typename I, typename F>
210  void
211  local_threshold_core_dispatch(const Image<I>& input, F& f,
212  unsigned window_size)
213  {
214  typedef mln_value(I) V;
215  local_threshold_core_dispatch(mln_trait_image_speed(I)(),
216  V(), input, f, window_size);
217  }
218 
219  } // end of namespace scribo::binarization::internal::internal
220 
221 
222 
223  // Facades
224 
225  template <typename I, typename F>
226  void
227  local_threshold_core(const Image<I>& input, F& f, unsigned window_size)
228  {
229  mln_trace("scribo::binarization::internal::local_threshold_core");
230 
231  mln_precondition(exact(input).is_valid());
232 
233  internal::local_threshold_core_dispatch(input, f, window_size);
234 
235 # ifdef SCRIBO_LOCAL_THRESHOLD_DEBUG
236  if (stddev_image_output)
237  io::pgm::save(data::saturate(value::int_u8(), debug_stddev),
238  mln::debug::filename(stddev_image_output));
239  if (mean_image_output)
240  io::pgm::save(data::saturate(value::int_u8(), debug_mean),
241  mln::debug::filename(mean_image_output));
242  if (threshold_image_output)
243  io::pgm::save(data::saturate(value::int_u8(), debug_threshold),
244  mln::debug::filename(threshold_image_output));
245 
246  if (alpham_image_output)
247  io::pgm::save(data::saturate(value::int_u8(), debug_alpham),
248  mln::debug::filename(alpham_image_output));
249  if (alphacond_image_output)
250  io::pbm::save(debug_alphacond,
251  mln::debug::filename(alphacond_image_output));
252 # endif // ! SCRIBO_LOCAL_THRESHOLD_DEBUG
253 
254  }
255 
256  template <typename I, typename F>
257  void
258  local_threshold_core(const Image<I>& input, F& f)
259  {
260  local_threshold_core(input, f, 11);
261  }
262 
263 
264 # endif // ! MLN_INCLUDE_ONLY
265 
266  } // end of namespace scribo::binarization::internal
267 
268  } // end of namespace scribo::binarization
269 
270 } // end of namespace scribo
271 
272 
273 #endif // ! SCRIBO_BINARIZATION_INTERNAL_LOCAL_THRESHOLD_CORE_HH