$extrastylesheet
Olena  User documentation 2.1
An Image Processing Platform
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
local_threshold.hh
1 // Copyright (C) 2009, 2010, 2011, 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_LOCAL_THRESHOLD_HH
28 # define SCRIBO_BINARIZATION_LOCAL_THRESHOLD_HH
29 
30 # include <mln/core/concept/image.hh>
31 # include <mln/value/concept/vectorial.hh>
32 
36 
37 namespace scribo
38 {
39 
40  using namespace mln;
41 
42  namespace binarization
43  {
44 
59  //
60  template <typename I, typename T>
61  mln_ch_value(I, bool)
62  local_threshold(const Image<I>& input, const Image<T>& threshold);
63 
64 
65 # ifndef MLN_INCLUDE_ONLY
66 
67  // Tests
68 
69  namespace internal
70  {
71 
72  template <typename I, typename T>
73  void
74  local_threshold_tests(const Image<I>& input, const Image<T>& threshold)
75  {
76  mln_precondition(exact(input).is_valid());
77  mln_precondition(exact(threshold).is_valid());
78  mln_precondition(exact(input).domain() == exact(threshold).domain());
79  mlc_is_not_a(mln_value(I), value::Vectorial)::check();
80  (void) input;
81  (void) threshold;
82  }
83 
84  } // end of namespace scribo::binarization::internal
85 
86 
87 
88  // Implementations
89 
90  namespace impl
91  {
92 
93  namespace generic
94  {
95 
96  template <typename I, typename T>
97  mln_ch_value(I, bool)
98  local_threshold(const Image<I>& input_, const Image<T>& threshold_)
99  {
100  mln_trace("scribo::binarization::impl::generic::local_threshold");
101 
102  internal::local_threshold_tests(input_, threshold_);
103 
104  const I& input = exact(input_);
105  const T& threshold = exact(threshold_);
106 
107  mln_ch_value(I, bool) output;
108  initialize(output, input);
109 
110  mln_piter(I) p(input.domain());
111  for_all(p)
112  output(p) = (input(p) <= threshold(p));
113 
114  return output;
115  }
116 
117 
118  } // end of namespace scribo::binarization::impl::generic
119 
120 
121  template <typename I, typename T>
122  mln_ch_value(I, bool)
123  local_threshold_fastest(const Image<I>& input_,
124  const Image<T>& threshold_)
125  {
126  mln_trace("scribo::binarization::impl::generic::local_threshold_fastest");
127  internal::local_threshold_tests(input_, threshold_);
128 
129  const I& input = exact(input_);
130  const T& threshold = exact(threshold_);
131 
132  typedef mln_ch_value(I, bool) O;
133  O output;
134  initialize(output, input);
135 
136  mln_pixter(const I) pi(input);
137  mln_pixter(const T) pt(threshold);
138  mln_pixter(O) po(output);
139 
140  for_all_3(pi, pt, po)
141  po.val() = (pi.val() <= pt.val());
142 
143  return output;
144  }
145 
146 
147 
148  } // end of namespace scribo::binarization::impl
149 
150 
151 
152  // Dispatch
153 
154  namespace internal
155  {
156 
157  template <typename I, typename T>
158  mln_ch_value(I, bool)
159  local_threshold_dispatch(trait::image::speed::any,
160  trait::image::speed::any,
161  const Image<I>& input, const Image<T>& threshold)
162  {
163  return binarization::impl::generic::local_threshold(input, threshold);
164  }
165 
166 
167  template <typename I, typename T>
168  mln_ch_value(I, bool)
169  local_threshold_dispatch(trait::image::speed::fastest,
170  trait::image::speed::fastest,
171  const Image<I>& input, const Image<T>& threshold)
172  {
173  return binarization::impl::local_threshold_fastest(input, threshold);
174  }
175 
176  template <typename I, typename T>
177  mln_ch_value(I, bool)
178  local_threshold_dispatch(const Image<I>& input,
179  const Image<T>& threshold)
180  {
181  return local_threshold_dispatch(mln_trait_image_speed(I)(),
182  mln_trait_image_speed(T)(),
183  input, threshold);
184  }
185 
186  } // end of namespace scribo::binarization::internal
187 
188 
189 
190  // Facade
191 
192  template <typename I, typename T>
193  mln_ch_value(I, bool)
194  local_threshold(const Image<I>& input, const Image<T>& threshold)
195  {
196  mln_trace("scribo::binarization::local_threshold");
197 
198  internal::local_threshold_tests(input, threshold);
199 
200  mln_ch_value(I, bool)
201  output = internal::local_threshold_dispatch(input, threshold);
202 
203  return output;
204  }
205 
206 
207 # endif // ! MLN_INCLUDE_ONLY
208 
209  } // end of namespace scribo::binarization
210 
211 } // end of namespace scribo
212 
213 
214 #endif // ! SCRIBO_BINARIZATION_LOCAL_THRESHOLD_HH