$extrastylesheet
Olena  User documentation 2.1
An Image Processing Platform
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
lines_pattern.hh
1 // Copyright (C) 2009, 2010, 2012 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_PRIMITIVE_EXTRACT_LINES_PATTERN_HH
28 # define SCRIBO_PRIMITIVE_EXTRACT_LINES_PATTERN_HH
29 
35 
36 # include <mln/core/concept/image.hh>
37 # include <mln/core/concept/window.hh>
38 # include <mln/core/routine/duplicate.hh>
39 
40 # include <mln/extension/adjust_fill.hh>
41 
42 # include <mln/geom/ncols.hh>
43 
44 # include <mln/accu/transform_line.hh>
45 # include <mln/accu/count_value.hh>
46 
47 # include <mln/data/paste.hh>
48 
49 
50 namespace scribo
51 {
52 
53  namespace primitive
54  {
55 
56  namespace extract
57  {
58 
59  using namespace mln;
60 
72  template <typename I, typename W>
73  mln_concrete(I)
74  lines_pattern(const Image<I>& input_, unsigned length,
75  unsigned dir, const Window<W>& win_);
76 
77 
78 # ifndef MLN_INCLUDE_ONLY
79 
80 
81  // Implementations
82 
83  namespace impl
84  {
85 
86  namespace generic
87  {
88 
89  template <typename I, typename W>
90  mln_concrete(I)
91  lines_pattern(const Image<I>& input_, unsigned length,
92  unsigned dir, const Window<W>& win_)
93  {
94  mln_trace("scribo::primitive::extract::impl::generic::lines_pattern");
95 
96  const I& input = exact(input_);
97  const W& win = exact(win_);
98  mlc_is(mln_value(I), bool)::check();
99  mln_precondition(input.is_valid());
100 
101  // Adjusting extension.
102  extension::adjust_fill(input, length / 2, 0);
103 
104  accu::count_value<bool> accu(true);
105  mln_ch_value(I,unsigned)
106  tmp = accu::transform_line(accu, input, length, dir);
107 
108  mln_concrete(I) output;
109  initialize(output, input);
110 
111  mln_piter(I) p(input.domain());
112  mln_qiter(W) q(win, p);
113  bool is_foreground;
114  for_all(p)
115  {
116 
117  // If the foreground part of the pattern has more than 20%
118  // of background pixels, the current pixel is considered as
119  // background pixel.
120  if (length - tmp(p) > unsigned(0.2f * length) + 1)
121  {
122  output(p) = false;
123  continue;
124  }
125 
126  // If the background parts of the pattern have exactly or
127  // less than 95% of background pixels, the current pixel is
128  // considered as part of the background.
129  is_foreground = true;
130  for_all(q)
131  if ((length - tmp(q)) < unsigned(length * 0.95f) + 1)
132  {
133  is_foreground = false;
134  break;
135  }
136 
137  output(p) = is_foreground;
138  }
139 
140  return output;
141  }
142 
143  } // end of namespace scribo::primitive::extract::impl::generic
144 
145 
146 
147  template <typename I, typename W>
148  mln_concrete(I)
149  lines_pattern_fast(const Image<I>& input_, unsigned length,
150  unsigned dir, const Window<W>& win_)
151  {
152  mln_trace("scribo::primitive::extract::impl::lines_pattern_fast");
153 
154  const I& input = exact(input_);
155  const W& win = exact(win_);
156  mlc_is(mln_value(I), bool)::check();
157  mln_precondition(input.is_valid());
158 
159  // Adjusting extension.
160  extension::adjust_fill(input, length / 2, 0);
161 
162  accu::count_value<bool> accu(true);
163  mln_ch_value(I,unsigned)
164  tmp = accu::transform_line(accu, input, length, dir);
165 
166  mln_concrete(I) output;
167  initialize(output, input);
168 
169  mln::util::array<int>
170  q_arr = offsets_wrt(output, win);
171 
172  bool is_foreground;
173  unsigned ncols = geom::ncols(output);
174  unsigned hit_ratio = unsigned(0.2f * length + 1);
175  unsigned miss_ratio = unsigned(0.95f * length + 1);
176 
177  mln_box_runstart_piter(I) p(output.domain());
178  for_all(p)
179  {
180  unsigned pi = output.offset_of_point(p);
181  unsigned *tmp_ptr = &tmp.element(pi);
182  unsigned *end_ptr = tmp_ptr + ncols;
183 
184  mln_value(I) *out_ptr = &output.element(pi);
185 
186  for (; tmp_ptr < end_ptr; ++out_ptr, ++tmp_ptr)
187  {
188 
189  // If the foreground part of the pattern has more than 20%
190  // of background pixels, the current pixel is considered as
191  // background pixel.
192  if (length - *tmp_ptr > hit_ratio)
193  {
194  *out_ptr = false;
195  continue;
196  }
197 
198  // If the background parts of the pattern have exactly
199  // or less than 95% of background pixels each, the
200  // current pixel is considered as part of the
201  // background.
202  is_foreground = true;
203  for (unsigned i = 0; i < q_arr.size(); ++i)
204  if ((length - *(tmp_ptr + q_arr[i])) < miss_ratio)
205  {
206  is_foreground = false;
207  break;
208  }
209 
210  *out_ptr = is_foreground;
211  }
212  }
213 
214  return output;
215  }
216 
217  } // end of namespace scribo::primitive::extract::impl
218 
219 
220 
221  // Dispatch
222 
223  namespace internal
224  {
225 
226  template <typename I, typename W>
227  mln_concrete(I)
228  lines_pattern_dispatch(mln::trait::image::value_storage::any,
229  mln::trait::image::value_access::any,
230  mln::trait::image::ext_domain::any,
231  const Image<I>& input, unsigned length,
232  unsigned dir, const Window<W>& win)
233  {
234  return impl::generic::lines_pattern(input, length, dir, win);
235  }
236 
237 
238  template <typename I, typename W>
239  mln_concrete(I)
240  lines_pattern_dispatch(mln::trait::image::value_storage::one_block,
241  mln::trait::image::value_access::direct,
242  mln::trait::image::ext_domain::some,
243  const Image<I>& input, unsigned length,
244  unsigned dir, const Window<W>& win)
245  {
246  return impl::lines_pattern_fast(input, length, dir, win);
247  }
248 
249 
250  template <typename I, typename W>
251  mln_concrete(I)
252  lines_pattern_dispatch(const Image<I>& input, unsigned length,
253  unsigned dir, const Window<W>& win)
254  {
255  return lines_pattern_dispatch(mln_trait_image_value_storage(I)(),
256  mln_trait_image_value_access(I)(),
257  mln_trait_image_ext_domain(I)(),
258  input,
259  length,
260  dir, win);
261  }
262 
263  } // end of namespace scribo::primitive::extract::internal
264 
265 
266  // Facade
267 
268  template <typename I, typename W>
269  mln_concrete(I)
270  lines_pattern(const Image<I>& input, unsigned length,
271  unsigned dir, const Window<W>& win)
272  {
273  mln_trace("scribo::primitive::extract::lines_pattern");
274 
275  mlc_is(mln_value(I), bool)::check();
276  mln_precondition(exact(input).is_valid());
277  mln_precondition(exact(win).is_valid());
278  mln_precondition(length != 0);
279  mln_precondition(dir == 0 || dir == 1);
280 
281  mln_concrete(I)
282  output = internal::lines_pattern_dispatch(input, length, dir, win);
283 
284  return output;
285  }
286 
287 
288 # endif // ! MLN_INCLUDE_ONLY
289 
290  } // end of namespace scribo::primitive::extract
291 
292  } // end of namespace scribo::primitive
293 
294 } // end of namespace scribo
295 
296 
297 #endif // ! SCRIBO_PRIMITIVE_EXTRACT_LINES_PATTERN_HH