Milena (Olena)  User documentation 2.0a Id
 All Classes Namespaces Functions Variables Typedefs Enumerator Groups Pages
weighted_mean_values.hh
1 // Copyright (C) 2012 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 
26 #ifndef MLN_LABELING_WEIGHTED_MEAN_VALUES_HH
27 # define MLN_LABELING_WEIGHTED_MEAN_VALUES_HH
28 
36 
37 
38 # include <mln/core/concept/image.hh>
39 # include <mln/core/alias/vec3d.hh>
40 
41 # include <mln/accu/stat/mean.hh>
42 
43 # include <mln/data/transform.hh>
44 
45 # include <mln/labeling/compute.hh>
46 
47 # include <mln/literal/colors.hh>
48 
49 
50 namespace mln
51 {
52 
53  // Forward declaration
54  namespace value
55  {
56  template <unsigned n> struct rgb;
57  }
58 
59 
60  namespace labeling
61  {
62 
63  template <typename I, typename L>
64  mln_concrete(I)
65  weighted_mean_values(const Image<I>& input,
66  const Image<L>& lbl, mln_value(L) nlabels);
67 
68 
69 # ifndef MLN_INCLUDE_ONLY
70 
71  namespace internal
72  {
73 
74  template <typename I, typename L>
75  void
76  weighted_mean_values_tests(const Image<I>& input,
77  const Image<L>& lbl, mln_value(L) nlabels)
78  {
79  mln_precondition(exact(input).is_valid());
80  mln_precondition(exact(lbl).is_valid());
81  (void) input;
82  (void) lbl;
83  (void) nlabels;
84  }
85 
86  } // end of namespace mln::labeling::internal
87 
88 
89  namespace impl
90  {
91 
92  namespace generic
93  {
94 
95  template <typename I, typename L>
96  mln_concrete(I)
97  weighted_mean_values(const Image<I>& input_,
98  const Image<L>& lbl_, mln_value(L) nlabels)
99  {
100  trace::entering("mln::labeling::impl::generic::weighted_mean_values");
101 
102  internal::weighted_mean_values_tests(input_, lbl_, nlabels);
103 
104  const I& input = exact(input_);
105  const L& lbl = exact(lbl_);
106  typedef mln_value(L) LV;
107  typedef mln_value(I) IV;
108 
109  image2d<bool> mask = data::convert(bool(), lbl);
110  image2d<value::int_u8>
111  inner_dist = transform::distance_geodesic(mask, c8(), mln_max(L));
112 
113  // Compute weighted mean per label
114  util::array<float> sum(value::succ(nlabels), 0);
115  util::array<float> count(value::succ(nlabels), 0);
116  {
117  mln_piter(I) p(input.domain());
118  for_all(p)
119  {
120  LV id = lbl(p);
121  if (id)
122  {
123  count[id] += inner_dist(p);
124  sum[id] += inner_dist(p) * input(p);
125  }
126  }
127  }
128 
129  // Create output
130  util::array<IV> values(resize(sum_red.size());
131  for (int l = 1; l < sum.size(); ++l)
132  {
133  values[l] = (sum[l] / count[l]);
134  }
135  values[0] = literal::white;
136 
137  image2d<IV> output;
138  initialize(output, input);
139  mln_piter(I) p(output.domain());
140  for_all(p)
141  output(p) = values[lbl(p)];
142 
143  trace::exiting("mln::labeling::impl::generic::weighted_mean_values");
144  return output;
145  }
146 
147  }
148 
149 
150  } // end of namespace mln::morpho::impl
151 
152 
153  namespace internal
154  {
155 
156  // template <unsigned n, typename I, typename L>
157  // mln_concrete(I)
158  // weighted_mean_values_dispatch(const value::rgb<n>&,
159  // const Image<I>& input,
160  // const Image<L>& lbl, mln_value(L) nlabels)
161  // {
162  // return impl::weighted_mean_values_rgb(input, lbl, nlabels);
163  // }
164 
165  template <typename I, typename L>
166  mln_concrete(I)
167  weighted_mean_values_dispatch(const mln_value(I)&,
168  const Image<I>& input,
169  const Image<L>& lbl, mln_value(L) nlabels)
170  {
171  return impl::generic::weighted_mean_values(input, lbl, nlabels);
172  }
173 
174  template <typename I, typename L>
175  mln_concrete(I)
176  weighted_mean_values_dispatch(const Image<I>& input,
177  const Image<L>& lbl, mln_value(L) nlabels)
178  {
179  return weighted_mean_values_dispatch(mln_value(I)(), input, lbl, nlabels);
180  }
181 
182  } // end of namespace mln::morpho::internal
183 
184 
185 
186  // Facade
187 
188  template <typename I, typename L>
189  mln_concrete(I)
190  weighted_mean_values(const Image<I>& input,
191  const Image<L>& lbl, mln_value(L) nlabels)
192 
193  {
194  trace::entering("mln::labeling::weighted_mean_values");
195 
196  internal::weighted_mean_values_tests(input, lbl, nlabels);
197 
198  mln_concrete(I) output = internal::weighted_mean_values_dispatch(input, lbl, nlabels);
199 
200  trace::exiting("mln::labeling::weighted_mean_values");
201  return output;
202  }
203 
204 
205 # endif // !MLN_INCLUDE_ONLY
206 
207  } // end of namespace mln::labeling
208 
209 } // end of namespace mln
210 
211 #endif // ! MLN_LABELING_WEIGHTED_MEAN_VALUES_HH