$extrastylesheet
Olena  User documentation 2.1
An Image Processing Platform
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
components.hh
1 // Copyright (C) 2010 EPITA Research and Development Laboratory (LRDE)
2 //
3 // This file is part of Olena.
4 //
5 // Olena is free software: you can redistribute it and/or modify it under
6 // the terms of the GNU General Public License as published by the Free
7 // Software Foundation, version 2 of the License.
8 //
9 // Olena is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 // General Public License for more details.
13 //
14 // You should have received a copy of the GNU General Public License
15 // along with Olena. If not, see <http://www.gnu.org/licenses/>.
16 //
17 // As a special exception, you may use this file as part of a free
18 // software project without restriction. Specifically, if other files
19 // instantiate templates or use macros or inline functions from this
20 // file, or you compile this file and link it with other files to produce
21 // an executable, this file does not by itself cause the resulting
22 // executable to be covered by the GNU General Public License. This
23 // exception does not however invalidate any other reasons why the
24 // executable file might be covered by the GNU General Public License.
25 
29 
30 #ifndef SCRIBO_PRIMITIVE_MERGE_COMPONENTS_HH
31 # define SCRIBO_PRIMITIVE_MERGE_COMPONENTS_HH
32 
33 # include <mln/core/concept/function.hh>
34 # include <mln/core/image/vmorph/fun_image.hh>
35 # include <mln/data/fill.hh>
36 # include <mln/pw/all.hh>
37 
38 # include <scribo/core/component_set.hh>
39 
40 
41 namespace scribo
42 {
43 
44  namespace primitive
45  {
46 
47  namespace merge
48  {
49 
50  using namespace mln;
51 
52 
53 # ifndef MLN_INCLUDE_ONLY
54 
55  namespace internal
56  {
57 
58  template <typename V>
59  struct rebase_label : mln::Function_v2v<rebase_label<V> >
60  {
61  typedef V result;
62 
63  rebase_label()
64  : base_label_(0)
65  {}
66 
67  rebase_label(const V& base_label)
68  : base_label_(base_label)
69  {}
70 
71  V operator()(const V& v) const
72  {
73  if (v)
74  return unsigned(base_label_) + unsigned(v);
75  return literal::zero;
76  }
77 
78 
79  V base_label_;
80  };
81 
82 
83  } // end of namespace scribo::primitive::merge
84 
85 
86  template <typename L>
87  component_set<L>
88  components(const component_set<L>& lhs, const component_set<L>& rhs)
89  {
90  mln_trace("scribo::primitive::merge::components");
91 
92  const L& lhs_lbl = lhs.labeled_image();
93  const L& rhs_lbl = rhs.labeled_image();
94 
95  typedef mln_value(L) V;
96 
97 
98  V nlabels = unsigned(lhs.nelements()) + unsigned(rhs.nelements());
99 
100  // Merge labeled images.
101  //
102  // Rebase labeling in the second image, according to the
103  // number of labels in the first one.
104  //
105  L lbl_merge = duplicate(lhs_lbl);
106 
107  internal::rebase_label<V> f(lhs.nelements());
108  fun_image<internal::rebase_label<V>, L> fima(f, rhs_lbl);
109  data::fill((lbl_merge | (pw::value(lbl_merge) == pw::cst(0))).rw(), fima);
110 
111  component_set<L> output(lbl_merge, nlabels);
112 
113  return output;
114  }
115 
116 # endif // ! MLN_INCLUDE_ONLY
117 
118  } // end of namespace scribo::primitive::merge
119 
120  } // end of namespace scribo::primitive
121 
122 } // end of namespace scribo
123 
124 
125 #endif // ! SCRIBO_PRIMITIVE_MERGE_COMPONENTS_HH