
URL: https://svn.lrde.epita.fr/svn/oln/trunk/milena/sandbox ChangeLog: 2009-04-21 Warren Seine <soow@lrde.epita.fr> inim: Implement line detection with boxes. Does not actually output the image. * boxes.cc: Add debug code and ugly implementation. --- boxes.cc | 106 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 102 insertions(+), 4 deletions(-) Index: trunk/milena/sandbox/inim/2010/boxes/boxes.cc =================================================================== --- trunk/milena/sandbox/inim/2010/boxes/boxes.cc (revision 3696) +++ trunk/milena/sandbox/inim/2010/boxes/boxes.cc (revision 3697) @@ -32,6 +32,7 @@ #include <mln/debug/println.hh> #include <scribo/text/extract_bboxes.hh> +#include <scribo/text/grouping/group_from_single_link.hh> #include <scribo/text/grouping/group_with_single_left_link.hh> #include <scribo/text/grouping/group_with_single_right_link.hh> #include <scribo/debug/save_linked_textbboxes_image.hh> @@ -47,6 +48,7 @@ #include <mln/io/pgm/save.hh> #include <mln/fun/l2l/wrap.hh> +#include <mln/math/abs.hh> int usage(const char *name) { @@ -54,28 +56,61 @@ return 1; } - -int main(int argc, char* argv[]) -{ using namespace scribo; using namespace mln; +typedef image2d<value::label_16> scribo_image; + +template< typename T > +double vec_y_norm(const T& t) +{ + return mln::math::abs(t[1] / t[0]); +} + +template< typename T > +double bboxes_y_mean(const T& bboxes) +{ + double mean = 0.; + + for (unsigned i = 1; i < bboxes.nelements(); i++) + { + mln_VAR(s, bboxes[i].pmax() - bboxes[i].pmin()); + mean += (s[1] / s[0]); + } + + mean /= static_cast<double>(bboxes.nelements()); + + return mean; +} + +int main(int argc, char* argv[]) +{ if (argc != 3) return usage(argv[0]); scribo::make::internal::debug_filename_prefix = "extract_text_double_link"; + std::cout << "Debug 1" << std::endl; + image2d<bool> input; io::pbm::load(input, argv[1]); + std::cout << "Debug 2" << std::endl; + logical::not_inplace(input); + std::cout << "Debug 3" << std::endl; + value::label_16 nbboxes; scribo::util::text<image2d<value::label_16> > text = text::extract_bboxes(input, c8(), nbboxes); + std::cout << "Debug 4" << std::endl; + text = filter::small_components(text,4); + std::cout << "Debug 5" << std::endl; + mln::util::array<unsigned> left_link = text::grouping::group_with_single_left_link(text, 30), right_link = text::grouping::group_with_single_right_link(text, 30); @@ -83,14 +118,19 @@ std::cout << "BEFORE - nbboxes = " << nbboxes << std::endl; + std::cout << "Debug 6" << std::endl; + scribo::debug::save_linked_textbboxes_image(input, text, left_link, right_link, literal::red, literal::cyan, literal::yellow, literal::green, scribo::make::debug_filename("links.ppm")); + std::cout << "Debug 7" << std::endl; + // Validation. - scribo::util::text<image2d<value::label_16> > grouped_text + + scribo::util::text< scribo_image > grouped_text = text::grouping::group_from_double_link(text, left_link, right_link); std::cout << "AFTER double grouping - nbboxes = " << grouped_text.bboxes().nelements() << std::endl; @@ -101,9 +141,67 @@ fun::l2l::wrap<value::int_u8>()), "tmp.pgm"); + std::cout << "Debug 8" << std::endl; + + + + + + + + mln_VAR(b, grouped_text.bboxes()); + std::cout << "Bounding boxes: " << std::endl << b << std::endl; + + mln_VAR(m, grouped_text.mass_centers()); + std::cout << "Mass centers: " << std::endl << m << std::endl; + + // Mean of the y components of the bounding boxes + double mean = bboxes_y_mean(b); + std::cout << "Mean: " << mean << std::endl; + + mln::util::array< unsigned > lines(m.size()); + + // Suppose that every component is on a different line + for (unsigned i = 1; i < m.size(); i++) + lines[i] = i; + + // Merge the lines if they are too close + for (unsigned i = 1; i < m.size(); i++) + for (unsigned j = i + 1; j < m.size(); j++) + { + if (i == j) + continue; + + double y_norm_diff = vec_y_norm(m[j] - m[i]); +// std::cout << "y_norm_diff: " << y_norm_diff << std::endl; + if (y_norm_diff > 1000) + { + std::cout << "(" << i << "," << j << ")" << std::endl; + continue; + } + if (y_norm_diff < mean * 2) + { + lines[j] = lines[i]; +// std::cout << "Associating " << i << " to " << j << std::endl; + } + } + + std::cout << "Lines: " << std::endl << lines << std::endl; + +// scribo::util::text< scribo_image > grouped_lines +// = text::grouping::group_from_single_link(text, lines); + + + + + + + + scribo::debug::save_textbboxes_image(input, grouped_text.bboxes(), literal::red, scribo::make::debug_filename("boxes.ppm")); + std::cout << "Debug 9" << std::endl; } -- :: Warren Seine // SooW :: :: warren.seine @ gmail.com :: :: EPITA CSI 2010 ::