
* filter/internal/compute.hh: New routine to compute filter results. * filter/object_links_center_aligned.hh, * filter/object_links_top_aligned.hh, * filter/objects_h_thick.hh, * filter/objects_v_thick.hh: New filters. --- scribo/ChangeLog | 12 ++ scribo/filter/internal/compute.hh | 147 +++++++++++++++++++ scribo/filter/object_links_center_aligned.hh | 113 +++++++++++++++ scribo/filter/object_links_top_aligned.hh | 121 ++++++++++++++++ scribo/filter/objects_h_thick.hh | 196 ++++++++++++++++++++++++++ scribo/filter/objects_v_thick.hh | 196 ++++++++++++++++++++++++++ 6 files changed, 785 insertions(+), 0 deletions(-) create mode 100644 scribo/filter/internal/compute.hh create mode 100644 scribo/filter/object_links_center_aligned.hh create mode 100644 scribo/filter/object_links_top_aligned.hh create mode 100644 scribo/filter/objects_h_thick.hh create mode 100644 scribo/filter/objects_v_thick.hh diff --git a/scribo/ChangeLog b/scribo/ChangeLog index bb22ef9..2a3c71e 100644 --- a/scribo/ChangeLog +++ b/scribo/ChangeLog @@ -1,5 +1,17 @@ 2009-11-03 Guillaume Lazzara <z@lrde.epita.fr> + Add new link filters. + + * filter/internal/compute.hh: New routine to compute filter + results. + + * filter/object_links_center_aligned.hh, + * filter/object_links_top_aligned.hh, + * filter/objects_h_thick.hh, + * filter/objects_v_thick.hh: New filters. + +2009-11-03 Guillaume Lazzara <z@lrde.epita.fr> + Revamp code related to object linking. * core/concept/link_functor.hh: New concept. diff --git a/scribo/filter/internal/compute.hh b/scribo/filter/internal/compute.hh new file mode 100644 index 0000000..346cee8 --- /dev/null +++ b/scribo/filter/internal/compute.hh @@ -0,0 +1,147 @@ +// Copyright (C) 2009 EPITA Research and Development Laboratory (LRDE) +// +// This file is part of Olena. +// +// Olena is free software: you can redistribute it and/or modify it under +// the terms of the GNU General Public License as published by the Free +// Software Foundation, version 2 of the License. +// +// Olena 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 Olena. If not, see <http://www.gnu.org/licenses/>. +// +// As a special exception, you may use this file as part of a free +// software project 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 SCRIBO_FILTER_INTERNAL_COMPUTE_HH +# define SCRIBO_FILTER_INTERNAL_COMPUTE_HH + +/// \file +/// +/// Compute filters. + +# include <mln/core/concept/image.hh> +# include <mln/core/concept/neighborhood.hh> + +# include <mln/util/array.hh> + +# include <scribo/core/object_image.hh> +# include <scribo/primitive/extract/objects.hh> + + + +namespace scribo +{ + + namespace filter + { + + namespace internal + { + + + using namespace mln; + + /// Apply a filter to an image. + /// + /// \param[in] input_ A binary image. + /// \param[in] nbh_ A neighborhood used in labeling algorithms. + /// \param[in] label_type The label type used for labeling. + /// \param[in] filter A filter. + /// + /// \result A filtered binary image. + // + template <typename I, typename N, typename V, typename F> + mln_concrete(I) + compute(const Image<I>& input_, + const Neighborhood<N>& nbh_, + const V& label_type, + const Function_v2b<F>& filter); + + + /// Filter an object image. + /// + /// \param[in] objects An object image. + /// \param[in] filter A filter functor. + /// + /// \result A filtered object image. + // + template <typename L, typename F> + object_image(L) + compute(const object_image(L)& objects, + const Function_v2b<F>& filter); + + +# ifndef MLN_INCLUDE_ONLY + + + template <typename I, typename N, typename V, typename F> + inline + mln_concrete(I) + compute(const Image<I>& input_, + const Neighborhood<N>& nbh_, + const V& label_type, + const Function_v2b<F>& filter) + { + trace::entering("scribo::filter::internal::compute"); + + const I& input = exact(input_); + const N& nbh = exact(nbh_); + + mln_precondition(input.is_valid()); + mln_precondition(nbh.is_valid()); + + V nlabels; + typedef mln_ch_value(I,V) lbl_t; + object_image(lbl_t) objects + = primitive::extract::objects(input, nbh, nlabels); + + filter.update_objects(objects); + objects.relabel(filter); + + mln_concrete(I) output = duplicate(input); + data::fill((output | pw::value(objects) == literal::zero).rw(), false); + + trace::exiting("scribo::filter::internal::compute"); + return output; + } + + + template <typename L, typename F> + inline + object_image(L) + compute(const object_image(L)& objects, + const Function_v2b<F>& filter) + { + trace::entering("scribo::filter::internal::compute"); + + mln_precondition(objects.is_valid()); + + object_image(L) output; + output.init_from_(objects); + output.relabel(filter); + + trace::exiting("scribo::filter::internal::compute"); + return output; + } + +# endif // ! MLN_INCLUDE_ONLY + + } // end of namespace scribo::filter::internal + + } // end of namespace scribo::filter + +} // end of namespace scribo + + +#endif // ! SCRIBO_FILTER_INTERNAL_COMPUTE_HH diff --git a/scribo/filter/object_links_center_aligned.hh b/scribo/filter/object_links_center_aligned.hh new file mode 100644 index 0000000..e44f3cd --- /dev/null +++ b/scribo/filter/object_links_center_aligned.hh @@ -0,0 +1,113 @@ +// Copyright (C) 2009 EPITA Research and Development Laboratory (LRDE) +// +// This file is part of Olena. +// +// Olena is free software: you can redistribute it and/or modify it under +// the terms of the GNU General Public License as published by the Free +// Software Foundation, version 2 of the License. +// +// Olena 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 Olena. If not, see <http://www.gnu.org/licenses/>. +// +// As a special exception, you may use this file as part of a free +// software project 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 SCRIBO_FILTER_OBJECT_LINKS_CENTER_ALIGNED_HH +# define SCRIBO_FILTER_OBJECT_LINKS_CENTER_ALIGNED_HH + +/// \file +/// +/// Invalidate links between two objects if their center are not +/// aligned. + + +# include <mln/util/array.hh> + +# include <scribo/core/macros.hh> +# include <scribo/core/object_links.hh> +# include <scribo/core/object_image.hh> + +# include <scribo/filter/object_links_non_aligned_simple.hh> + +namespace scribo +{ + + namespace filter + { + + using namespace mln; + + /*! \brief Invalidate links between two objects if their center are not + aligned. + + \param[in] objects An object image. + \param[in] links Object links information. + \param[in] max_alpha Maximum angle value (degrees). + + \verbatim + + ------ ------ + | | | | v + | x ~| ~ ~ ~ | ~ ~|~ ~ + | ~| ~ | | | => Alpha + | | ~ ~ | x ~|~ ~ + ------ | | ^ + object1 | | + ------ + object2 + + \endverbatim + + The angle between the two bottoms must be lower than \p max_alpha. + + */ + template <typename L> + object_links<L> + object_links_center_aligned(const object_image(L)& objects, + const object_links<L>& links, + float max_alpha); + + +# ifndef MLN_INCLUDE_ONLY + + + template <typename L> + object_links<L> + object_links_center_aligned(const object_image(L)& objects, + const object_links<L>& links, + float max_alpha) + { + trace::entering("scribo::filter::object_links_center_aligned"); + + mln_precondition(objects.is_valid()); + mln_precondition(links.is_valid()); + + object_links<L> + output = object_links_non_aligned_simple(objects, links, + 2, max_alpha); + + trace::exiting("scribo::filter::object_links_center_aligned"); + return output; + } + + +# endif // ! MLN_INCLUDE_ONLY + + + } // end of namespace scribo::filter + +} // end of namespace scribo + + +#endif // ! SCRIBO_FILTER_OBJECT_LINKS_CENTER_ALIGNED_HH diff --git a/scribo/filter/object_links_top_aligned.hh b/scribo/filter/object_links_top_aligned.hh new file mode 100644 index 0000000..47e26c8 --- /dev/null +++ b/scribo/filter/object_links_top_aligned.hh @@ -0,0 +1,121 @@ +// Copyright (C) 2009 EPITA Research and Development Laboratory (LRDE) +// +// This file is part of Olena. +// +// Olena is free software: you can redistribute it and/or modify it under +// the terms of the GNU General Public License as published by the Free +// Software Foundation, version 2 of the License. +// +// Olena 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 Olena. If not, see <http://www.gnu.org/licenses/>. +// +// As a special exception, you may use this file as part of a free +// software project 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 SCRIBO_FILTER_OBJECT_LINKS_TOP_ALIGNED_HH +# define SCRIBO_FILTER_OBJECT_LINKS_TOP_ALIGNED_HH + +/// \file +/// +/// Invalidate links between two objects if their top are not +/// aligned. + + +# include <mln/util/array.hh> + +# include <scribo/core/macros.hh> +# include <scribo/core/object_links.hh> +# include <scribo/core/object_image.hh> + +# include <scribo/filter/object_links_non_aligned_simple.hh> + +namespace scribo +{ + + namespace filter + { + + using namespace mln; + + /*! \brief Invalidate links between two objects if their top are not + aligned. + + \param[in] objects An object image. + \param[in] links Object links information. + \param[in] max_alpha Maximum angle value (degrees). + + \verbatim + + + ~ + ~ ^ + ~ | + ~ | + ~------ | Alpha + ~ | | | + ~ | | | + ~ | | v + ------ ~ ~ ~ | ~ ~| ~ + | | | | + | x------------x | + | | | | + ------ | | + object1 | | + ------ + object2 + + \endverbatim + + The angle between the two tops must be lower than \p max_alpha. + */ + template <typename L> + object_links<L> + object_links_top_aligned(const object_image(L)& objects, + const object_links<L>& links, + float max_alpha); + + +# ifndef MLN_INCLUDE_ONLY + + + template <typename L> + object_links<L> + object_links_top_aligned(const object_image(L)& objects, + const object_links<L>& links, + float max_alpha) + { + trace::entering("scribo::filter::object_links_top_aligned"); + + mln_precondition(objects.is_valid()); + mln_precondition(links.is_valid()); + + object_links<L> + output = object_links_non_aligned_simple(objects, links, + 0, + max_alpha); + + trace::exiting("scribo::filter::object_links_top_aligned"); + return output; + } + + +# endif // ! MLN_INCLUDE_ONLY + + + } // end of namespace scribo::filter + +} // end of namespace scribo + + +#endif // ! SCRIBO_FILTER_OBJECT_LINKS_TOP_ALIGNED_HH diff --git a/scribo/filter/objects_h_thick.hh b/scribo/filter/objects_h_thick.hh new file mode 100644 index 0000000..9709346 --- /dev/null +++ b/scribo/filter/objects_h_thick.hh @@ -0,0 +1,196 @@ +// Copyright (C) 2009 EPITA Research and Development Laboratory (LRDE) +// +// This file is part of Olena. +// +// Olena is free software: you can redistribute it and/or modify it under +// the terms of the GNU General Public License as published by the Free +// Software Foundation, version 2 of the License. +// +// Olena 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 Olena. If not, see <http://www.gnu.org/licenses/>. +// +// As a special exception, you may use this file as part of a free +// software project 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 SCRIBO_FILTER_OBJECTS_H_THICK_HH +# define SCRIBO_FILTER_OBJECTS_H_THICK_HH + +/// \file +/// +/// Remove too horizontaly thick objects. + +# include <mln/core/concept/image.hh> +# include <mln/core/concept/neighborhood.hh> + +# include <mln/util/array.hh> + +# include <scribo/core/object_image.hh> +# include <scribo/primitive/extract/objects.hh> +# include <scribo/filter/internal/compute.hh> + + +namespace scribo +{ + + namespace filter + { + + using namespace mln; + + /// Remove objects horizontaly thicker or equal to \p max_thickness. + /// + /// \param[in] input_ A binary image. + /// \param[in] nbh_ A neighborhood used in labeling algorithms. + /// \param[in] label_type The label type used for labeling. + /// \param[in] max_thickness The maximum thickness value. + /// + /// \result A binary image without thick objects. + // + template <typename I, typename N, typename V> + inline + mln_concrete(I) + objects_h_thick(const Image<I>& input_, + const Neighborhood<N>& nbh_, + const V& label_type, + unsigned max_thickness); + + + /// Remove objects horizontaly thicker or equal to \p max_thickness. + /// + /// \param[in] objects An object image. + /// \param[in] max_thickness The maximum thickness value. + /// + /// \result An object image without too thick objects. + // + template <typename L> + inline + object_image(L) + objects_h_thick(const object_image(L)& objects, + unsigned max_thickness); + + + +# ifndef MLN_INCLUDE_ONLY + + + namespace internal + { + + /// Filter Functor. Return false for all objects which are too + /// large. + template <typename L> + struct h_thick_object_filter + : Function_v2b< h_thick_object_filter<L> > + { + + /// Constructor + /// + /// \param[in] objects An object image. + /// \param[in] max_thickness the maximum thickness allowed. + // + h_thick_object_filter(const object_image(L)& objects, + unsigned max_thickness) + : objects_(objects), max_thickness_(max_thickness) + { + } + + /// Constructor + /// + /// \param[in] max_thickness the maximum thickness allowed. + // + h_thick_object_filter(unsigned max_thickness) + : max_thickness_(max_thickness) + { + } + + /// Set the underlying object image. + // + void update_objects(const object_image(L)& objects) + { + objects_ = objects; + } + + /// Return false if the objects is thicker than + /// \p max_thickness_. + /// + /// \param[in] l An image value. + bool operator()(const mln_value(L)& l) const + { + if (l == literal::zero) + return true; + return objects_.bbox(l).nrows() < max_thickness_; + } + + + /// An object image. + object_image(L) objects_; + + /// The maximum thickness. + unsigned max_thickness_; + }; + + + } // end of namespace scribo::filter::internal + + + template <typename I, typename N, typename V> + inline + mln_concrete(I) + objects_thick(const Image<I>& input_, + const Neighborhood<N>& nbh_, + const V& label_type, + unsigned max_thickness) + { + trace::entering("scribo::filter::objects_h_thick"); + + const I& input = exact(input_); + const N& nbh = exact(nbh_); + + mln_precondition(input.is_valid()); + mln_precondition(nbh.is_valid()); + + internal::h_thick_object_filter<V> functor(max_thickness); + mln_concrete(I) + output = internal::compute(input, nbh, label_type, functor); + + trace::exiting("scribo::filter::objects_h_thick"); + return output; + } + + + template <typename L> + inline + object_image(L) + objects_h_thick(const object_image(L)& objects, + unsigned max_thickness) + { + trace::entering("scribo::filter::objects_h_thick"); + + mln_precondition(objects.is_valid()); + + internal::h_thick_object_filter<L> functor(objects, max_thickness); + object_image(L) output = internal::compute(objects, functor); + + trace::exiting("scribo::filter::objects_h_thick"); + return output; + } + +# endif // ! MLN_INCLUDE_ONLY + + } // end of namespace scribo::filter + +} // end of namespace scribo + + +#endif // ! SCRIBO_FILTER_OBJECTS_H_THICK_HH diff --git a/scribo/filter/objects_v_thick.hh b/scribo/filter/objects_v_thick.hh new file mode 100644 index 0000000..730e144 --- /dev/null +++ b/scribo/filter/objects_v_thick.hh @@ -0,0 +1,196 @@ +// Copyright (C) 2009 EPITA Research and Development Laboratory (LRDE) +// +// This file is part of Olena. +// +// Olena is free software: you can redistribute it and/or modify it under +// the terms of the GNU General Public License as published by the Free +// Software Foundation, version 2 of the License. +// +// Olena 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 Olena. If not, see <http://www.gnu.org/licenses/>. +// +// As a special exception, you may use this file as part of a free +// software project 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 SCRIBO_FILTER_OBJECTS_V_THICK_HH +# define SCRIBO_FILTER_OBJECTS_V_THICK_HH + +/// \file +/// +/// Remove too verticaly thick objects. + +# include <mln/core/concept/image.hh> +# include <mln/core/concept/neighborhood.hh> + +# include <mln/util/array.hh> + +# include <scribo/core/object_image.hh> +# include <scribo/primitive/extract/objects.hh> +# include <scribo/filter/internal/compute.hh> + + +namespace scribo +{ + + namespace filter + { + + using namespace mln; + + /// Remove objects verticaly thicker or equal to \p max_thickness. + /// + /// \param[in] input_ A binary image. + /// \param[in] nbh_ A neighborhood used in labeling algorithms. + /// \param[in] label_type The label type used for labeling. + /// \param[in] max_thickness The maximum thickness value. + /// + /// \result A binary image without thick objects. + // + template <typename I, typename N, typename V> + inline + mln_concrete(I) + objects_v_thick(const Image<I>& input_, + const Neighborhood<N>& nbh_, + const V& label_type, + unsigned max_thickness); + + + /// Remove objects verticaly thicker or equal to \p max_thickness. + /// + /// \param[in] objects An object image. + /// \param[in] max_thickness The maximum thickness value. + /// + /// \result An object image without too thick objects. + // + template <typename L> + inline + object_image(L) + objects_v_thick(const object_image(L)& objects, + unsigned max_thickness); + + + +# ifndef MLN_INCLUDE_ONLY + + + namespace internal + { + + /// Filter Functor. Return false for all objects which are too + /// large. + template <typename L> + struct v_thick_object_filter + : Function_v2b< v_thick_object_filter<L> > + { + + /// Constructor + /// + /// \param[in] objects An object image. + /// \param[in] max_thickness the maximum thickness allowed. + // + v_thick_object_filter(const object_image(L)& objects, + unsigned max_thickness) + : objects_(objects), max_thickness_(max_thickness) + { + } + + /// Constructor + /// + /// \param[in] max_thickness the maximum thickness allowed. + // + v_thick_object_filter(unsigned max_thickness) + : max_thickness_(max_thickness) + { + } + + /// Set the underlying object image. + // + void update_objects(const object_image(L)& objects) + { + objects_ = objects; + } + + /// Return false if the objects is thicker than + /// \p max_thickness_. + /// + /// \param[in] l An image value. + bool operator()(const mln_value(L)& l) const + { + if (l == literal::zero) + return true; + return objects_.bbox(l).nrows() < max_thickness_; + } + + + /// An object image. + object_image(L) objects_; + + /// The maximum thickness. + unsigned max_thickness_; + }; + + + } // end of namespace scribo::filter::internal + + + template <typename I, typename N, typename V> + inline + mln_concrete(I) + objects_thick(const Image<I>& input_, + const Neighborhood<N>& nbh_, + const V& label_type, + unsigned max_thickness) + { + trace::entering("scribo::filter::objects_v_thick"); + + const I& input = exact(input_); + const N& nbh = exact(nbh_); + + mln_precondition(input.is_valid()); + mln_precondition(nbh.is_valid()); + + internal::v_thick_object_filter<V> functor(max_thickness); + mln_concrete(I) + output = internal::compute(input, nbh, label_type, functor); + + trace::exiting("scribo::filter::objects_v_thick"); + return output; + } + + + template <typename L> + inline + object_image(L) + objects_v_thick(const object_image(L)& objects, + unsigned max_thickness) + { + trace::entering("scribo::filter::objects_v_thick"); + + mln_precondition(objects.is_valid()); + + internal::v_thick_object_filter<L> functor(objects, max_thickness); + object_image(L) output = internal::compute(objects, functor); + + trace::exiting("scribo::filter::objects_v_thick"); + return output; + } + +# endif // ! MLN_INCLUDE_ONLY + + } // end of namespace scribo::filter + +} // end of namespace scribo + + +#endif // ! SCRIBO_FILTER_OBJECTS_V_THICK_HH -- 1.5.6.5