Milena (Olena)  User documentation 2.0a Id
 All Classes Namespaces Functions Variables Typedefs Enumerator Groups Pages
blobs_and_compute.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_LABELING_BLOBS_AND_COMPUTE_HH
28 # define MLN_LABELING_BLOBS_AND_COMPUTE_HH
29 
33 
34 
35 # include <mln/core/concept/image.hh>
36 # include <mln/core/concept/neighborhood.hh>
37 
38 # include <mln/labeling/blobs.hh>
39 # include <mln/labeling/compute.hh>
40 
41 
42 namespace mln
43 {
44 
45  namespace labeling
46  {
47 
59  template <typename I, typename N, typename L, typename A>
60  util::couple<mln_ch_value(I,L),
61  util::couple<util::array<mln_result(A)>,
62  util::array<A> > >
63  blobs_and_compute(const Image<I>& input, const Neighborhood<N>& nbh,
64  L& nlabels, const Accumulator<A>& accu);
65 
66 
67 
68 # ifndef MLN_INCLUDE_ONLY
69 
70 
71  namespace internal
72  {
73 
76  template <typename L, typename A>
77  struct compute_functor
78  {
79  typedef mln_result(A) accu_result;
80  typedef mln_argument(A) accu_argument;
81  typedef util::couple<util::array<accu_result>,
82  util::array<A> > result;
83 
84  compute_functor(const mln_value(L)& nlabels)
85  : nlabels_(nlabels)
86  {
87  }
88 
89  inline
90  void init()
91  {
92  accus_.reserve(1000);
93  accus_.append(A());
94  }
95 
96  inline
97  void new_label(const mln_value(L)& l)
98  {
99  current_lbl_ = l;
100  accus_.append(A());
101  }
102 
103  inline
104  void process_p(const mln_site(L)& p)
105  {
106  process__(accu_argument(), p);
107  }
108 
109  inline
110  void process_n(const mln_site(L)& n)
111  {
112  process__(accu_argument(), n);
113  }
114 
115  inline
116  void finalize()
117  {
118  convert::from_to(accus_, result_);
119  }
120 
121 
122  private:
123  inline
124  void process__(const mln_psite(L)&, const mln_site(L)& p)
125  {
126  accus_[current_lbl_].take(p);
127  }
128 
129  inline
130  void process__(const mln_value(L)&, const mln_site(L)&)
131  {
132  accus_[current_lbl_].take(current_lbl_);
133  }
134 
135  template <typename V>
136  inline
137  void process__(const V&, const mln_site(L)&)
138  {
139  mlc_abort(V)::check();
140  }
141 
142  public:
143  util::array<mln_result(A)> result_;
144  util::array<A> accus_;
145  mln_value(L) current_lbl_;
146  mln_value(L) nlabels_;
147  };
148 
149  } // end of namespace mln::labeling::internal
150 
151 
152 
153  // Facade.
154 
155 
156  template <typename I, typename N, typename L, typename A>
157  util::couple<mln_ch_value(I,L),
158  util::couple<util::array<mln_result(A)>,
159  util::array<A> > >
160  blobs_and_compute(const Image<I>& input, const Neighborhood<N>& nbh,
161  L& nlabels, const Accumulator<A>& accu)
162  {
163  trace::entering("labeling::blobs_and_compute");
164 
165  (void) accu;
166  mlc_equal(mln_trait_image_kind(I),
167  mln::trait::image::kind::binary)::check();
168  mln_precondition(exact(input).is_valid());
169 
170  typedef mln_ch_value(I,L) out_t;
171  typedef internal::compute_functor<out_t,A> func_t;
172  func_t functor(nlabels);
173  out_t
174  output = canvas::labeling::blobs(input, nbh, nlabels, functor);
175 
177  result = make::couple(output,
178  make::couple(functor.result_, functor.accus_));
179 
180  trace::exiting("labeling::blobs_and_compute");
181  return result;
182  }
183 
184 
185 # endif // ! MLN_INCLUDE_ONLY
186 
187 
188  } // end of namespace mln::labeling
189 
190 } // end of namespace mln
191 
192 
193 #endif // ! MLN_LABELING_BLOBS_AND_COMPUTE_HH