Milena (Olena)  User documentation 2.0a Id
 All Classes Namespaces Functions Variables Typedefs Enumerator Groups Pages
canvas/labeling/blobs.hh
1 // Copyright (C) 2009, 2010 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 MLN_CANVAS_LABELING_BLOBS_HH
28 # define MLN_CANVAS_LABELING_BLOBS_HH
29 
34 
35 # include <mln/core/concept/image.hh>
36 # include <mln/core/concept/neighborhood.hh>
37 # include <mln/data/fill.hh>
38 # include <mln/core/site_set/p_queue_fast.hh>
39 
40 # include <mln/extension/fill.hh>
41 
42 # include <mln/util/pix.hh>
43 
44 namespace mln
45 {
46 
47  namespace canvas
48  {
49 
50  namespace labeling
51  {
52 
69  template <typename I, typename N, typename L, typename F>
70  mln_ch_value(I, L)
71  blobs(const Image<I>& input_, const Neighborhood<N>& nbh_,
72  L& nlabels, F& functor);
73 
74 
75 
76 # ifndef MLN_INCLUDE_ONLY
77 
78 
79  // Implementations.
80 
81  namespace impl
82  {
83 
84  // Generic implementation.
85 
86  namespace generic
87  {
88 
89  template <typename I, typename N, typename L, typename F>
90  mln_ch_value(I, L)
91  blobs(const Image<I>& input_, const N& nbh, L& nlabels, F& functor)
92  {
93  const I& input = exact(input_);
94 
95  typedef mln_psite(I) P;
96 
97  P cur;
98  mln_niter(N) n(nbh, cur);
99  p_queue_fast<P> qu;
100  const L zero = literal::zero;
101 
102  // Initialization.
103  nlabels = literal::zero;
104  typedef mln_ch_value(I, L) out_t;
105  out_t output;
106  initialize(output, input);
107  data::fill(output, zero);
108 
109  extension::fill(input, false);
110 
111  functor.init(); // <-- functor.init()
112 
113  // Loop.
114  mln_piter(I) p(input.domain());
115  for_all(p)
116  if (input(p) && output(p) == zero) // Object point, not labeled yet.
117  {
118  // Label this point component.
119  if (nlabels == mln_max(L))
120  {
121  trace::warning("labeling aborted! Too many labels \
122 for this label type: nlabels > max(label_type).");
123 
124  return output;
125  }
126  ++nlabels;
127  functor.new_label(nlabels); // <-- functor.new_label()
128  mln_invariant(qu.is_empty());
129  qu.push(p);
130  output(p) = nlabels;
131  functor.process_p(p); // <-- functor.process_p()
132  // output(p) == nlabels
133  do
134  {
135  cur = qu.front();
136  qu.pop();
137  for_all(n) if (input.has(n))
138  if (input(n) && output(n) == zero)
139  {
140  mln_invariant(! qu.compute_has(n));
141  qu.push(n);
142  output(n) = nlabels;
143  functor.process_n(n); // <-- functor.process_n()
144  // output(n) == nlabels
145  }
146  }
147  while (! qu.is_empty());
148  }
149 
150  functor.finalize(); // <-- functor.finalize()
151 
152  return output;
153  }
154 
155  } // end of namespace mln::labeling::impl::generic
156 
157 
158 
159  } // end of namespace mln::canvas::labeling::impl
160 
161 
162  // Facade.
163 
164  template <typename I, typename N, typename L, typename F>
165  inline
166  mln_ch_value(I, L)
167  blobs(const Image<I>& input_, const Neighborhood<N>& nbh_,
168  L& nlabels, F& functor)
169  {
170  trace::entering("labeling::blobs");
171  mlc_equal(mln_trait_image_kind(I),
172  mln::trait::image::kind::binary)::check();
173  const I& input = exact(input_);
174  const N& nbh = exact(nbh_);
175  mln_precondition(input.is_valid());
176 
177  // The only implementation is the generic one.
178  mln_ch_value(I, L)
179  output = impl::generic::blobs(input, nbh, nlabels, functor);
180 
181  trace::exiting("labeling::blobs");
182  return output;
183  }
184 
185 
186 # endif // ! MLN_INCLUDE_ONLY
187 
188  } // end of namespace mln::canvas::labeling
189 
190  } // end of namespace mln::canvas
191 
192 } // end of namespace mln
193 
194 
195 #endif // ! MLN_CANVAS_LABELING_BLOBS_HH