4393: New object grouping routine based on a rag.

* text/grouping/group_with_rag.hh, * text/grouping/internal/have_link_valid.hh: New. * src/text/grouping/group_from_rag.cc: New related example. --- scribo/ChangeLog | 9 + scribo/src/text/grouping/group_from_rag.cc | 240 ++++++++++++++++++++++ scribo/text/grouping/group_with_rag.hh | 99 +++++++++ scribo/text/grouping/internal/have_link_valid.hh | 83 ++++++++ 4 files changed, 431 insertions(+), 0 deletions(-) create mode 100644 scribo/src/text/grouping/group_from_rag.cc create mode 100644 scribo/text/grouping/group_with_rag.hh create mode 100644 scribo/text/grouping/internal/have_link_valid.hh diff --git a/scribo/ChangeLog b/scribo/ChangeLog index 472640e..23c8c0d 100644 --- a/scribo/ChangeLog +++ b/scribo/ChangeLog @@ -1,5 +1,14 @@ 2009-08-24 Guillaume Lazzara <lazzara@lrde.epita.fr> + New object grouping routine based on a rag. + + * text/grouping/group_with_rag.hh, + * text/grouping/internal/have_link_valid.hh: New. + + * src/text/grouping/group_from_rag.cc: New related example. + +2009-08-24 Guillaume Lazzara <lazzara@lrde.epita.fr> + * src/text_in_photo.cc: Improve results quality. 2009-08-24 Guillaume Lazzara <lazzara@lrde.epita.fr> diff --git a/scribo/src/text/grouping/group_from_rag.cc b/scribo/src/text/grouping/group_from_rag.cc new file mode 100644 index 0000000..944624c --- /dev/null +++ b/scribo/src/text/grouping/group_from_rag.cc @@ -0,0 +1,240 @@ +// 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. + +#include <iostream> + +#include <mln/core/var.hh> +#include <mln/core/image/image2d.hh> +#include <mln/core/image/edge_image.hh> +#include <mln/core/image/vertex_image.hh> +#include <mln/core/image/dmorph/image_if.hh> +#include <mln/value/label_16.hh> +#include <mln/io/ppm/save.hh> +#include <mln/io/pbm/load.hh> +#include <mln/core/alias/neighb2d.hh> +#include <mln/literal/colors.hh> +#include <mln/labeling/colorize.hh> +#include <mln/labeling/compute.hh> +#include <mln/make/p_vertices_with_mass_centers.hh> +#include <mln/make/edge_image.hh> +#include <mln/make/vertex_image.hh> +#include <mln/debug/draw_graph.hh> +#include <mln/util/graph.hh> +#include <mln/accu/center.hh> + +#include <scribo/extract/primitive/objects.hh> +#include <scribo/text/grouping/group_with_rag.hh> +//#include <scribo/text/grouping/group_from_rag.hh> + +#include <scribo/filter/small_objects.hh> +#include <scribo/filter/thin_objects.hh> +#include <scribo/filter/thick_objects.hh> + + +#include <scribo/fun/v2b/small_objects_filter.hh> +#include <scribo/debug/save_bboxes_image.hh> +#include <scribo/debug/save_linked_bboxes_image.hh> +#include <scribo/make/debug_filename.hh> + +int usage(const char *name) +{ + std::cout << "Usage: " << name << " <input.pbm> " << std::endl; + return 1; +} + + + +namespace scribo +{ + + using namespace mln; + + + namespace graph + { + + template <typename A, typename G, typename I> + inline + vertex_image<void,mln_result(A),G> + compute_vertex(const Accumulator<A>& accu, + const Graph<G>& g_, + const Image<I>& lbl_, + const mln_value(I)& nlabels) + { + const G& g = exact(g_); + const I& lbl = exact(lbl_); + + mln_precondition(g.is_valid()); + mln_precondition(lbl.is_valid()); + + util::array<mln_result(A)> + values = labeling::compute(accu, lbl_, nlabels); + + vertex_image<void, mln_result(A), G> + v_ima = mln::make::vertex_image(g, values); + + return v_ima; + } + + } // end of namespace scribo::graph + + + template <typename I> + struct edge_color : mln::Function_v2b< edge_color<I> > + { + typedef value::rgb8 result; + + edge_color(const Image<I>& mask) : mask_(exact(mask)) {} + + value::rgb8 operator()(const unsigned id) const + { + if (mask_(id)) + return literal::green; + return literal::red; + } + + I mask_; + }; + + + /// Compute a distance between two rgb8 values. + struct dist : Function_vv2v< dist > + { + + typedef bool result; + + bool operator()(const point2d& p1, const point2d& p2) const + { + return (math::sqrt(math::sqr(p1.row() - p2.row()) + + math::sqr(p1.col() - p2.col()))) < 5; + } + + }; + + + namespace filter + { + + template <typename P, typename V, typename G, typename F, typename FP> + edge_image<void,bool,G> + graph_edges(const vertex_image<P,V,G>& v_ima, + const Function<F>& edge_values, + const Function<FP>& predicate) + { + typedef edge_image<void,bool,util::graph> e_filter_t; + e_filter_t e_filter = mln::make::edge_image(v_ima, dist()); + + + } + + } // end of namespace scribo::filter + + +} + + + +int main(int argc, char* argv[]) +{ + using namespace scribo; + using namespace mln; + + if (argc != 2) + return usage(argv[0]); + + scribo::make::internal::debug_filename_prefix = "group_with_rag"; + + image2d<bool> input; + io::pbm::load(input, argv[1]); + + + typedef image2d<value::label_16> L; + value::label_16 nbboxes; + typedef object_image(L) objects_t; + objects_t objects = extract::primitive::objects(input, c8(), nbboxes); + + /// First filtering. + objects_t filtered_objects + = scribo::filter::small_objects(objects, 6); + + filtered_objects + = scribo::filter::thin_objects(filtered_objects, 3); + + filtered_objects + = scribo::filter::thick_objects(filtered_objects, + math::min(input.ncols(), input.nrows()) / 6); + + + /// Getting objects links from a Region Adjacency graph. + mln_VAR(rag_data, text::grouping::group_with_rag(filtered_objects, c8())); + + + mln_VAR(v_ima, scribo::graph::compute_vertex(accu::center<point2d>(), + rag_data.first(), + filtered_objects, + filtered_objects.nlabels())); + + //FOR DEBUGGING PURPOSE + { + image2d<value::rgb8> + before_grouping = data::convert(value::rgb8(), input); + + scribo::draw::bounding_boxes(before_grouping, + filtered_objects.bboxes(), + literal::blue); + + mln_VAR(pv, mln::make::p_vertices_with_mass_centers(filtered_objects, rag_data.first())); + mln::debug::draw_graph(before_grouping, pv, literal::green, literal::green); + + io::ppm::save(before_grouping, + scribo::make::debug_filename("before_grouping.ppm")); + } + + + + typedef edge_image<void,bool,util::graph> e_filter_t; + e_filter_t e_filter = mln::make::edge_image(v_ima, dist()); + + + + + //FOR DEBUGGING PURPOSE +// { +// image2d<value::rgb8> +// after_grouping = data::convert(value::rgb8(), input); + + +// scribo::draw::bounding_boxes(after_grouping, +// filtered_objects.bboxes(), +// literal::blue); + +// mln::debug::draw_graph(after_grouping, v_ima.domain(), +// pw::cst(literal::black), edge_color<e_filter_t>(e_filter)); + +// io::ppm::save(after_grouping, "after_grouping.ppm"); +// } +} + + diff --git a/scribo/text/grouping/group_with_rag.hh b/scribo/text/grouping/group_with_rag.hh new file mode 100644 index 0000000..c995335 --- /dev/null +++ b/scribo/text/grouping/group_with_rag.hh @@ -0,0 +1,99 @@ +// 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_TEXT_GROUPING_GROUP_WITH_RAG_HH +# define SCRIBO_TEXT_GROUPING_GROUP_WITH_RAG_HH + + +/// \file +/// +/// Group objects with a region adjacency graph. + +# include <mln/core/concept/neighborhood.hh> + +# include <mln/util/graph.hh> +# include <mln/util/couple.hh> + +# include <mln/transform/influence_zone_geodesic.hh> + +# include <mln/make/influence_zone_adjacency_graph.hh> + + +# include <scribo/core/macros.hh> +# include <scribo/core/object_image.hh> + + +namespace scribo +{ + + namespace text + { + + namespace grouping + { + + using namespace mln; + + + template <typename L, typename N> + util::couple<mln::util::graph, mln_concrete(L)> + group_with_rag(const object_image(L)& objects, + const Neighborhood<N>& nbh); + + +# ifndef MLN_INCLUDE_ONLY + + + template <typename L, typename N> + util::couple<mln::util::graph, mln_concrete(L)> + group_with_rag(const object_image(L)& objects, + const Neighborhood<N>& nbh) + { + trace::entering("scribo::text::grouping::group_with_rag"); + + mln_precondition(objects.is_valid()); + + mln_concrete(L) + iz = transform::influence_zone_geodesic(objects, nbh); + + mln::util::graph + g = mln::make::influence_zone_adjacency_graph(iz, + nbh, + objects.nlabels()); + + trace::exiting("scribo::text::grouping::group_with_rag"); + return make::couple(g, iz); + } + + +# endif // ! MLN_INCLUDE_ONLY + + } // end of namespace scribo::text::grouping + + } // end of namespace scribo::text + +} // end of namespace scribo + +#endif // ! SCRIBO_TEXT_GROUPING_GROUP_WITH_RAG_HH diff --git a/scribo/text/grouping/internal/have_link_valid.hh b/scribo/text/grouping/internal/have_link_valid.hh new file mode 100644 index 0000000..bdc6f18 --- /dev/null +++ b/scribo/text/grouping/internal/have_link_valid.hh @@ -0,0 +1,83 @@ +// 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_TEXT_GROUPING_INTERNAL_HAVE_LINK_VALID_HH +# define SCRIBO_TEXT_GROUPING_INTERNAL_HAVE_LINK_VALID_HH + +/// \file +/// +/// Tells whether a component have at least one valid link + + +# include <mln/util/array.hh> +# include <mln/util/couple.hh> + +namespace scribo +{ + + namespace text + { + + namespace grouping + { + + namespace internal + { + + /// Tells whether a component have at least one valid link link. + /// + /// \param[in] left_link Left link of components. + /// \param[in] right_link Right link of components. + /// \param[in] i The component id. + /// + /// \return True if the \p i-th component has at least one + /// valid link. + bool + have_link_valid(const mln::util::array<unsigned>& left_link, + const mln::util::array<unsigned>& right_link, + unsigned i); + +# ifndef MLN_INCLUDE_ONLY + + bool + have_link_valid(const mln::util::array<unsigned>& left_link, + const mln::util::array<unsigned>& right_link, + unsigned i) + { + return (right_link[left_link[i]] == i && left_link[i] != i) + || (left_link[right_link[i]] == i && right_link[i] != i); + } + +# endif // ! MLN_INCLUDE_ONLY + + } // end of namespace scribo::text::grouping::internal + + } // end of namespace scribo::text::grouping + + } // end of namespace scribo::text + +} // end of namespace scribo + +#endif // ! SCRIBO_TEXT_GROUPING_INTERNAL_HAVE_LINK_VALID_HH -- 1.5.6.5
participants (1)
-
Guillaume Lazzara