
--- milena/ChangeLog | 7 ++ milena/mln/labeling/compute.hh | 207 +++++++++++++++++++++++++++++++++++++ milena/tests/labeling/Makefile.am | 2 + milena/tests/labeling/compute.cc | 83 +++++++++++++++ 4 files changed, 299 insertions(+), 0 deletions(-) create mode 100644 milena/mln/labeling/compute.hh create mode 100644 milena/tests/labeling/compute.cc diff --git a/milena/ChangeLog b/milena/ChangeLog index 734d001..9ad63cc 100644 --- a/milena/ChangeLog +++ b/milena/ChangeLog @@ -1,5 +1,12 @@ 2008-10-14 Guillaume Lazzara <z@lrde.epita.fr> + Add labeling::compute. + * milena/mln/labeling/compute.hh: do it. + * milena/tests/labeling/Makefile.am, + * milena/tests/labeling/compute.cc: Add the proper test. + +2008-10-14 Guillaume Lazzara <z@lrde.epita.fr> + Use geom::bbox instead of bbox(). * milena/mln/geom/max_col.hh, * milena/mln/geom/max_row.hh, diff --git a/milena/mln/labeling/compute.hh b/milena/mln/labeling/compute.hh new file mode 100644 index 0000000..d579c7e --- /dev/null +++ b/milena/mln/labeling/compute.hh @@ -0,0 +1,207 @@ +// Copyright (C) 2008 EPITA Research and Development Laboratory +// +// This file is part of the Olena Library. This library is free +// software; you can redistribute it and/or modify it under the terms +// of the GNU General Public License version 2 as published by the +// Free Software Foundation. +// +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this library; see the file COPYING. If not, write to +// the Free Software Foundation, 51 Franklin Street, Fifth Floor, +// Boston, MA 02111-1307, USA. +// +// As a special exception, you may use this file as part of a free +// software library without restriction. Specifically, if other files +// instantiate templates or use macros or inline functions from this +// file, or you compile this file and link it with other files to +// produce an executable, this file does not by itself cause the +// resulting executable to be covered by the GNU General Public +// License. This exception does not however invalidate any other +// reasons why the executable file might be covered by the GNU General +// Public License. + +#ifndef MLN_LABELING_COMPUTE_HH +# define MLN_LABELING_COMPUTE_HH + +/*! \file mln/labeling/compute.hh + * + * \brief Compute an accumulator onto image pixel values. + */ + +# include <mln/core/concept/meta_accumulator.hh> +# include <mln/util/array.hh> + + + +namespace mln +{ + + namespace labeling + { + + /*! Compute an accumulator onto the pixel values of the image \p input. + * for each component of the image \p label. + * + * \param[in] a An accumulator. + * \param[in] input The input image. + * \param[in] label The labeled image. + * \param[in] input The number of component in \p label. + * \return A mln::p_array of accumulator result. One result per component in + * \p label. + * + * It fully relies on labeling::update. + */ + template <typename A, typename I, typename J> + p_array<mln_result(A)> + compute(const Accumulator<A>& a, + const Image<I>& input, + const Image<J>& label, mln_value(J) nlabels); + + /*! Compute an accumulator onto the pixel values of the image \p input. + * for each component of the image \p label. + * + * \param[in] a A meta-accumulator. + * \param[in] input The input image. + * \param[in] label The labeled image. + * \param[in] input The number of component in \p label. + * \return A mln::p_array of accumulator result. One result per component in + * \p label. + * + * It fully relies on labeling::update. + */ + template <typename A, typename I, typename J> + p_array<mln_accu_with(A, mln_value(I))::result> + compute(const Meta_Accumulator<A>& a, + const Image<I>& input, + const Image<J>& label, mln_value(J) nlabels); + + /*! Compute an accumulator onto the sites of each component domain of + * \p label. + * + * \param[in] a An accumulator. + * \param[in] label The labeled image. + * \param[in] input The number of component in \p label. + * \return A mln::p_array of accumulator result. One result per component in + * \p label. + * + * It fully relies on labeling::update. + */ + template <typename A, typename J> + p_array<mln_result(A)> + compute(const Accumulator<A>& a, + const Image<J>& label, mln_value(J) nlabels); + + /*! Compute an accumulator onto the sites of each component domain of + * \p label. + * + * \param[in] a A meta-accumulator. + * \param[in] label The labeled image. + * \param[in] input The number of component in \p label. + * \return A mln::p_array of accumulator result. One result per component in + * \p label. + * + * It fully relies on labeling::update. + */ + template <typename A, typename J> + p_array<mln_accu_with(A, mln_psite(J))::result> + compute(const Meta_Accumulator<A>& a, + const Image<J>& label, mln_value(J) nlabels); + + + +# ifndef MLN_INCLUDE_ONLY + + + // Facades. + + template <typename A, typename I, typename J> + inline + p_array<mln_result(A)> + compute(const Accumulator<A>&, + const Image<I>& input_, + const Image<J>& label_, mln_value(J) nlabels) + { + trace::entering("labeling::compute"); + + const I& input = exact(input_); + const J& label = exact(label_); + + util::array<A> accus; + for (mln_value(J) i = 0; i <= nlabels; ++i) + accus.append(A()); + + mln_piter(I) p(input.domain()); + for_all(p) + accus[label(p)].take(input(p)); + + p_array<mln_result(A)> results; + for (unsigned i = 0; i < accus.nelements(); ++i) + results.append(accus[i]); + + trace::exiting("labeling::compute"); + return results; + } + + template <typename A, typename I, typename J> + inline + p_array<mln_accu_with(A, mln_value(I))::result> + compute(const Meta_Accumulator<A>&, + const Image<I>& input, + const Image<J>& label, mln_value(J) nlabels) + { + mln_accu_with(A, mln_value(I)) accu; + return compute(accu, input, label, nlabels); + } + + + template <typename A, typename J> + inline + p_array<mln_result(A)> + compute(const Accumulator<A>& a, + const Image<J>& label_, mln_value(J) nlabels) + { + trace::entering("labeling::compute"); + + const J& label = exact(label_); + + util::array<A> accus; + for (mln_value(J) i = 0; i <= nlabels; ++i) + accus.append(exact(a)); + + mln_piter(J) p(label.domain()); + for_all(p) + accus[label(p)].take(p); + + p_array<mln_result(A)> results; + for (unsigned i = 0; i < accus.nelements(); ++i) + results.append(accus[i]); + + trace::exiting("labeling::compute"); + return results; + } + + + template <typename A, typename J> + inline + p_array<mln_accu_with(A, mln_psite(J))::result> + compute(const Meta_Accumulator<A>&, + const Image<J>& label, mln_value(J) nlabels) + { + mln_accu_with(A, mln_psite(J)) accu; + return compute(accu, label, nlabels); + } + + +# endif // ! MLN_INCLUDE_ONLY + + } // end of namespace mln::labeling + +} // end of namespace mln + + +#endif // ! MLN_LABELING_COMPUTE_HH diff --git a/milena/tests/labeling/Makefile.am b/milena/tests/labeling/Makefile.am index 212eb15..399a1eb 100644 --- a/milena/tests/labeling/Makefile.am +++ b/milena/tests/labeling/Makefile.am @@ -5,6 +5,7 @@ include $(top_srcdir)/milena/tests/tests.mk check_PROGRAMS = \ background \ blobs \ + compute \ flat_zones \ foreground \ level \ @@ -13,6 +14,7 @@ check_PROGRAMS = \ background_SOURCES = background.cc blobs_SOURCES = blobs.cc +compute_SOURCES = compute.cc flat_zones_SOURCES = flat_zones.cc foreground_SOURCES = foreground.cc level_SOURCES = level.cc diff --git a/milena/tests/labeling/compute.cc b/milena/tests/labeling/compute.cc new file mode 100644 index 0000000..cd36ae3 --- /dev/null +++ b/milena/tests/labeling/compute.cc @@ -0,0 +1,83 @@ +// Copyright (C) 2008 EPITA Research and Development Laboratory +// +// This file is part of the Olena Library. This library is free +// software; you can redistribute it and/or modify it under the terms +// of the GNU General Public License version 2 as published by the +// Free Software Foundation. +// +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this library; see the file COPYING. If not, write to +// the Free Software Foundation, 51 Franklin Street, Fifth Floor, +// Boston, MA 02111-1307, USA. +// +// As a special exception, you may use this file as part of a free +// software library without restriction. Specifically, if other files +// instantiate templates or use macros or inline functions from this +// file, or you compile this file and link it with other files to +// produce an executable, this file does not by itself cause the +// resulting executable to be covered by the GNU General Public +// License. This exception does not however invalidate any other +// reasons why the executable file might be covered by the GNU General +// Public License. + +/*! \file tests/labeling/compute.cc + * + * \brief Tests on mln::labeling::compute. + */ + +#include <mln/core/image/image2d.hh> +#include <mln/core/alias/neighb2d.hh> +#include <mln/labeling/blobs.hh> +#include <mln/labeling/compute.hh> +#include <mln/accu/count.hh> +#include <mln/accu/sum.hh> +#include <mln/value/int_u8.hh> + +int main() +{ + using namespace mln; + using value::int_u8; + + int_u8 vals[6][5] = { + {0, 1, 1, 0, 0}, + {0, 1, 1, 0, 0}, + {0, 0, 0, 0, 0}, + {2, 2, 0, 3, 0}, + {2, 0, 3, 3, 3}, + {2, 0, 0, 0, 0} + }; + image2d<int_u8> ima = make::image2d(vals); + int_u8 nlabels = 3; + + accu::sum_<int_u8> sum; + p_array<float> sums = labeling::compute(sum, ima, ima, nlabels); + mln_assertion(sums[0] == 0); + mln_assertion(sums[1] == 4); + mln_assertion(sums[2] == 8); + mln_assertion(sums[3] == 12); + + sums = labeling::compute(accu::meta::sum(), ima, ima, nlabels); + mln_assertion(sums[0] == 0); + mln_assertion(sums[1] == 4); + mln_assertion(sums[2] == 8); + mln_assertion(sums[3] == 12); + + accu::count_<mln_site_(image2d<int_u8>)> count; + p_array<unsigned int> counts = labeling::compute(count, ima, nlabels); + mln_assertion(counts[0] == 18); + mln_assertion(counts[1] == 4); + mln_assertion(counts[2] == 4); + mln_assertion(counts[3] == 4); + + counts = labeling::compute(accu::meta::count(), ima, nlabels); + mln_assertion(counts[0] == 18); + mln_assertion(counts[1] == 4); + mln_assertion(counts[2] == 4); + mln_assertion(counts[3] == 4); +} + -- 1.5.6.5