* filter/large_components.hh, * filter/small_components.hh, * filter/thick_bboxes.hh, * filter/thin_bboxes.hh: Move...
* filter/large_objects.hh, * filter/small_objects.hh, * filter/thick_objects.hh, * filter/thin_objects.hh: ... here. Make use of labeled_image type and cleanup comments.
--- scribo/ChangeLog | 15 +++ scribo/filter/large_components.hh | 214 ----------------------------------- scribo/filter/large_objects.hh | 198 +++++++++++++++++++++++++++++++++ scribo/filter/small_components.hh | 222 ------------------------------------- scribo/filter/small_objects.hh | 217 ++++++++++++++++++++++++++++++++++++ scribo/filter/thick_bboxes.hh | 204 ---------------------------------- scribo/filter/thick_objects.hh | 191 +++++++++++++++++++++++++++++++ scribo/filter/thin_bboxes.hh | 200 --------------------------------- scribo/filter/thin_objects.hh | 191 +++++++++++++++++++++++++++++++ 9 files changed, 812 insertions(+), 840 deletions(-) delete mode 100644 scribo/filter/large_components.hh create mode 100644 scribo/filter/large_objects.hh delete mode 100644 scribo/filter/small_components.hh create mode 100644 scribo/filter/small_objects.hh delete mode 100644 scribo/filter/thick_bboxes.hh create mode 100644 scribo/filter/thick_objects.hh delete mode 100644 scribo/filter/thin_bboxes.hh create mode 100644 scribo/filter/thin_objects.hh
diff --git a/scribo/ChangeLog b/scribo/ChangeLog index 0c8eae6..05ca9d5 100644 --- a/scribo/ChangeLog +++ b/scribo/ChangeLog @@ -1,5 +1,20 @@ 2009-05-28 Guillaume Lazzara lazzara@lrde.epita.fr
+ Cleanup object filters in Scribo. + + * filter/large_components.hh, + * filter/small_components.hh, + * filter/thick_bboxes.hh, + * filter/thin_bboxes.hh: Move... + + * filter/large_objects.hh, + * filter/small_objects.hh, + * filter/thick_objects.hh, + * filter/thin_objects.hh: ... here. Make use of labeled_image type and + cleanup comments. + +2009-05-28 Guillaume Lazzara lazzara@lrde.epita.fr + Make use of labeled_image type in Scribo.
* text/extract_bboxes.hh, diff --git a/scribo/filter/large_components.hh b/scribo/filter/large_components.hh deleted file mode 100644 index c3ea79b..0000000 --- a/scribo/filter/large_components.hh +++ /dev/null @@ -1,214 +0,0 @@ -// Copyright (C) 2009 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 SCRIBO_FILTER_LARGE_COMPONENTS_HH -# define SCRIBO_FILTER_LARGE_COMPONENTS_HH - -/// \file scribo/filter/large_components.hh -/// -/// Remove large components in a binary image. - - -# include <mln/core/concept/image.hh> -# include <mln/core/concept/neighborhood.hh> -# include <mln/core/concept/function.hh> - -# include <mln/labeling/blobs.hh> -# include <mln/labeling/relabel.hh> - -# include <mln/util/array.hh> -# include <mln/value/label_16.hh> - -# include <mln/pw/all.hh> - -# include <scribo/make/text.hh> - -namespace scribo -{ - - namespace filter - { - - using namespace mln; - - - /// Remove large components in a binary image. - /// Set to 'false' all the removed components. - /// - /// \param[in] input_ A binary image. - /// \param[in] nbh_ A neighborhood used for labeling \p input_. - /// \param[in] label_type The label type used for labeling. - /// \param[in] max_size The minimum cardinality of a component. - /// - /// \return A binary image without large components. - template <typename I, typename N, typename V> - mln_concrete(I) - large_components(const Image<I>& input_, - const Neighborhood<N>& nbh_, - const V& label_type, - unsigned max_size); - - /// Remove too large text components. - /// - /// \param[in] text Text data. - /// \param[in] min_size The minimum cardinality of a component. - /// - /// \return updated text data. - template <typename I> - scribo::util::text<I> - large_components(const scribo::util::text<I>& text, - unsigned min_size); - - - -# ifndef MLN_INCLUDE_ONLY - - - namespace internal - { - - - /// Filter Functor. - /// Return false for all components which are too large. - template <typename R> - struct filter_large_components_functor - : Function_l2b< filter_large_components_functor<R> > - { - - /// Constructor - /// - /// \param[in] compbboxes Component bounding boxes. - /// \param[in] max_size Maximum component size. - filter_large_components_functor(const mln::util::array<R>& compbboxes, - unsigned max_size) - : compbboxes_(compbboxes), max_size_(max_size) - { - } - - - /// Check if the component is large enough. - /// - /// \param l A label. - /// - /// \return false if the component area is strictly inferion to - /// \p max_size_. - bool operator()(const value::label_16& l) const - { - return compbboxes_[l] <= max_size_; - } - - - /// The component bounding boxes. - const mln::util::array<R>& compbboxes_; - /// The maximum area. - unsigned max_size_; - }; - - - } // end of namespace scribo::filter::internal - - - - template <typename I, typename N, typename V> - inline - mln_concrete(I) - large_components(const Image<I>& input_, - const Neighborhood<N>& nbh_, - const V& label_type, - unsigned max_size) - { - trace::entering("scribo::filter::large_components"); - - const I& input = exact(input_); - const N& nbh = exact(nbh_); - - mln_precondition(input.is_valid()); - mln_precondition(nbh.is_valid()); - - V nlabels; - mln_ch_value(I,V) lbl = labeling::blobs(input, nbh, nlabels); - - typedef accu::count<mln_psite(I)> accu_count_t; - typedef mln_result(accu_count_t) accu_count_res_t; - typedef mln::util::array<accu_count_res_t> nsitecomp_t; - nsitecomp_t nsitecomp = labeling::compute(accu_count_t(), lbl, nlabels); - - typedef internal::filter_large_components_functor<accu_count_res_t> func_t; - func_t fl2b(nsitecomp, max_size); - labeling::relabel_inplace(lbl, nlabels, fl2b); - - mln_concrete(I) output = duplicate(input); - data::fill((output | pw::value(lbl) == literal::zero).rw(), false); - - trace::exiting("scribo::filter::large_components"); - return output; - } - - - template <typename I> - inline - scribo::util::text<I> - large_components(const scribo::util::text<I>& text, - unsigned max_size) - { - trace::entering("scribo::filter::large_components"); - - mln_precondition(text.is_valid()); - - typedef mln_site(I) P; - typedef accu::count<P> accu_count_t; - typedef mln_result(accu_count_t) accu_count_res_t; - typedef mln::util::array<accu_count_res_t> nsitecomp_t; - - fun::i2v::array<bool> f(text.nbboxes().next(), false); - f(0) = true; - mln::util::array<box<P> > bresult; - bresult.append(box<P>()); - for_all_components(i, text.bboxes()) - { - accu_count_res_t count = set::compute(accu_count_t(), text.bbox(i)); - if (count <= max_size) - { - bresult.append(text.bbox(i)); - f(i) = true; - } - } - - util::text<I> output = scribo::make::text(text, f); - - trace::exiting("scribo::filter::large_components"); - return output; - } - - -# endif // ! MLN_INCLUDE_ONLY - - } // end of namespace scribo::filter - -} // end of namespace scribo - -#endif // ! SCRIBO_FILTER_LARGE_COMPONENTS_HH diff --git a/scribo/filter/large_objects.hh b/scribo/filter/large_objects.hh new file mode 100644 index 0000000..16e50c6 --- /dev/null +++ b/scribo/filter/large_objects.hh @@ -0,0 +1,198 @@ +// Copyright (C) 2009 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 SCRIBO_FILTER_LARGE_OBJECTS_HH +# define SCRIBO_FILTER_LARGE_OBJECTS_HH + +/// \file scribo/filter/large_objects.hh +/// +/// Remove large objects in a binary image. + + +# include <mln/core/concept/image.hh> +# include <mln/core/concept/neighborhood.hh> +# include <mln/core/concept/function.hh> + +# include <mln/labeling/compute.hh> +# include <mln/accu/count.hh> + +# include <mln/util/array.hh> + +# include <mln/pw/all.hh> + +# include <scribo/core/object_image.hh> +# include <scribo/extract/primitive/objects.hh> + +namespace scribo +{ + + namespace filter + { + + using namespace mln; + + + /// Remove large objects in a binary image. + /// Set to 'false' all the removed objects. + /// + /// \param[in] input_ A binary image. + /// \param[in] nbh_ A neighborhood used for labeling \p input_. + /// \param[in] label_type The label type used for labeling. + /// \param[in] max_size The minimum cardinality of an object. + /// + /// \return A binary image without large objects. + template <typename I, typename N, typename V> + mln_concrete(I) + large_objects(const Image<I>& input_, + const Neighborhood<N>& nbh_, + const V& label_type, + unsigned max_size); + + /// Remove too large text objects. + /// + /// \param[in] objects An object image. + /// \param[in] max_size The minimum cardinality of an object. + /// + /// \return updated text data. + template <typename L> + object_image(L) + large_objects(const object_image(L)& objects, + unsigned max_size); + + + +# ifndef MLN_INCLUDE_ONLY + + + namespace internal + { + + + /// Filter Functor. + /// Return false for all objects which are too large. + template <typename L> + struct large_objects_filter + : Function_v2b< large_objects_filter<L> > + { + + typedef accu::count<mln_psite(L)> card_t; + + /// Constructor + /// + /// \param[in] compbboxes Component bounding boxes. + /// \param[in] max_size Maximum object size. + large_objects_filter(const object_image(L)& objects, + unsigned max_size) + { + card_ = labeling::compute(card_t(), objects, objects.nlabels()); + max_size_ = max_size; + } + + + /// Check if the object is large enough. + /// + /// \param l A label. + /// + /// \return false if the object area is strictly inferior to + /// \p max_size_. + bool operator()(const mln_value(L)& l) const + { + return card_[l] <= max_size_; + } + + + /// The object bounding boxes. + mln::util::array<mln_result(card_t)> card_; + /// The maximum area. + unsigned max_size_; + }; + + + } // end of namespace scribo::filter::internal + + + + template <typename I, typename N, typename V> + inline + mln_concrete(I) + large_objects(const Image<I>& input_, + const Neighborhood<N>& nbh_, + const V& label_type, + unsigned max_size) + { + trace::entering("scribo::filter::large_objects"); + + const I& input = exact(input_); + const N& nbh = exact(nbh_); + + mln_precondition(input.is_valid()); + mln_precondition(nbh.is_valid()); + + V nlabels; + typedef object_image(mln_ch_value(I,V)) lbl_t; + lbl_t lbl = extract::primitive::objects(input, nbh, nlabels); + + typedef internal::large_objects_filter<lbl_t> func_t; + func_t fv2b(lbl, max_size); + labeling::relabel_inplace(lbl, nlabels, fv2b); + + mln_concrete(I) output = duplicate(input); + data::fill((output | pw::value(lbl) == literal::zero).rw(), false); + + trace::exiting("scribo::filter::large_objects"); + return output; + } + + + template <typename L> + inline + object_image(L) + large_objects(const object_image(L)& objects, + unsigned max_size) + { + trace::entering("scribo::filter::large_objects"); + + mln_precondition(objects.is_valid()); + + internal::large_objects_filter<L> f(objects, max_size); + + object_image(L) output; + output.init_from_(objects); + output.relabel(f); + + trace::exiting("scribo::filter::large_objects"); + return output; + } + + +# endif // ! MLN_INCLUDE_ONLY + + } // end of namespace scribo::filter + +} // end of namespace scribo + +#endif // ! SCRIBO_FILTER_LARGE_OBJECTS_HH diff --git a/scribo/filter/small_components.hh b/scribo/filter/small_components.hh deleted file mode 100644 index c28772b..0000000 --- a/scribo/filter/small_components.hh +++ /dev/null @@ -1,222 +0,0 @@ -// Copyright (C) 2009 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 SCRIBO_FILTER_SMALL_COMPONENTS_HH -# define SCRIBO_FILTER_SMALL_COMPONENTS_HH - -/// \file scribo/filter/small_components.hh -/// -/// Remove small components in a binary image. - - -# include <mln/core/concept/image.hh> -# include <mln/core/concept/neighborhood.hh> -# include <mln/core/concept/function.hh> - -# include <mln/labeling/blobs.hh> -# include <mln/labeling/relabel.hh> - -# include <mln/make/relabelfun.hh> - -# include <mln/util/array.hh> - -# include <mln/pw/all.hh> - -# include <mln/accu/count.hh> - -# include <mln/set/compute.hh> - -# include <scribo/util/text.hh> -# include <scribo/make/text.hh> - - -namespace scribo -{ - - namespace filter - { - - using namespace mln; - - - /// Remove small components in a binary image. - /// Set to 'false' all the removed components. - /// - /// \param[in] input_ A binary image. - /// \param[in] nbh_ A neighborhood used for labeling \p input_. - /// \param[in] label_type The label type used for labeling. - /// \param[in] min_size The minimum cardinality of a component. - /// - /// \return A binary image without small components. - template <typename I, typename N, typename V> - mln_concrete(I) - small_components(const Image<I>& input_, - const Neighborhood<N>& nbh_, - const V& label_type, - unsigned min_size); - - - /// Remove too small text components. - /// - /// \param[in] text Text data. - /// \param[in] min_size The minimum cardinality of a component. - /// - /// \return Lines of text without too small components. - template <typename I> - scribo::util::text<I> - small_components(const scribo::util::text<I>& text, - unsigned min_size); - - -# ifndef MLN_INCLUDE_ONLY - - - namespace internal - { - - - /// Filter Functor. - /// Return false for all components which are too small. - template <typename B> - struct filter_small_components_functor - : Function_v2b< filter_small_components_functor<B> > - { - - /// Constructor - /// - /// \param[in] compbboxes Component bounding boxes. - /// \param[in] min_size Minimum component size. - filter_small_components_functor(const mln::util::array<B>& compbboxes, - unsigned min_size) - : compbboxes_(compbboxes), min_size_(min_size) - { - } - - - /// Check if the component is large enough. - /// - /// \param l A label. - /// - /// \return false if the component area is strictly inferion to - /// \p min_size_. - template <typename L> - bool operator()(const L& l) const - { - return compbboxes_[l] >= min_size_; - } - - /// The component bounding boxes. - const mln::util::array<B>& compbboxes_; - - /// The minimum area. - unsigned min_size_; - }; - - - } // end of namespace scribo::filter::internal - - - - template <typename I, typename N, typename V> - inline - mln_concrete(I) - small_components(const Image<I>& input_, - const Neighborhood<N>& nbh_, - const V& label_type, - unsigned min_size) - { - trace::entering("scribo::filter::small_components"); - - const I& input = exact(input_); - const N& nbh = exact(nbh_); - - mln_precondition(input.is_valid()); - mln_precondition(nbh.is_valid()); - - V nlabels; - mln_ch_value(I,V) lbl = labeling::blobs(input, nbh, nlabels); - - typedef accu::count<mln_psite(I)> accu_count_t; - typedef mln_result(accu_count_t) accu_count_res_t; - typedef mln::util::array<accu_count_res_t> compbboxes_t; - compbboxes_t compbboxes = labeling::compute(accu_count_t(), lbl, nlabels); - - typedef internal::filter_small_components_functor<accu_count_res_t> func_t; - func_t fv2b(compbboxes, min_size); - labeling::relabel_inplace(lbl, nlabels, fv2b); - - mln_concrete(I) output = duplicate(input); - data::fill((output | pw::value(lbl) == literal::zero).rw(), false); - - trace::exiting("scribo::filter::small_components"); - return output; - } - - - template <typename L> - inline - scribo::util::text<L> - small_components(const scribo::util::text<L>& text, - unsigned min_size) - { - trace::entering("scribo::filter::small_components"); - - mln_precondition(text.is_valid()); - - typedef mln_site(L) P; - typedef accu::count<P> accu_count_t; - typedef mln_result(accu_count_t) accu_count_res_t; - typedef mln::util::array<accu_count_res_t> nsitecomp_t; - - fun::i2v::array<bool> f(text.nbboxes().next(), false); - f(0) = true; - mln::util::array<box<P> > bresult; - bresult.append(box<P>()); - for_all_components(i, text.bboxes()) - { - accu_count_res_t count = set::compute(accu_count_t(), text.bbox(i)); - if (count >= min_size) - { - bresult.append(text.bbox(i)); - f(i) = true; - } - } - - util::text<L> output = scribo::make::text(text, f); - - trace::exiting("scribo::filter::small_components"); - return output; - } - - -# endif // ! MLN_INCLUDE_ONLY - - } // end of namespace scribo::filter - -} // end of namespace scribo - -#endif // ! SCRIBO_FILTER_SMALL_COMPONENTS_HH diff --git a/scribo/filter/small_objects.hh b/scribo/filter/small_objects.hh new file mode 100644 index 0000000..0b7844e --- /dev/null +++ b/scribo/filter/small_objects.hh @@ -0,0 +1,217 @@ +// Copyright (C) 2009 EPITA Research and Development Laboratory (LRDE) +// +// 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 SCRIBO_FILTER_SMALL_OBJECTS_HH +# define SCRIBO_FILTER_SMALL_OBJECTS_HH + +/// \file scribo/filter/small_objects.hh +/// +/// Remove small objects in a binary image. + + +# include <mln/core/concept/image.hh> +# include <mln/core/concept/neighborhood.hh> +# include <mln/core/concept/function.hh> + +# include <mln/labeling/blobs.hh> +# include <mln/labeling/relabel.hh> + +# include <mln/make/relabelfun.hh> + +# include <mln/util/array.hh> + +# include <mln/pw/all.hh> + +# include <mln/accu/count.hh> + +# include <mln/set/compute.hh> + +# include <scribo/core/object_image.hh> +# include <scribo/extract/primitive/objects.hh> + + +//forward declaration. +namespace mln +{ + namespace accu + { + template <typename T> struct count; + } +} + +namespace scribo +{ + + namespace filter + { + + using namespace mln; + + + /// Remove small objects in a binary image. + /// Set to 'false' all the removed objects. + /// + /// \param[in] input_ A binary image. + /// \param[in] nbh_ A neighborhood used for labeling \p input_. + /// \param[in] label_type The label type used for labeling. + /// \param[in] min_size The minimum cardinality of an object. + /// + /// \return A binary image without small objects. + template <typename I, typename N, typename V> + mln_concrete(I) + small_objects(const Image<I>& input_, + const Neighborhood<N>& nbh_, + const V& label_type, + unsigned min_size); + + + /// Remove too small objects. + /// + /// \param[in] objects An object image. + /// \param[in] min_size The minimum cardinality of an object. + /// + /// \return An object image without small objects. + template <typename L> + object_image(L) + small_objects(const object_image(L)& objects, + unsigned min_size); + + +# ifndef MLN_INCLUDE_ONLY + + + namespace internal + { + + + /// Filter Functor. + /// Return false for all objects which are too small. + template <typename L> + struct small_objects_filter + : Function_v2b< small_objects_filter<L> > + { + typedef accu::count<mln_psite(L)> card_t; + + /// Constructor + /// + /// \param[in] objects Component bounding boxes. + /// \param[in] min_size Minimum component size. + // + small_objects_filter(const object_image(L)& objects, + unsigned min_size) + { + card_ = labeling::compute(card_t(), objects, objects.nlabels()); + min_size_ = min_size; + } + + + /// Check if the component is large enough. + /// + /// \param l A label. + /// + /// \return false if the component area is strictly inferion to + /// \p min_size_. + // + bool operator()(const mln_value(L)& l) const + { + if (l == literal::zero) + return true; + return card_[l] >= min_size_; + } + + /// The component bounding boxes. + mln::util::array<mln_result(card_t)> card_; + + /// The minimum area. + unsigned min_size_; + }; + + + } // end of namespace scribo::filter::internal + + + + template <typename I, typename N, typename V> + inline + mln_concrete(I) + small_objects(const Image<I>& input_, + const Neighborhood<N>& nbh_, + const V& label_type, + unsigned min_size) + { + trace::entering("scribo::filter::small_objects"); + + const I& input = exact(input_); + const N& nbh = exact(nbh_); + + mln_precondition(input.is_valid()); + mln_precondition(nbh.is_valid()); + + V nlabels; + typedef object_image(mln_ch_value(I,V)) lbl_t; + lbl_t lbl = extract::primitive::objects(input, nbh, nlabels); + + typedef internal::small_objects_filter<lbl_t> func_t; + func_t fv2b(lbl, min_size); + labeling::relabel_inplace(lbl, nlabels, fv2b); + + mln_concrete(I) output = duplicate(input); + data::fill((output | pw::value(lbl) == literal::zero).rw(), false); + + trace::exiting("scribo::filter::small_objects"); + return output; + } + + + template <typename L> + inline + object_image(L) + small_objects(const object_image(L)& objects, + unsigned min_size) + { + trace::entering("scribo::filter::small_objects"); + + mln_precondition(objects.is_valid()); + + internal::small_objects_filter<L> f(objects, min_size); + + object_image(L) output; + output.init_from_(objects); + output.relabel(f); + + trace::exiting("scribo::filter::small_objects"); + return output; + } + + +# endif // ! MLN_INCLUDE_ONLY + + } // end of namespace scribo::filter + +} // end of namespace scribo + +#endif // ! SCRIBO_FILTER_SMALL_OBJECTS_HH diff --git a/scribo/filter/thick_bboxes.hh b/scribo/filter/thick_bboxes.hh deleted file mode 100644 index 9f717b3..0000000 --- a/scribo/filter/thick_bboxes.hh +++ /dev/null @@ -1,204 +0,0 @@ -// Copyright (C) 2009 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 SCRIBO_FILTER_THICK_BBOXES_HH -# define SCRIBO_FILTER_THICK_BBOXES_HH - -/// \file scribo/filter/thick_bboxes.hh -/// -/// Remove too thick bboxes. - -# include <mln/labeling/blobs.hh> -# include <mln/labeling/compute.hh> -# include <mln/util/array.hh> - -# include <scribo/util/text.hh> - -# include <scribo/make/text.hh> - - -namespace scribo -{ - - namespace filter - { - - /// Remove components 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 components. - template <typename I, typename N, typename V> - inline - mln_concrete(I) - thick_bboxes(const Image<I>& input_, - const Neighborhood<N>& nbh_, - const V& label_type, - unsigned max_thickness); - - - /// Remove lines of text thicker or equal to \p max_thickness. - /// - /// \param[in] text Lines of text. - /// \param[in] max_thickness The maximum thickness value. - /// - /// \result Lines of text without too thick lines. - template <typename L> - inline - scribo::util::text<L> - thick_bboxes(const scribo::util::text<L>& text, - unsigned max_thickness); - - -# ifndef MLN_INCLUDE_ONLY - - namespace internal - { - - /// Filter Functor. Return false for all components which are too - /// large. - template <typename R> - struct filter_too_thick_component_functor - : Function_l2b< filter_too_thick_component_functor<R> > - { - - /// Constructor - /// - /// \param[in] compbboxes component bounding boxes. - /// \param[in] max_thickness the maximum thickness allowed. - filter_too_thick_component_functor(const mln::util::array<R>& compbboxes, - unsigned max_thickness) - : compbboxes_(compbboxes), max_thickness_(max_thickness) - { - } - - - /// Return false if the components is thicker than - /// \p max_thickness_. - /// - /// \param[in] l An image value. - bool operator()(const value::label_16& l) const - { - return compbboxes_[l].nrows() < max_thickness_ - && compbboxes_[l].ncols() < max_thickness_; - } - - - /// Component bounding boxes. - const mln::util::array<R>& compbboxes_; - - /// The maximum thickness. - unsigned max_thickness_; - }; - - - } // end of namespace scribo::filter::internal - - - template <typename I, typename N, typename V> - inline - mln_concrete(I) - thick_bboxes(const Image<I>& input_, - const Neighborhood<N>& nbh_, - const V& label_type, - unsigned max_thickness) - { - trace::entering("scribo::filter::thick_bboxes"); - - const I& input = exact(input_); - const N& nbh = exact(nbh_); - - mln_precondition(input.is_valid()); - mln_precondition(nbh.is_valid()); - - V nlabels; - mln_ch_value(I,V) lbl = labeling::blobs(input, nbh, nlabels); - - typedef accu::bbox<mln_psite(I)> accu_bbox_t; - typedef mln_result(accu_bbox_t) accu_bbox_res_t; - typedef mln::util::array<accu_bbox_res_t> compbboxes_t; - compbboxes_t compbboxes = labeling::compute(accu_bbox_t(), lbl, nlabels); - - typedef internal::filter_too_thick_component_functor<accu_bbox_res_t> func_t; - func_t fl2b(compbboxes, max_thickness); - labeling::relabel_inplace(lbl, nlabels, fl2b); - - mln_concrete(I) output = duplicate(input); - data::fill((output | pw::value(lbl) == literal::zero).rw(), false); - - trace::exiting("scribo::filter::thick_bboxes"); - return output; - } - - - template <typename L> - inline - scribo::util::text<L> - thick_bboxes(const scribo::util::text<L>& text, - unsigned max_thickness) - { - trace::entering("scribo::filter::thick_bboxes"); - - mln_precondition(text.is_valid()); - - typedef mln_site(L) P; - typedef accu::bbox<P> accu_bbox_t; - typedef mln_result(accu_bbox_t) accu_bbox_res_t; - typedef mln::util::array<accu_bbox_res_t> nsitecomp_t; - - typedef internal::filter_too_thick_component_functor<accu_bbox_res_t> func_t; - func_t is_not_too_thick(text.bboxes(), max_thickness); - - fun::i2v::array<bool> f(text.nbboxes().next(), false); - f(0) = true; - mln::util::array<box<P> > bresult; - bresult.append(box<P>()); - for_all_components(i, text.bboxes()) - if (is_not_too_thick(i)) - { - bresult.append(text.bbox(i)); - f(i) = true; - } - - util::text<L> output = scribo::make::text(text, f); - - trace::exiting("scribo::filter::thick_bboxes"); - /// FIXME: construct a new util::text from the old one. - return output; - } - -# endif // ! MLN_INCLUDE_ONLY - - } // end of namespace scribo::filter - -} // end of namespace scribo - - -#endif // ! SCRIBO_FILTER_THICK_BBOXES_HH diff --git a/scribo/filter/thick_objects.hh b/scribo/filter/thick_objects.hh new file mode 100644 index 0000000..d7ef128 --- /dev/null +++ b/scribo/filter/thick_objects.hh @@ -0,0 +1,191 @@ +// Copyright (C) 2009 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 SCRIBO_FILTER_THICK_OBJECTS_HH +# define SCRIBO_FILTER_THICK_OBJECTS_HH + +/// \file scribo/filter/thick_objects.hh +/// +/// Remove too 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/extract/primitive/objects.hh> + + + +namespace scribo +{ + + namespace filter + { + + using namespace mln; + + /// Remove objects 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) + thick_objects(const Image<I>& input_, + const Neighborhood<N>& nbh_, + const V& label_type, + unsigned max_thickness); + + + /// Remove lines of text 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) + thick_objects(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 thick_object_filter + : Function_v2b< thick_object_filter<L> > + { + + /// Constructor + /// + /// \param[in] objects An object image. + /// \param[in] max_thickness the maximum thickness allowed. + thick_object_filter(const object_image(L)& objects, + unsigned max_thickness) + : objects_(objects), max_thickness_(max_thickness) + { + } + + + /// 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 + { + return objects_.bbox(l).nrows() < max_thickness_ + && objects_.bbox(l).ncols() < 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) + thick_objects(const Image<I>& input_, + const Neighborhood<N>& nbh_, + const V& label_type, + unsigned max_thickness) + { + trace::entering("scribo::filter::thick_objects"); + + 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 + = extract::primitive::objects(input, nbh, nlabels); + + typedef internal::thick_object_filter<lbl_t> func_t; + func_t fv2b(objects, max_thickness); + objects.relabel(fv2b); + + mln_concrete(I) output = duplicate(input); + data::fill((output | pw::value(objects) == literal::zero).rw(), false); + + trace::exiting("scribo::filter::thick_objects"); + return output; + } + + + template <typename L> + inline + object_image(L) + thick_objects(const object_image(L)& objects, + unsigned max_thickness) + { + trace::entering("scribo::filter::thick_objects"); + + mln_precondition(text.is_valid()); + + typedef internal::thick_object_filter<L> func_t; + func_t is_not_too_thick(objects, max_thickness); + + object_image(L) output; + output.init_from_(objects); + output.relabel(is_not_too_thick); + + trace::exiting("scribo::filter::thick_objects"); + return output; + } + +# endif // ! MLN_INCLUDE_ONLY + + } // end of namespace scribo::filter + +} // end of namespace scribo + + +#endif // ! SCRIBO_FILTER_THICK_OBJECTS_HH diff --git a/scribo/filter/thin_bboxes.hh b/scribo/filter/thin_bboxes.hh deleted file mode 100644 index 1463a64..0000000 --- a/scribo/filter/thin_bboxes.hh +++ /dev/null @@ -1,200 +0,0 @@ -// Copyright (C) 2009 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 SCRIBO_FILTER_THIN_BBOXES_HH -# define SCRIBO_FILTER_THIN_BBOXES_HH - -/// \file scribo/filter/thin_bboxes.hh -/// -/// Remove too thin bboxes. - -# include <mln/labeling/blobs.hh> -# include <mln/labeling/compute.hh> -# include <mln/util/array.hh> - -# include <scribo/util/text.hh> - -namespace scribo -{ - - namespace filter - { - - /// Remove components thinner or equal to \p min_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] min_thickness the minimum thickness value. - /// - /// \result A binary image without thin components. - template <typename I, typename N, typename V> - inline - mln_concrete(I) - thin_bboxes(const Image<I>& input_, - const Neighborhood<N>& nbh_, - const V& label_type, - unsigned min_thickness); - - /// Remove lines of text thinner or equal to \p min_thickness. - /// - /// \param[in] text lines of text. - /// \param[in] min_thickness the minimum thickness value. - /// - /// \result Lines of text without too thin lines. - template <typename L> - inline - scribo::util::text<L> - thin_bboxes(const scribo::util::text<L>& text, - unsigned min_thickness); - - -# ifndef MLN_INCLUDE_ONLY - - namespace internal - { - - - /// Filter Functor. - /// Return false for all components which are too large. - template <typename R> - struct filter_too_thin_component_functor - : Function_v2b< filter_too_thin_component_functor<R> > - { - - /// Constructor - /// - /// \param[in] compbboxes component bounding boxes. - /// \param[in] min_thickness the minimum thickness allowed. - filter_too_thin_component_functor(const mln::util::array<R>& compbboxes, - unsigned min_thickness) - : compbboxes_(compbboxes), min_thickness_(min_thickness) - { - } - - - /// Return false if the components is thinner than - /// \p min_thickness_. - /// - /// \param[in] l An image value. - bool operator()(const value::label_16& l) const - { - return compbboxes_[l].nrows() > min_thickness_ - && compbboxes_[l].ncols() > min_thickness_; - } - - - /// Component bounding boxes. - const mln::util::array<R>& compbboxes_; - - /// The minimum thickness. - unsigned min_thickness_; - }; - - - } // end of namespace scribo::filter::internal - - - template <typename I, typename N, typename V> - inline - mln_concrete(I) - thin_bboxes(const Image<I>& input_, - const Neighborhood<N>& nbh_, - const V& label_type, - unsigned min_thickness) - { - trace::entering("scribo::filter::thin_bboxes"); - - const I& input = exact(input_); - const N& nbh = exact(nbh_); - - mln_precondition(input.is_valid()); - mln_precondition(nbh.is_valid()); - - V nlabels; - mln_ch_value(I,V) lbl = labeling::blobs(input, nbh, nlabels); - - typedef accu::bbox<mln_psite(I)> accu_bbox_t; - typedef mln_result(accu_bbox_t) accu_bbox_res_t; - typedef mln::util::array<accu_bbox_res_t> compbboxes_t; - compbboxes_t compbboxes = labeling::compute(accu_bbox_t(), lbl, nlabels); - - typedef internal::filter_too_thin_component_functor<accu_bbox_res_t> func_t; - func_t fv2b(compbboxes, min_thickness); - labeling::relabel_inplace(lbl, nlabels, fv2b); - - mln_concrete(I) output = duplicate(input); - data::fill((output | pw::value(lbl) == literal::zero).rw(), false); - - trace::exiting("scribo::filter::thin_bboxes"); - return output; - } - - - template <typename L> - inline - scribo::util::text<L> - thin_bboxes(const scribo::util::text<L>& text, - unsigned min_thickness) - { - trace::entering("scribo::filter::thin_bboxes"); - - mln_precondition(text.is_valid()); - - typedef mln_site(L) P; - typedef accu::bbox<P> accu_bbox_t; - typedef mln_result(accu_bbox_t) accu_bbox_res_t; - typedef mln::util::array<accu_bbox_res_t> nsitecomp_t; - - typedef internal::filter_too_thin_component_functor<accu_bbox_res_t> func_t; - func_t is_not_too_thin(text.bboxes(), min_thickness); - - fun::i2v::array<bool> f(text.nbboxes().next(), false); - f(0) = true; - mln::util::array<box<P> > bresult; - bresult.append(box<P>()); - for_all_components(i, text.bboxes()) - if (is_not_too_thin(i)) - { - bresult.append(text.bbox(i)); - f(i) = true; - } - - util::text<L> output = scribo::make::text(text, f); - - trace::exiting("scribo::filter::thin_bboxes"); - return output; - } - -# endif // ! MLN_INCLUDE_ONLY - - } // end of namespace scribo::filter - -} // end of namespace scribo - - -#endif // ! SCRIBO_FILTER_THIN_BBOXES_HH diff --git a/scribo/filter/thin_objects.hh b/scribo/filter/thin_objects.hh new file mode 100644 index 0000000..45e491a --- /dev/null +++ b/scribo/filter/thin_objects.hh @@ -0,0 +1,191 @@ +// Copyright (C) 2009 EPITA Research and Development Laboratory (LRDE) +// +// 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 SCRIBO_FILTER_THIN_OBJECTS_HH +# define SCRIBO_FILTER_THIN_OBJECTS_HH + +/// \file scribo/filter/thin_objects.hh +/// +/// Remove too thin 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/extract/primitive/objects.hh> + +namespace scribo +{ + + namespace filter + { + + using namespace mln; + + /// Remove objects thinner or equal to \p min_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] min_thickness the minimum thickness value. + /// + /// \result A binary image without thin objects. + // + template <typename I, typename N, typename V> + inline + mln_concrete(I) + thin_objects(const Image<I>& input_, + const Neighborhood<N>& nbh_, + const V& label_type, + unsigned min_thickness); + + /// Remove lines of text thinner or equal to \p min_thickness. + /// + /// \param[in] objects An object image. + /// \param[in] min_thickness the minimum thickness value. + /// + /// \result An object image without too thin objects. + // + template <typename L> + inline + object_image(L) + thin_objects(const object_image(L)& text, + unsigned min_thickness); + + +# ifndef MLN_INCLUDE_ONLY + + namespace internal + { + + + /// Filter Functor. + /// Return false for all objects which are too large. + template <typename L> + struct thin_objects_filter + : Function_v2b< thin_objects_filter<L> > + { + typedef accu::bbox<mln_psite(L)> box_accu_t; + + /// Constructor + /// + /// \param[in] objects object bounding boxes. + /// \param[in] min_thickness the minimum thickness allowed. + thin_objects_filter(const object_image(L)& objects, + unsigned min_thickness) + : objects_(objects), min_thickness_(min_thickness) + { + } + + + /// Return false if the objects is thinner than + /// \p min_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() > min_thickness_ + && objects_.bbox(l).ncols() > min_thickness_; + } + + /// Component bounding boxes. + object_image(L) objects_; + + /// The minimum thickness. + unsigned min_thickness_; + }; + + + } // end of namespace scribo::filter::internal + + + template <typename I, typename N, typename V> + inline + mln_concrete(I) + thin_objects(const Image<I>& input_, + const Neighborhood<N>& nbh_, + const V& label_type, + unsigned min_thickness) + { + trace::entering("scribo::filter::thin_objects"); + + 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 + = extract::primitive::objects(input, nbh, nlabels); + + typedef internal::thin_objects_filter<lbl_t> func_t; + func_t fv2b(objects, min_thickness); + objects.relabel(fv2b); + + mln_concrete(I) output = duplicate(input); + data::fill((output | pw::value(objects) == literal::zero).rw(), false); + + trace::exiting("scribo::filter::thin_objects"); + return output; + } + + + template <typename L> + inline + object_image(L) + thin_objects(const object_image(L)& objects, + unsigned min_thickness) + { + trace::entering("scribo::filter::thin_objects"); + + mln_precondition(objects.is_valid()); + + typedef internal::thin_objects_filter<L> func_t; + func_t is_not_too_thin(objects, min_thickness); + + object_image(L) output; + output.init_from_(objects); + output.relabel(is_not_too_thin); + + trace::exiting("scribo::filter::thin_objects"); + return output; + } + +# endif // ! MLN_INCLUDE_ONLY + + } // end of namespace scribo::filter + +} // end of namespace scribo + + +#endif // ! SCRIBO_FILTER_THIN_OBJECTS_HH