last-svn-commit-52-g56ab608 Add a small tool to recognize text in a binary image.

* src/text/Makefile.am: New target. * src/text/pbm_recognition.cc: New. * text/recognition.hh: Add an overload taking an image as argument. --- scribo/ChangeLog | 11 +++ scribo/src/text/Makefile.am | 10 ++ .../unskew.cc => text/pbm_recognition.cc} | 31 ++++--- scribo/text/recognition.hh | 96 +++++++++++++++----- 4 files changed, 113 insertions(+), 35 deletions(-) copy scribo/src/{preprocessing/unskew.cc => text/pbm_recognition.cc} (73%) diff --git a/scribo/ChangeLog b/scribo/ChangeLog index 4626c4d..b29f31e 100644 --- a/scribo/ChangeLog +++ b/scribo/ChangeLog @@ -1,5 +1,16 @@ 2010-02-19 Guillaume Lazzara <z@lrde.epita.fr> + Add a small tool to recognize text in a binary image. + + * src/text/Makefile.am: New target. + + * src/text/pbm_recognition.cc: New. + + * text/recognition.hh: Add an overload taking an image as + argument. + +2010-02-19 Guillaume Lazzara <z@lrde.epita.fr> + Add a subsampling algorithm. * subsampling/integral.hh: New. Compute a subsample image and an diff --git a/scribo/src/text/Makefile.am b/scribo/src/text/Makefile.am index b84e0dd..aab9b19 100644 --- a/scribo/src/text/Makefile.am +++ b/scribo/src/text/Makefile.am @@ -38,5 +38,15 @@ if HAVE_TIFF $(TESSERACT_LDFLAGS) \ $(TIFF_LDFLAGS) \ -lpthread + + bin_PROGRAMS += pbm_recognition + pbm_recognition_SOURCES = pbm_recognition.cc + pbm_recognition_CPPFLAGS = $(AM_CPPFLAGS) \ + $(TESSERACT_CPPFLAGS) \ + $(TIFF_CPPFLAGS) + pbm_recognition_LDFLAGS = $(AM_LDFLAGS) \ + $(TESSERACT_LDFLAGS) \ + $(TIFF_LDFLAGS) \ + -lpthread endif HAVE_TIFF endif HAVE_TESSERACT diff --git a/scribo/src/preprocessing/unskew.cc b/scribo/src/text/pbm_recognition.cc similarity index 73% copy from scribo/src/preprocessing/unskew.cc copy to scribo/src/text/pbm_recognition.cc index b8c1979..70ceca1 100644 --- a/scribo/src/preprocessing/unskew.cc +++ b/scribo/src/text/pbm_recognition.cc @@ -1,4 +1,4 @@ -// Copyright (C) 2009 EPITA Research and Development Laboratory (LRDE) +// Copyright (C) 2010 EPITA Research and Development Laboratory (LRDE) // // This file is part of Olena. // @@ -23,36 +23,43 @@ // 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/io/pbm/all.hh> +#include <mln/io/pbm/load.hh> + +#include <scribo/text/recognition.hh> -#include <scribo/preprocessing/unskew.hh> #include <scribo/debug/usage.hh> const char *args_desc[][2] = { - { "input.pbm", "A binary image. 'True' for objects, 'false' for background." }, + { "input.pbm", "A binary image. 'True' for objects, 'False'\ +for the background." }, {0, 0} }; -int main(int argc, char *argv[]) +int main(int argc, char* argv[]) { - mln::trace::entering("main"); + using namespace scribo; using namespace mln; - if (argc != 3) + if (argc != 2) return scribo::debug::usage(argv, - "Unskew a binary image", - "input.pbm output.pbm", - args_desc, "A binary image."); + "Text recognition", + "input.pbm", + args_desc, + "The text is printed on the standard output"); + + trace::entering("main"); image2d<bool> input; io::pbm::load(input, argv[1]); - io::pbm::save(scribo::preprocessing::unskew(input).first(), argv[2]); + scribo::text::recognition(input, "fra"); - mln::trace::exiting("main"); + trace::exiting("main"); } diff --git a/scribo/text/recognition.hh b/scribo/text/recognition.hh index c4dd6e2..364e56b 100644 --- a/scribo/text/recognition.hh +++ b/scribo/text/recognition.hh @@ -39,41 +39,28 @@ # include <mln/core/image/dmorph/image_if.hh> # include <mln/core/concept/neighborhood.hh> # include <mln/core/site_set/box.hh> + # include <mln/util/array.hh> -# include <mln/labeling/blobs.hh> # include <mln/data/fill.hh> +# include <mln/data/paste.hh> # include <mln/pw/all.hh> -# include <mln/transform/distance_front.hh> - -# include <mln/morpho/thickening.hh> -# include <mln/morpho/dilation.hh> - # include <mln/core/alias/w_window2d_int.hh> # include <mln/make/w_window2d_int.hh> # include <mln/border/resize.hh> -# include <mln/win/rectangle2d.hh> -# include <mln/win/disk2d.hh> -# include <mln/win/octagon2d.hh> - -# include <mln/debug/put_word.hh> - # include <scribo/core/macros.hh> # include <scribo/util/text.hh> # include <scribo/text/clean.hh> -# include <tesseract/baseapi.h> +# include <scribo/core/line_set.hh> -#include <mln/labeling/colorize.hh> - +# include <tesseract/baseapi.h> -#include <mln/debug/filename.hh> -#include <mln/io/pbm/save.hh> namespace scribo @@ -96,7 +83,15 @@ namespace scribo void recognition(const line_set<L>& lines, const char *language, - const char *output_file); + const char *output_file = 0); + + + /// Recognize text from an image. + template <typename I> + void + recognition(const Image<I>& line, + const char *language, + const char *output_file = 0); @@ -109,7 +104,7 @@ namespace scribo void recognition(const line_set<L>& lines, const char *language, - const char *output_file) + const char *output_file = 0) { trace::entering("scribo::text::recognition"); @@ -182,10 +177,6 @@ namespace scribo text_ima_cleaned.nrows()); // n rows - - mln_site(L) p = lines(i).bbox().pcenter(); - p.col() -= (lines(i).bbox().pmax().col() - - lines(i).bbox().pmin().col()) / 2; if (s != 0) { std::cerr << s << std::endl; @@ -204,6 +195,65 @@ namespace scribo } + template <typename I> + void + recognition(const Image<I>& line_, + const char *language, + const char *output_file = 0) + { + trace::entering("scribo::text::recognition"); + + const I& line = exact(line_); + mln_precondition(line.is_valid()); + + // Initialize Tesseract. + TessBaseAPI::InitWithLanguage(NULL, NULL, language, NULL, false, 0, NULL); + + std::ofstream file; + if (output_file != 0) + file.open(output_file); + + mln_domain(I) box = line.domain(); + // Make sure characters are isolated from the borders. + // Help Tesseract. + box.enlarge(2); + + I text_ima(box); + data::fill(text_ima, false); + data::paste(line, text_ima); + + // Make sure there is no border. + border::resize(text_ima, 0); + + // Recognize characters. + char* s = TessBaseAPI::TesseractRect( + (unsigned char*) text_ima.buffer(), + sizeof (bool), // Pixel size. + text_ima.ncols() * sizeof (bool), // Row_offset + 0, // Left + 0, // Top + text_ima.ncols(), // n cols + text_ima.nrows()); // n rows + + + if (s != 0) + { + std::cout << s << std::endl; + if (output_file != 0) + file << line.domain() << " " << s << std::endl; + } + + // The string has been allocated by Tesseract. We must free it. + free(s); + + if (output_file != 0) + file.close(); + + trace::exiting("scribo::text::recognition"); + } + + + # endif // ! MLN_INCLUDE_ONLY } // end of namespace scribo::text -- 1.5.6.5
participants (1)
-
Guillaume Lazzara