3926: Make a sample extracting text in a photo.

* src/Makefile.am: add new files. * src/text_in_photo.cc: new sample. * text/grouping/group_with_single_left_link.hh, * text/grouping/group_with_single_right_link.hh, * text/grouping/internal/find_left_link.hh: make them compile and use the new object_image type. --- scribo/ChangeLog | 13 ++ scribo/src/Makefile.am | 2 + scribo/src/text_in_photo.cc | 121 ++++++++++++++++++++ .../text/grouping/group_with_single_left_link.hh | 36 ++++-- .../text/grouping/group_with_single_right_link.hh | 39 ++++--- scribo/text/grouping/internal/find_left_link.hh | 2 +- 6 files changed, 184 insertions(+), 29 deletions(-) create mode 100644 scribo/src/text_in_photo.cc diff --git a/scribo/ChangeLog b/scribo/ChangeLog index 4e49716..2117f69 100644 --- a/scribo/ChangeLog +++ b/scribo/ChangeLog @@ -1,5 +1,18 @@ 2009-05-28 Guillaume Lazzara <lazzara@lrde.epita.fr> + Make a sample extracting text in a photo. + + * src/Makefile.am: add new files. + + * src/text_in_photo.cc: new sample. + + * text/grouping/group_with_single_left_link.hh, + * text/grouping/group_with_single_right_link.hh, + * text/grouping/internal/find_left_link.hh: make them compile and use + the new object_image type. + +2009-05-28 Guillaume Lazzara <lazzara@lrde.epita.fr> + Small fixes in Scribo. * preprocessing/all.hh, diff --git a/scribo/src/Makefile.am b/scribo/src/Makefile.am index 7d02f86..af837ff 100644 --- a/scribo/src/Makefile.am +++ b/scribo/src/Makefile.am @@ -24,6 +24,7 @@ bin_PROGRAMS = \ superpose \ table_rebuild_opening \ table_rebuild_rank \ + text_in_photo \ thin_bboxes dmap_SOURCES = dmap.cc @@ -39,5 +40,6 @@ recognition_SOURCES = recognition.cc superpose_SOURCES = superpose.cc table_rebuild_opening_SOURCES = table_rebuild_opening.cc table_rebuild_rank_SOURCES = table_rebuild_rank.cc +text_in_photo_SOURCES = text_in_photo.cc thin_bboxes_SOURCES = thin_bboxes.cc diff --git a/scribo/src/text_in_photo.cc b/scribo/src/text_in_photo.cc new file mode 100644 index 0000000..94fe84e --- /dev/null +++ b/scribo/src/text_in_photo.cc @@ -0,0 +1,121 @@ +// 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. + +#include <iostream> + +#include <mln/core/image/image2d.hh> +#include <mln/core/alias/neighb2d.hh> + +#include <mln/labeling/colorize.hh> + +#include <mln/io/pbm/all.hh> +#include <mln/io/ppm/save.hh> + +#include <scribo/extract/primitive/objects.hh> +#include <scribo/text/grouping/group_with_single_left_link.hh> +#include <scribo/text/grouping/group_with_single_right_link.hh> +#include <scribo/text/grouping/group_from_double_link.hh> +#include <scribo/filter/small_objects.hh> +#include <scribo/filter/thin_objects.hh> +#include <scribo/filter/thick_objects.hh> + +#include <scribo/make/debug_filename.hh> +#include <scribo/debug/save_textbboxes_image.hh> +#include <scribo/debug/save_linked_textbboxes_image.hh> + +#include <scribo/debug/usage.hh> + +const char *args_desc[][2] = +{ + { "input.pbm", "A binary image. 'True' for objects, 'False'\ +for the background." }, + {0, 0} +}; + + +int main(int argc, char* argv[]) +{ + using namespace scribo; + using namespace mln; + + scribo::make::internal::debug_filename_prefix = "photo"; + + if (argc != 3) + return usage(argv, "Find text in a binarized photo.", "input.pbm output.ppm", + args_desc, "A color image where the text is highlighted."); + + trace::entering("main"); + + image2d<bool> input; + io::pbm::load(input, argv[1]); + + typedef image2d<value::label_16> L; + value::label_16 nobjects; + object_image(L) objects = scribo::extract::primitive::objects(input, c8(), nobjects); + + object_image(L) 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); + + + mln::util::array<unsigned> left_link + = text::grouping::group_with_single_left_link(objects, 30); + mln::util::array<unsigned> right_link + = text::grouping::group_with_single_right_link(objects, 30); + + std::cout << "BEFORE - nobjects = " << nobjects << std::endl; +// scribo::debug::save_linked_textbboxes_image(input, +// filtered_textbboxes, left_link, right_link, +// literal::red, literal::cyan, literal::yellow, +// literal::green, +// scribo::make::debug_filename("links.ppm")); +// +// scribo::debug::save_textbboxes_image(input, filtered_textbboxes.bboxes(), +// literal::red, +// scribo::make::debug_filename("test_graph_filtered_text.ppm")); + object_image(L) grouped_objects + = text::grouping::group_from_double_link(filtered_objects, left_link, right_link); + + std::cout << "AFTER - nobjects = " << grouped_objects.nlabels() << std::endl; + + +// scribo::debug::save_textbboxes_image(input, grouped_textbboxes.bboxes(), +// literal::red, +// scribo::make::debug_filename("test_graph_grouped_text.ppm")); +// + io::ppm::save(mln::labeling::colorize(value::rgb8(), grouped_objects, grouped_objects.nlabels()), + argv[2]); + trace::exiting("main"); +} + diff --git a/scribo/text/grouping/group_with_single_left_link.hh b/scribo/text/grouping/group_with_single_left_link.hh index 7375c93..d0f96e6 100644 --- a/scribo/text/grouping/group_with_single_left_link.hh +++ b/scribo/text/grouping/group_with_single_left_link.hh @@ -32,21 +32,25 @@ /// \file scribo/text/grouping/group_with_single_left_link.hh /// -/// Link text bounding boxes with their left neighbor. +/// Link text objects with their left neighbor. /// /// Merge code with text::grouping::group_with_single_right_link.hh # include <mln/core/concept/image.hh> # include <mln/core/concept/neighborhood.hh> +# include <mln/accu/center.hh> + +# include <mln/labeling/compute.hh> + # include <mln/math/abs.hh> # include <mln/util/array.hh> # include <scribo/core/macros.hh> +# include <scribo/core/object_image.hh> # include <scribo/text/grouping/internal/init_link_array.hh> # include <scribo/text/grouping/internal/find_left_link.hh> -# include <scribo/util/text.hh> //FIXME: not generic. # include <mln/core/alias/dpoint2d.hh> @@ -60,15 +64,19 @@ namespace scribo namespace grouping { - /// Map each character bounding box to its left bounding box neighbor + /// Map each text object to its left bounding box neighbor /// if possible. /// Iterate to the right but link boxes to the left. /// + /// \param[in] objects An object image. + /// \param[in] The maximum distance allowed to seach a neighbor object. + /// /// \return an mln::util::array. Map a bounding box to its left neighbor. + // template <typename L> inline mln::util::array<unsigned> - group_with_single_left_link(const scribo::util::text<L>& text, + group_with_single_left_link(const object_image(L)& objects, unsigned neighb_max_distance); # ifndef MLN_INCLUDE_ONLY @@ -76,25 +84,29 @@ namespace scribo template <typename L> inline mln::util::array<unsigned> - group_with_single_left_link(const scribo::util::text<L>& text, + group_with_single_left_link(const object_image(L)& objects, unsigned neighb_max_distance) { trace::entering("scribo::text::grouping::group_with_single_left_link"); - mln_precondition(text.is_valid()); + mln_precondition(objects.is_valid()); - mln::util::array<unsigned> left_link(text.nbboxes().next()); + mln::util::array<unsigned> left_link(objects.nlabels().next()); internal::init_link_array(left_link); - for_all_ncomponents(i, text.nbboxes()) + mln::util::array<mln_result(accu::center<mln_psite(L)>)> + mass_centers = labeling::compute(accu::meta::center(), + objects, objects.nlabels()); + + for_all_ncomponents(i, objects.nlabels()) { - unsigned midcol = (text.bbox(i).pmax().col() - - text.bbox(i).pmin().col()) / 2; + unsigned midcol = (objects.bbox(i).pmax().col() + - objects.bbox(i).pmin().col()) / 2; int dmax = midcol + neighb_max_distance; - mln_site(L) c = text.mass_center(i); + mln_site(L) c = mass_centers(i); /// Find a neighbor on the left - internal::find_left_link(text, left_link, i, dmax, c); + internal::find_left_link(objects, left_link, i, dmax, c); } trace::exiting("scribo::text::grouping::group_with_single_left_link"); diff --git a/scribo/text/grouping/group_with_single_right_link.hh b/scribo/text/grouping/group_with_single_right_link.hh index 0ad091f..8a11a5e 100644 --- a/scribo/text/grouping/group_with_single_right_link.hh +++ b/scribo/text/grouping/group_with_single_right_link.hh @@ -32,23 +32,25 @@ /// \file scribo/text/grouping/group_with_single_right_link.hh /// -/// Link text bounding boxes with their right neighbor. +/// Link text objects with their right neighbor. /// /// \todo Merge code with text::grouping::group_with_single_right_link.hh # include <mln/core/concept/image.hh> # include <mln/core/concept/neighborhood.hh> +# include <mln/accu/center.hh> + +# include <mln/labeling/compute.hh> + # include <mln/math/abs.hh> # include <mln/util/array.hh> # include <scribo/core/macros.hh> +# include <scribo/core/object_image.hh> # include <scribo/text/grouping/internal/init_link_array.hh> -# include <scribo/text/grouping/internal/update_link_array.hh> -# include <scribo/text/grouping/internal/find_root.hh> # include <scribo/text/grouping/internal/find_right_link.hh> -# include <scribo/util/text.hh> //FIXME: not generic. # include <mln/core/alias/dpoint2d.hh> @@ -62,15 +64,18 @@ namespace scribo namespace grouping { - /// Map each character bounding box to its right bounding box neighbor + /// Map each text object to its right bounding box neighbor /// if possible. /// Iterate to the right but link boxes to the right. /// + /// \param[in] objects An object image. + /// \param[in] The maximum distance allowed to seach a neighbor object. + /// /// \return an mln::util::array. Map a bounding box to its right neighbor. template <typename L> inline mln::util::array<unsigned> - group_with_single_right_link(const scribo::util::text<L>& text, + group_with_single_right_link(const object_image(L)& objects, unsigned neighb_max_distance); # ifndef MLN_INCLUDE_ONLY @@ -78,27 +83,29 @@ namespace scribo template <typename L> inline mln::util::array<unsigned> - group_with_single_right_link(const scribo::util::text<L>& text, + group_with_single_right_link(const object_image(L)& objects, unsigned neighb_max_distance) { trace::entering("scribo::text::grouping::group_with_single_right_link"); - mln_precondition(text.is_valid()); + mln_precondition(objects.is_valid()); - mln::util::array<unsigned> right_link(text.nbboxes().next()); + mln::util::array<unsigned> right_link(objects.nlabels().next()); internal::init_link_array(right_link); - for_all_ncomponents(i, text.nbboxes()) + mln::util::array<mln_result(accu::center<mln_psite(L)>)> + mass_centers = labeling::compute(accu::meta::center(), + objects, objects.nlabels()); + + for_all_ncomponents(i, objects.nlabels()) { - unsigned midcol = (text.bbox(i).pmax().col() - - text.bbox(i).pmin().col()) / 2; + unsigned midcol = (objects.bbox(i).pmax().col() + - objects.bbox(i).pmin().col()) / 2; int dmax = midcol + neighb_max_distance; - mln_site(L) c = text.mass_center(i); + mln_site(L) c = mass_centers(i); - /// /// Find a neighbor on the right - /// - internal::find_right_link(text, right_link, i, dmax, c); + internal::find_right_link(objects, right_link, i, dmax, c); } trace::exiting("scribo::text::grouping::group_with_single_right_link"); diff --git a/scribo/text/grouping/internal/find_left_link.hh b/scribo/text/grouping/internal/find_left_link.hh index f598115..e3a6b4b 100644 --- a/scribo/text/grouping/internal/find_left_link.hh +++ b/scribo/text/grouping/internal/find_left_link.hh @@ -39,7 +39,7 @@ # include <mln/util/array.hh> -# include <scribo/util/text.hh> +# include <scribo/core/object_image.hh> # include <scribo/text/grouping/internal/update_link_array.hh> //FIXME: not generic. -- 1.5.6.5
participants (1)
-
Guillaume Lazzara