26 #ifndef MLN_LABELING_WEIGHTED_MEAN_VALUES_HH
27 # define MLN_LABELING_WEIGHTED_MEAN_VALUES_HH
38 # include <mln/core/concept/image.hh>
39 # include <mln/core/alias/vec3d.hh>
41 # include <mln/accu/stat/mean.hh>
43 # include <mln/data/transform.hh>
45 # include <mln/labeling/compute.hh>
47 # include <mln/literal/colors.hh>
56 template <
unsigned n>
struct rgb;
63 template <
typename I,
typename L>
65 weighted_mean_values(const Image<I>& input,
66 const Image<L>& lbl, mln_value(L) nlabels);
69 # ifndef MLN_INCLUDE_ONLY
74 template <
typename I,
typename L>
76 weighted_mean_values_tests(
const Image<I>& input,
77 const Image<L>& lbl, mln_value(L) nlabels)
79 mln_precondition(exact(input).is_valid());
80 mln_precondition(exact(lbl).is_valid());
95 template <
typename I,
typename L>
97 weighted_mean_values(const Image<I>& input_,
98 const Image<L>& lbl_, mln_value(L) nlabels)
100 trace::entering(
"mln::labeling::impl::generic::weighted_mean_values");
102 internal::weighted_mean_values_tests(input_, lbl_, nlabels);
104 const I& input = exact(input_);
105 const L& lbl = exact(lbl_);
106 typedef mln_value(L) LV;
107 typedef mln_value(I) IV;
109 image2d<
bool> mask =
data::convert(
bool(), lbl);
110 image2d<
value::int_u8>
111 inner_dist = transform::distance_geodesic(mask,
c8(), mln_max(L));
114 util::array<
float> sum(
value::succ(nlabels), 0);
115 util::array<
float> count(
value::succ(nlabels), 0);
117 mln_piter(I) p(input.domain());
123 count[id] += inner_dist(p);
124 sum[id] += inner_dist(p) * input(p);
130 util::array<IV> values(
resize(sum_red.size());
131 for (
int l = 1; l <
sum.size(); ++l)
133 values[l] = (
sum[l] / count[l]);
139 mln_piter(I) p(output.domain());
141 output(p) = values[lbl(p)];
143 trace::exiting("mln::labeling::impl::generic::weighted_mean_values");
165 template <
typename I,
typename L>
167 weighted_mean_values_dispatch(const mln_value(I)&,
168 const Image<I>& input,
169 const Image<L>& lbl, mln_value(L) nlabels)
171 return impl::generic::weighted_mean_values(input, lbl, nlabels);
174 template <
typename I,
typename L>
176 weighted_mean_values_dispatch(const Image<I>& input,
177 const Image<L>& lbl, mln_value(L) nlabels)
179 return weighted_mean_values_dispatch(mln_value(I)(), input, lbl, nlabels);
188 template <
typename I,
typename L>
190 weighted_mean_values(const Image<I>& input,
191 const Image<L>& lbl, mln_value(L) nlabels)
194 trace::entering(
"mln::labeling::weighted_mean_values");
196 internal::weighted_mean_values_tests(input, lbl, nlabels);
198 mln_concrete(I) output = internal::weighted_mean_values_dispatch(input, lbl, nlabels);
200 trace::exiting("mln::labeling::weighted_mean_values");
205 # endif // !MLN_INCLUDE_ONLY
211 #endif // ! MLN_LABELING_WEIGHTED_MEAN_VALUES_HH