$extrastylesheet
Olena  User documentation 2.1
An Image Processing Platform
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
mean_values.hh
1 // Copyright (C) 2009, 2013 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_MEAN_VALUES_HH
28 # define MLN_LABELING_MEAN_VALUES_HH
29 
37 
38 
39 # include <mln/core/concept/image.hh>
40 # include <mln/core/alias/vec3d.hh>
41 
42 # include <mln/accu/stat/mean.hh>
43 
44 # include <mln/data/transform.hh>
45 
46 # include <mln/labeling/compute.hh>
47 
48 # include <mln/literal/colors.hh>
49 
50 
51 namespace mln
52 {
53 
54  // Forward declaration
55  namespace value
56  {
57  template <unsigned n> struct rgb;
58  }
59 
60 
61  namespace labeling
62  {
63 
72  template <typename I, typename L>
73  mln_concrete(I)
74  mean_colors(const Image<I>& input,
75  const Image<L>& lbl, mln_value(L) nlabels);
76 
77 
78 # ifndef MLN_INCLUDE_ONLY
79 
80  namespace internal
81  {
82 
83  template <typename I, typename L>
84  void
85  mean_values_tests(const Image<I>& input,
86  const Image<L>& lbl, mln_value(L) nlabels)
87  {
88  mln_precondition(exact(input).is_valid());
89  mln_precondition(exact(lbl).is_valid());
90  (void) input;
91  (void) lbl;
92  (void) nlabels;
93  }
94 
95  } // end of namespace mln::labeling::internal
96 
97 
98  namespace impl
99  {
100 
101  namespace generic
102  {
103 
104  template <typename I, typename L>
105  mln_concrete(I)
106  mean_values(const Image<I>& input_,
107  const Image<L>& lbl_, mln_value(L) nlabels)
108  {
109  mln_trace("mln::labeling::impl::generic::mean_values");
110 
111  internal::mean_values_tests(input_, lbl_, nlabels);
112 
113  const I& input = exact(input_);
114  const L& lbl = exact(lbl_);
115  typedef mln_value(L) LV;
116  typedef mln_value(I) IV;
117 
118  util::array<float> m_3f
119  = labeling::compute(accu::stat::mean<IV>(),
120  input, // input color image
121  lbl, // watershed labeling
122  nlabels);
123  m_3f[0] = 0.f;
124 
125  util::array<IV> m;
126  convert::from_to(m_3f, m);
127  m[0] = 150u; //FIXME: handle label 0 correctly.
128 
129  mln_concrete(I) output = data::transform(lbl, m);
130 
131  return output;
132  }
133 
134  }
135 
136  template <typename I, typename L>
137  mln_concrete(I)
138  mean_values_rgb(const Image<I>& input_,
139  const Image<L>& lbl_, mln_value(L) nlabels)
140  {
141  mln_trace("mln::labeling::impl::mean_values_rgb");
142 
143  internal::mean_values_tests(input_, lbl_, nlabels);
144 
145  const I& input = exact(input_);
146  const L& lbl = exact(lbl_);
147 
148  util::array<vec3d_f> m_3f = labeling::compute(accu::stat::mean<mln_value(I)>(),
149  input, // input color image
150  lbl, // watershed labeling
151  nlabels);
152  m_3f[0] = literal::zero;
153 
154  util::array<mln_value(I)> m;
155  convert::from_to(m_3f, m);
156  m[0] = literal::yellow; //FIXME: handle label 0 correctly.
157 
158  mln_concrete(I) output = data::transform(lbl,
159  convert::to< fun::i2v::array<mln_value(I)> >(m));
160 
161 
162  return output;
163  }
164 
165  } // end of namespace mln::morpho::impl
166 
167 
168  namespace internal
169  {
170 
171  template <unsigned n, typename I, typename L>
172  mln_concrete(I)
173  mean_values_dispatch(const value::rgb<n>&,
174  const Image<I>& input,
175  const Image<L>& lbl, mln_value(L) nlabels)
176  {
177  return impl::mean_values_rgb(input, lbl, nlabels);
178  }
179 
180  template <typename I, typename L>
181  mln_concrete(I)
182  mean_values_dispatch(const mln_value(I)&,
183  const Image<I>& input,
184  const Image<L>& lbl, mln_value(L) nlabels)
185  {
186  return impl::generic::mean_values(input, lbl, nlabels);
187  }
188 
189  template <typename I, typename L>
190  mln_concrete(I)
191  mean_values_dispatch(const Image<I>& input,
192  const Image<L>& lbl, mln_value(L) nlabels)
193  {
194  return mean_values_dispatch(mln_value(I)(), input, lbl, nlabels);
195  }
196 
197  } // end of namespace mln::morpho::internal
198 
199 
200 
201  // Facade
202 
203  template <typename I, typename L>
204  mln_concrete(I)
205  mean_values(const Image<I>& input,
206  const Image<L>& lbl, mln_value(L) nlabels)
207 
208  {
209  mln_trace("mln::labeling::mean_values");
210 
211  internal::mean_values_tests(input, lbl, nlabels);
212 
213  mln_concrete(I) output = internal::mean_values_dispatch(input, lbl, nlabels);
214 
215  return output;
216  }
217 
218 
219 # endif // !MLN_INCLUDE_ONLY
220 
221  } // end of namespace mln::labeling
222 
223 } // end of namespace mln
224 
225 #endif // ! MLN_LABELING_MEAN_VALUES_HH