$extrastylesheet
Olena  User documentation 2.1
An Image Processing Platform
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
group_from_double_link_filter_non_aligned.cc
1 // Copyright (C) 2009 EPITA Research and Development Laboratory (LRDE)
2 //
3 // This file is part of Olena.
4 //
5 // Olena is free software: you can redistribute it and/or modify it under
6 // the terms of the GNU General Public License as published by the Free
7 // Software Foundation, version 2 of the License.
8 //
9 // Olena is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 // General Public License for more details.
13 //
14 // You should have received a copy of the GNU General Public License
15 // along with Olena. If not, see <http://www.gnu.org/licenses/>.
16 //
17 // As a special exception, you may use this file as part of a free
18 // software project without restriction. Specifically, if other files
19 // instantiate templates or use macros or inline functions from this
20 // file, or you compile this file and link it with other files to produce
21 // an executable, this file does not by itself cause the resulting
22 // executable to be covered by the GNU General Public License. This
23 // exception does not however invalidate any other reasons why the
24 // executable file might be covered by the GNU General Public License.
25 
26 #include <iostream>
27 
28 #include <mln/core/image/image2d.hh>
29 #include <mln/labeling/colorize.hh>
30 #include <mln/debug/println.hh>
31 #include <mln/util/array.hh>
32 #include <mln/literal/colors.hh>
33 #include <mln/io/pbm/load.hh>
34 #include <mln/core/alias/neighb2d.hh>
35 #include <mln/value/label_16.hh>
36 
37 #include <scribo/core/object_image.hh>
38 
39 #include <scribo/primitive/extract/objects.hh>
40 #include <scribo/primitive/link/with_single_left_link.hh>
41 #include <scribo/primitive/link/with_single_right_link.hh>
42 #include <scribo/debug/save_linked_bboxes_image.hh>
43 #include <scribo/primitive/group/from_double_link.hh>
44 #include <scribo/primitive/group/from_single_link.hh>
45 #include <scribo/primitive/link/merge_double_link.hh>
46 #include <scribo/primitive/group/apply.hh>
47 #include <scribo/filter/objects_small.hh>
48 
49 #include <scribo/filter/object_links_non_h_aligned.hh>
50 
51 #include <scribo/debug/save_bboxes_image.hh>
52 #include <scribo/make/debug_filename.hh>
53 
54 
55 #include <scribo/debug/usage.hh>
56 
57 const char *args_desc[][2] =
58 {
59  { "input.pbm", "A binary image. 'True' for objects, 'False'\
60 for the background." },
61  { "hlmax", "Maximum distance between two grouped objects while browsing on the left." },
62  { "hrmax", "Maximum distance between two grouped objects while browsing on the right." },
63  { "hdelta_max", "Maximum horizontal distance between top/bottom corner of two grouped bboxes." },
64  { "prefix", "Output names prefix" },
65  {0, 0}
66 };
67 
68 int main(int argc, char *argv[])
69 {
70  using namespace scribo;
71  using namespace mln;
72 
73  if (argc != 6)
74  return scribo::debug::usage(argv,
75  "Group potential text objects using a double validation link.",
76  "input.pbm hlmax hrmax hdelta_max prefix",
77  args_desc,
78  "Several images showing the process.");
79 
80 
81  scribo::make::internal::debug_filename_prefix = argv[5];
82 
83  image2d<bool> input;
84  io::pbm::load(input, argv[1]);
85 
86  value::label_16 nbboxes;
87  typedef image2d<value::label_16> L;
88  typedef object_image(L) text_t;
89 
90  text_t text = primitive::extract::objects(input, c8(), nbboxes);
91 
92  text = filter::objects_small(text, 4);
93 
94  object_links<L> left_link
95  = primitive::link::with_single_left_link(text, atoi(argv[2]));
96  object_links<L> right_link
97  = primitive::link::with_single_right_link(text, atoi(argv[3]));
98 
99  std::cout << "BEFORE - nbboxes = " << nbboxes << std::endl;
100 
101 // scribo::debug::save_linked_textbboxes_image(input,
102 // text, left_link,
103 // literal::red, literal::cyan,
104 // scribo::make::debug_filename("left_linked.ppm"));
105 // scribo::debug::save_linked_textbboxes_image(input,
106 // text, right_link,
107 // literal::red, literal::cyan,
108 // scribo::make::debug_filename("right_linked.ppm"));
109 
110  scribo::debug::save_linked_bboxes_image(input,
111  text, left_link, right_link,
112  literal::red, literal::cyan, literal::yellow,
113  literal::green,
114  scribo::make::debug_filename("links.ppm"));
115 
116  // With validation.
117  object_links<L> double_link
118  = primitive::link::merge_double_link(text,
119  left_link,
120  right_link);
121 
122 
123  text_t
124  grouped_text = primitive::group::apply(text, double_link);
125  std::cout << "AFTER double grouping - nbboxes = "
126  << grouped_text.bboxes().nelements() << std::endl;
127 
128 
129  io::ppm::save(mln::labeling::colorize(value::rgb8(),
130  grouped_text,
131  grouped_text.nlabels()),
132  scribo::make::debug_filename("label_color.ppm"));
133 
134 
135  scribo::debug::save_bboxes_image(input, grouped_text.bboxes(),
136  literal::red,
137  scribo::make::debug_filename("bboxes.ppm"));
138 
139 
140 
141  // Filter non-correctly aligned grouped objects.
142 
143  object_links<L> filtered_links
144  = filter::object_links_non_h_aligned(text, double_link, atof(argv[4]));
145 
146 
147  text_t aligned_grouped_text
148  = primitive::group::apply(text, filtered_links);
149 
150 
151  std::cout << "AFTER filtering - Links may have been canceled - nbboxes = "
152  << aligned_grouped_text.bboxes().nelements() << std::endl;
153 
154 
155  io::ppm::save(mln::labeling::colorize(value::rgb8(),
156  aligned_grouped_text,
157  aligned_grouped_text.nlabels()),
158  scribo::make::debug_filename("label_color-filtered.ppm"));
159 
160 
161  scribo::debug::save_bboxes_image(input, aligned_grouped_text.bboxes(),
162  literal::red,
163  scribo::make::debug_filename("bboxes-filtered.ppm"));
164 
165 }