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