Milena (Olena)  User documentation 2.0a Id
 All Classes Namespaces Functions Variables Typedefs Enumerator Groups Pages
mean_values.hh
1 // Copyright (C) 2009 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_MEAN_VALUES_HH
27 # define MLN_LABELING_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  mean_colors(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  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  mean_values(const Image<I>& input_,
98  const Image<L>& lbl_, mln_value(L) nlabels)
99  {
100  trace::entering("mln::labeling::impl::generic::mean_values");
101 
102  internal::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  util::array<float> m_3f
110  = labeling::compute(accu::stat::mean<IV>(),
111  input, // input color image
112  lbl, // watershed labeling
113  nlabels);
114  m_3f[0] = 0.f;
115 
116  util::array<IV> m;
117  convert::from_to(m_3f, m);
118  m[0] = 150u; //FIXME: handle label 0 correctly.
119 
120  mln_concrete(I) output = data::transform(lbl, m);
121 
122  trace::exiting("mln::labeling::impl::generic::mean_values");
123  return output;
124  }
125 
126  }
127 
128  template <typename I, typename L>
129  mln_concrete(I)
130  mean_values_rgb(const Image<I>& input_,
131  const Image<L>& lbl_, mln_value(L) nlabels)
132  {
133  trace::entering("mln::labeling::impl::mean_values_rgb");
134 
135  internal::mean_values_tests(input_, lbl_, nlabels);
136 
137  const I& input = exact(input_);
138  const L& lbl = exact(lbl_);
139 
140  util::array<vec3d_f> m_3f = labeling::compute(accu::stat::mean<mln_value(I)>(),
141  input, // input color image
142  lbl, // watershed labeling
143  nlabels);
144  m_3f[0] = literal::zero;
145 
146  util::array<mln_value(I)> m;
147  convert::from_to(m_3f, m);
148  m[0] = literal::yellow; //FIXME: handle label 0 correctly.
149 
150  mln_concrete(I) output = data::transform(lbl,
151  convert::to< fun::i2v::array<mln_value(I)> >(m));
152 
153 
154  trace::exiting("mln::labeling::impl::mean_values_rgb");
155  return output;
156  }
157 
158  } // end of namespace mln::morpho::impl
159 
160 
161  namespace internal
162  {
163 
164  template <unsigned n, typename I, typename L>
165  mln_concrete(I)
166  mean_values_dispatch(const value::rgb<n>&,
167  const Image<I>& input,
168  const Image<L>& lbl, mln_value(L) nlabels)
169  {
170  return impl::mean_values_rgb(input, lbl, nlabels);
171  }
172 
173  template <typename I, typename L>
174  mln_concrete(I)
175  mean_values_dispatch(const mln_value(I)&,
176  const Image<I>& input,
177  const Image<L>& lbl, mln_value(L) nlabels)
178  {
179  return impl::generic::mean_values(input, lbl, nlabels);
180  }
181 
182  template <typename I, typename L>
183  mln_concrete(I)
184  mean_values_dispatch(const Image<I>& input,
185  const Image<L>& lbl, mln_value(L) nlabels)
186  {
187  return mean_values_dispatch(mln_value(I)(), input, lbl, nlabels);
188  }
189 
190  } // end of namespace mln::morpho::internal
191 
192 
193 
194  // Facade
195 
196  template <typename I, typename L>
197  mln_concrete(I)
198  mean_values(const Image<I>& input,
199  const Image<L>& lbl, mln_value(L) nlabels)
200 
201  {
202  trace::entering("mln::labeling::mean_values");
203 
204  internal::mean_values_tests(input, lbl, nlabels);
205 
206  mln_concrete(I) output = internal::mean_values_dispatch(input, lbl, nlabels);
207 
208  trace::exiting("mln::labeling::mean_values");
209  return output;
210  }
211 
212 
213 # endif // !MLN_INCLUDE_ONLY
214 
215  } // end of namespace mln::labeling
216 
217 } // end of namespace mln
218 
219 #endif // ! MLN_LABELING_MEAN_VALUES_HH