
--- scribo/ChangeLog | 9 ++ scribo/scribo/inverse_video/choose.hh | 118 ++++++++++++++++++++++ scribo/scribo/inverse_video/handle_collisions.hh | 15 ++-- scribo/scribo/inverse_video/nb_comps.hh | 103 +++++++++++++++++++ scribo/scribo/inverse_video/ratio_area.hh | 93 +++++++++++++++++ 5 files changed, 331 insertions(+), 7 deletions(-) create mode 100644 scribo/scribo/inverse_video/choose.hh create mode 100644 scribo/scribo/inverse_video/nb_comps.hh create mode 100644 scribo/scribo/inverse_video/ratio_area.hh diff --git a/scribo/ChangeLog b/scribo/ChangeLog index 0c7b65f..3fbee9e 100644 --- a/scribo/ChangeLog +++ b/scribo/ChangeLog @@ -1,3 +1,12 @@ +2011-05-27 Sylvain Lobry <lobry@lrde.epita.fr> + + Added a choose function, to be used with handle_collisions. + + * scribo/inverse_video/handle_collisions.hh: Prototype fixed. + * scribo/inverse_video/choose.hh, + * scribo/inverse_video/nb_comps.hh, + * scribo/inverse_video/ratio_area.hh: New. + 2011-05-26 Coddy Levi <levi@lrde.epita.fr> Bug corrected, several improvements, interface modified. diff --git a/scribo/scribo/inverse_video/choose.hh b/scribo/scribo/inverse_video/choose.hh new file mode 100644 index 0000000..9b0d46e --- /dev/null +++ b/scribo/scribo/inverse_video/choose.hh @@ -0,0 +1,118 @@ +// Copyright (C) 2011 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_INVERSE_VIDEO_CHOOSE_HH +# define SCRIBO_INVERSE_VIDEO_CHOOSE_HH + +/// \file +/// +/// \brief Choose beetwen two lines. + +# include <scribo/core/line_set.hh> +# include <scribo/inverse_video/nb_comps.hh> +# include <scribo/inverse_video/ratio_area.hh> +# include <iostream> + +namespace scribo +{ + + namespace inverse_video + { + /// Choose the most meaningful line between two. + /*! + ** \param[in] input The input image. + ** \param[in] line The first line. + ** \param[in] line_inverse The second line + ** + ** \return void + */ + template <typename L, typename I> + static inline + void + choose (mln::image2d<I>& input, + scribo::line_info<L>& line, + scribo::line_info<L>& line_inverse); + +# ifndef MLN_INCLUDE_ONLY + + namespace internal + { + template <typename I, typename L> + static inline + void + handle_score (double score, + I& image, + scribo::line_info<L>& line, + scribo::line_info<L>& line_inverse) + { + if (score > 0.5) + { + mln::draw::box(image, line.bbox(), mln::literal::green); + mln::draw::box(image, line_inverse.bbox(), mln::literal::red); + line_inverse.update_tag(scribo::line::Ignored); + line_inverse.set_hidden(true); + std::cout << "normal choosed" << std::endl; + } + else + { + mln::draw::box(image, line_inverse.bbox(), mln::literal::green); + mln::draw::box(image, line.bbox(), mln::literal::red); + line.update_tag(scribo::line::Ignored); + line.set_hidden(true); + std::cout << "inverse choosed" << std::endl; + } + } + } // end of namespace scribo::text::internal + + template <typename L, typename I> + static inline + void + choose (mln::image2d<I>& input, + scribo::line_info<L>& line, + scribo::line_info<L>& line_inverse) + { + mln::trace::entering("scribo::inverse_video::choose"); + + double score = 0; + + double score_nb_comps = scribo::inverse_video::nb_comps(line, line_inverse); + double score_ratio_area = scribo::inverse_video::ratio_area(line, line_inverse); + + score = + score_nb_comps * 0.5 + + score_ratio_area * 0.5; + + scribo::inverse_video::internal::handle_score (score, input, line, line_inverse); + + mln::trace::exiting("scribo::inverse_video::choose"); + } + +# endif // ! MLN_INCLUDE_ONLY + + } // end of namespace scribo::inverse_video + +} // end of namespace scribo + +#endif // ! SCRIBO_INVERSE_VIDEO_CHOOSE_HH diff --git a/scribo/scribo/inverse_video/handle_collisions.hh b/scribo/scribo/inverse_video/handle_collisions.hh index 4a772c3..3d27599 100644 --- a/scribo/scribo/inverse_video/handle_collisions.hh +++ b/scribo/scribo/inverse_video/handle_collisions.hh @@ -44,14 +44,14 @@ namespace scribo { template <typename I, - typename L, - typename F> + typename L> void - handle_collisions(const image2d<I>& input, + handle_collisions(image2d<I>& input, scribo::line_set<L>& ls1, scribo::line_set<L>& ls2, - F choose); - + void (*choose)(image2d<I>&, + scribo::line_info<L>&, + scribo::line_info<L>&)); # ifndef MLN_INCLUDE_ONLY namespace internal @@ -90,7 +90,8 @@ namespace scribo handle_collisions(image2d<I>& input, scribo::line_set<L>& ls1, scribo::line_set<L>& ls2, - void (*choose)(scribo::line_info<L>&, + void (*choose)(image2d<I>&, + scribo::line_info<L>&, scribo::line_info<L>&)) { mln::trace::entering("scribo::inverse_video::handle_collisions"); @@ -158,7 +159,7 @@ namespace scribo || !l_tested.is_valid() || l_tested.is_hidden()) continue; - choose(ls(l), l_tested); + choose(input, ls(l), l_tested); if (ls(l).is_hidden()) break; diff --git a/scribo/scribo/inverse_video/nb_comps.hh b/scribo/scribo/inverse_video/nb_comps.hh new file mode 100644 index 0000000..e067d1a --- /dev/null +++ b/scribo/scribo/inverse_video/nb_comps.hh @@ -0,0 +1,103 @@ +// Copyright (C) 2011 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_INVERSE_VIDEO_NB_COMPS_HH +# define SCRIBO_INVERSE_VIDEO_NB_COMPS_HH + +/// \file +/// +/// \brief Choose beetwen two lines according to the number of +/// components they have. + +# include <scribo/core/line_set.hh> + +namespace scribo +{ + + namespace inverse_video + { + /// Count the number of big enough components in each line and + /// choose the one which have the most. + /*! + ** \param[in] line The first line. + ** \param[in] line_inverse The second line + ** + ** \return A double : if it is close to 0, it means the second line is + ** more likely to be the right one. As opposite, if it is closer + ** to 1, it means the first one should be the good one. + */ + template <typename L> + double + nb_comps (scribo::line_info<L>& line, + scribo::line_info<L>& line_inverse); + +# ifndef MLN_INCLUDE_ONLY + + template <typename L> + double + nb_comps (scribo::line_info<L>& line, + scribo::line_info<L>& line_inverse) + { + mln::trace::entering("scribo::inverse_video::nb_comps"); + unsigned max_normal = 0; + unsigned nb_comps_normal = 0; + unsigned max_inverse = 0; + unsigned nb_comps_inverse = 0; + + + const scribo::component_set<L> comps_normal = line.holder ().components (); + const scribo::component_set<L> comps_inverse = line_inverse.holder ().components (); + + + for (unsigned i = 0; i < line.component_ids().nelements(); ++i) + if (comps_normal((line.component_ids())[i]).bbox().nsites() > max_normal) + max_normal = comps_normal((line.component_ids())[i]).bbox().nsites(); + + for (unsigned i = 0; i < line_inverse.component_ids().nelements(); ++i) + if (comps_inverse((line_inverse.component_ids())[i]).bbox().nsites() > max_inverse) + max_inverse = comps_inverse((line_inverse.component_ids())[i]).bbox().nsites(); + + + + for (unsigned i = 0; i < line.component_ids().nelements(); ++i) + if (comps_normal((line.component_ids())[i]).bbox().nsites() > max_normal / 30) + ++nb_comps_normal; + + for (unsigned i = 0; i < line_inverse.component_ids().nelements(); ++i) + if (comps_inverse((line_inverse.component_ids())[i]).bbox().nsites() > max_inverse / 30) + ++nb_comps_inverse; + + + mln::trace::exiting("scribo::inverse_video::nb_comps"); + return (double)(nb_comps_normal) / (double)((nb_comps_normal + nb_comps_inverse)); + } + +# endif // ! MLN_INCLUDE_ONLY + + } // end of namespace scribo::inverse_video + +} // end of namespace scribo + +#endif // ! SCRIBO_INVERSE_VIDEO_NB_COMPS_HH diff --git a/scribo/scribo/inverse_video/ratio_area.hh b/scribo/scribo/inverse_video/ratio_area.hh new file mode 100644 index 0000000..db39f36 --- /dev/null +++ b/scribo/scribo/inverse_video/ratio_area.hh @@ -0,0 +1,93 @@ +// Copyright (C) 2011 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_INVERSE_VIDEO_RATIO_AREA_HH +# define SCRIBO_INVERSE_VIDEO_RATIO_AREA_HH + +/// \file +/// +/// \brief Choose beetwen two lines according to the ratio of +/// components area on the total area. + +# include <scribo/core/line_set.hh> + +namespace scribo +{ + + namespace inverse_video + { + /// Choose the line where the ratio of components area is the highest + /*! + ** \param[in] line The first line. + ** \param[in] line_inverse The second line + ** + ** \return A double : if it is close to 0, it means the second line is + ** more likely to be the right one. As opposite, if it is closer + ** to 1, it means the first one should be the good one. + */ + template <typename L> + double + ratio_area (scribo::line_info<L>& line, + scribo::line_info<L>& line_inverse); + +# ifndef MLN_INCLUDE_ONLY + + template <typename L> + double + ratio_area (scribo::line_info<L>& line, + scribo::line_info<L>& line_inverse) + { + mln::trace::entering("scribo::inverse_video::ratio_area"); + + double area_comps_normal = 0; + double area_comps_inverse = 0; + + const scribo::component_set<L> comps_normal = line.holder ().components (); + const scribo::component_set<L> comps_inverse = line_inverse.holder ().components (); + + const mln::util::array<scribo::component_id_t>& comps_id_normal = line.component_ids(); + const mln::util::array<scribo::component_id_t>& comps_id_inverse = line_inverse.component_ids(); + + for (unsigned i = 0; i < comps_id_normal.nelements(); ++i) + area_comps_normal += comps_normal(comps_id_normal[i]).bbox().nsites(); + + for (unsigned i = 0; i < comps_id_inverse.nelements(); ++i) + area_comps_inverse += comps_inverse(comps_id_inverse[i]).bbox().nsites(); + + area_comps_inverse /= line_inverse.bbox().nsites(); + area_comps_normal /= line.bbox().nsites(); + + mln::trace::exiting("scribo::inverse_video::ratio_area"); + + return (area_comps_normal / (area_comps_normal + area_comps_inverse)); + } + +# endif // ! MLN_INCLUDE_ONLY + + } // end of namespace scribo::inverse_video + +} // end of namespace scribo + +#endif // ! SCRIBO_INVERSE_VIDEO_RATIO_AREA_HH -- 1.5.6.5