$extrastylesheet
Olena  User documentation 2.1
An Image Processing Platform
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
labeled_image_base.hh
1 // Copyright (C) 2009, 2010, 2012, 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 MLN_CORE_IMAGE_IMORPH_LABELED_IMAGE_BASE_HH
28 # define MLN_CORE_IMAGE_IMORPH_LABELED_IMAGE_BASE_HH
29 
33 
34 # include <mln/core/image/dmorph/image_if.hh>
35 # include <mln/core/concept/function.hh>
36 # include <mln/core/internal/image_identity.hh>
37 # include <mln/core/site_set/box.hh>
38 
39 # include <mln/accu/shape/bbox.hh>
40 
41 # include <mln/labeling/relabel.hh>
42 
43 # include <mln/util/array.hh>
44 # include <mln/value/next.hh>
45 
46 # include <mln/pw/cst.hh>
47 # include <mln/pw/value.hh>
48 
49 # include <mln/make/relabelfun.hh>
50 
51 
52 namespace mln
53 {
54 
55  // Forward declaration.
56  template <typename I, typename E>
57  class labeled_image_base;
58 
59 
60  namespace internal
61  {
62 
66  template <typename I, typename E>
67  struct data< labeled_image_base<I,E> >
68  {
69  data(const I& ima, const mln_value(I)& nlabels);
70  data(const I& ima, const mln_value(I)& nlabels,
71  const util::array<mln_box(I)>& bboxes);
72 
73  I ima_;
74  mln_value(I) nlabels_;
75  mutable util::array< box<mln_psite(I)> > bboxes_;
76  };
77 
78  } // end of namespace mln::internal
79 
80 
81 
82  namespace trait
83  {
84 
85  template <typename I, typename E>
86  struct image_< labeled_image_base<I,E> > : image_< I > // Same as I except...
87  {
88  // ...these changes.
89  typedef trait::image::category::identity_morpher category;
90  typedef mln_internal_trait_image_speed_from(I) speed; // Un-fastest.
91  typedef trait::image::value_access::indirect value_access;
92 
93  typedef trait::image::value_io::read_only value_io;
94  typedef trait::image::pw_io::read pw_io;
95 
96  // extended domain
97  typedef trait::image::ext_value::multiple ext_value;
98  typedef trait::image::ext_io::read_only ext_io;
99  };
100 
101  } // end of namespace mln::trait
102 
103 
104 
117  template <typename I, typename E>
119  : public internal::image_identity< const I, mln_domain(I), E >
120  {
122  super_;
123 
124  public:
126  typedef mln_result(accu::shape::bbox<mln_psite(I)>) bbox_t;
127 
130 
133 
135 
141 
142  // FIXME: currently the label is kept contiguous for
143  // performance reasons. Do we want to be less restrictive?
144  template <typename F>
145  void relabel(const Function_v2v<F>& f);
146 
149  template <typename F>
150  void relabel(const Function_v2b<F>& f);
151 
153 
155  mln_value(I) nlabels() const;
156 
158  const bbox_t& bbox(const mln_value(I)& label) const;
159 
161  const util::array<bbox_t>& bboxes() const;
162 
164  p_if<mln_box(I),
165  fun::eq_v2b_expr_<pw::value_<I>, pw::cst_<mln_value(I)> > >
166  subdomain(const mln_value(I)& label) const;
167 
168  protected:
170  void update_data(const fun::i2v::array<mln_value(I)>& relabel_fun);
171 
172  template <typename F>
173  void relabel_(const Function_v2v<F>& f);
174 
175  template <typename F>
176  void relabel_(const Function_v2b<F>& f);
177 
180 
181  void init_update_data_();
182  void prepare_update_data_(const mln_value(I)& lbl,
183  const mln_value(I)& new_lbl);
184  void update_data_(const fun::i2v::array<mln_value(I)>& relabel_fun);
185 
187  };
188 
189 
190 
191 
192 # ifndef MLN_INCLUDE_ONLY
193 
194 
195  // internal::data< labeled_image_base<I,E> >
196 
197  namespace internal
198  {
199 
200 
201  // data< labeled_image_base<I,E> >
202 
203  template <typename I, typename E>
204  inline
205  data< labeled_image_base<I,E> >::data(const I& ima, const mln_value(I)& nlabels)
206  : ima_(ima), nlabels_(nlabels)
207  {
208  }
209 
210  template <typename I, typename E>
211  inline
212  data< labeled_image_base<I,E> >::data(const I& ima, const mln_value(I)& nlabels,
213  const util::array<mln_box(I)>& bboxes)
214  : ima_(ima), nlabels_(nlabels), bboxes_(bboxes)
215  {
216  }
217 
218 
219  } // end of namespace mln::internal
220 
221 
222  template <typename I, typename E>
223  inline
225  {
226  }
227 
228 
229  template <typename I, typename E>
230  template <typename F>
231  inline
232  void
233  labeled_image_base<I,E>::relabel(const Function_v2v<F>& f_)
234  {
235  const F& f = exact(f_);
236  mln_value(I) new_nlabels;
237 
238  fun::i2v::array<mln_value(I)>
239  packed_relabel_fun = make::relabelfun(f,
240  this->data_->nlabels_,
241  new_nlabels);
242 
243  labeling::relabel_inplace(this->data_->ima_, this->data_->nlabels_,
244  packed_relabel_fun);
245 
246  this->data_->nlabels_ = new_nlabels;
247 
248  exact(this)->relabel_(f);
249 
251  update_data(packed_relabel_fun);
252  }
253 
254 
255  template <typename I, typename E>
256  template <typename F>
257  inline
258  void
259  labeled_image_base<I,E>::relabel(const Function_v2b<F>& f_)
260  {
261  const F& f = exact(f_);
262 
263  // Relabel the underlying image.
264  typedef fun::i2v::array<mln_value(I)> fv2v_t;
265  fv2v_t fv2v = make::relabelfun(f,
266  this->data_->nlabels_,
267  this->data_->nlabels_);
268 
269  labeling::relabel_inplace(this->data_->ima_, this->data_->nlabels_, fv2v);
270 
271  exact(this)->relabel_(f);
272 
273  // Then, merge or delete bounding boxes according to this relabeling.
274  update_data(fv2v);
275  }
276 
277  template <typename I, typename E>
278  inline
279  mln_value(I)
280  labeled_image_base<I,E>::nlabels() const
281  {
282  return this->data_->nlabels_;
283  }
284 
285 
286  template <typename I, typename E>
287  void
288  labeled_image_base<I,E>::update_data(const fun::i2v::array<mln_value(I)>& relabel_fun)
289  {
290  util::array<accu::shape::bbox<mln_psite(I)> >
291  new_bboxes(mln::value::next(this->data_->nlabels_));
292 
293  exact(this)->init_update_data_();
294 
295  for (unsigned i = 1; i < this->data_->bboxes_.size(); ++i)
296  if (relabel_fun(i) != 0)
297  {
298  new_bboxes[relabel_fun(i)].take(this->data_->bboxes_[i]);
299  exact(this)->prepare_update_data_(i, relabel_fun(i));
300  }
301 
302  convert::from_to(new_bboxes, this->data_->bboxes_);
303 
304  mln_assertion(new_bboxes.size() == this->data_->bboxes_.size());
305 
306  exact(this)->update_data_(relabel_fun);
307  }
308 
309 
310  template <typename I, typename E>
311  const typename labeled_image_base<I,E>::bbox_t&
312  labeled_image_base<I,E>::bbox(const mln_value(I)& label) const
313  {
314  return this->data_->bboxes_[label];
315  }
316 
317 
318  template <typename I, typename E>
319  const util::array<typename labeled_image_base<I,E>::bbox_t>&
320  labeled_image_base<I,E>::bboxes() const
321  {
322  return this->data_->bboxes_;
323  }
324 
325 
326  template <typename I, typename E>
327  p_if<mln_box(I),
328  fun::eq_v2b_expr_<pw::value_<I>, pw::cst_<mln_value(I)> > >
329  labeled_image_base<I,E>::subdomain(const mln_value(I)& label) const
330  {
331  return ((this->data_->ima_ | bbox(label))
332  | (pw::value(this->data_->ima_) == pw::cst(label))).domain();
333  }
334 
335 
336 
337  template <typename I, typename E>
338  template <typename F>
339  void
340  labeled_image_base<I,E>::relabel_(const Function_v2v<F>& f)
341  {
342  (void) f;
343  // No-Op.
344  }
345 
346  template <typename I, typename E>
347  template <typename F>
348  void
349  labeled_image_base<I,E>::relabel_(const Function_v2b<F>& f)
350  {
351  (void) f;
352  // No-Op.
353  }
354 
355  template <typename I, typename E>
356  void
357  labeled_image_base<I,E>::update_data_(
358  const fun::i2v::array<mln_value(I)>& relabel_fun)
359  {
360  (void) relabel_fun;
361  // No-Op.
362  }
363 
364  template <typename I, typename E>
365  void
366  labeled_image_base<I,E>::prepare_update_data_(const mln_value(I)& lbl,
367  const mln_value(I)& new_lbl)
368  {
369  (void) lbl;
370  (void) new_lbl;
371  // No-Op.
372  }
373 
374  template <typename I, typename E>
375  void
376  labeled_image_base<I,E>::init_update_data_()
377  {
378  // No-Op.
379  }
380 
381 
382 # endif // ! MLN_INCLUDE_ONLY
383 
384 } // end of namespace mln
385 
386 
387 #endif // ! MLN_CORE_IMAGE_IMORPH_LABELED_IMAGE_BASE_HH