group_from_double_link.cc 4 KB
Newer Older
1
2
// Copyright (C) 2009, 2010 EPITA Research and Development Laboratory
// (LRDE)
3
//
4
// This file is part of Olena.
5
//
6
7
8
9
10
// 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,
11
12
13
14
15
// 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
16
// along with Olena.  If not, see <http://www.gnu.org/licenses/>.
17
18
//
// As a special exception, you may use this file as part of a free
19
// software project without restriction.  Specifically, if other files
20
// instantiate templates or use macros or inline functions from this
21
22
23
24
25
// 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.
26
27
28

#include <iostream>

29
#include <mln/core/image/image2d.hh>
30
#include <mln/labeling/colorize.hh>
Guillaume Lazzara's avatar
Guillaume Lazzara committed
31
#include <mln/debug/println.hh>
32
33
34
35
#include <mln/util/array.hh>
#include <mln/literal/colors.hh>
#include <mln/io/pbm/load.hh>
#include <mln/core/alias/neighb2d.hh>
36
#include <mln/value/label_16.hh>
37

38
#include <scribo/core/object_links.hh>
Guillaume Lazzara's avatar
Guillaume Lazzara committed
39

40
#include <scribo/primitive/extract/components.hh>
41
42
43
#include <scribo/primitive/group/apply.hh>
#include <scribo/primitive/link/with_single_left_link.hh>
#include <scribo/primitive/link/with_single_right_link.hh>
Guillaume Lazzara's avatar
Guillaume Lazzara committed
44
#include <scribo/debug/save_linked_bboxes_image.hh>
45
#include <scribo/primitive/group/from_double_link.hh>
Guillaume Lazzara's avatar
Guillaume Lazzara committed
46
#include <scribo/filter/objects_small.hh>
47

Guillaume Lazzara's avatar
Guillaume Lazzara committed
48
#include <scribo/debug/save_bboxes_image.hh>
Guillaume Lazzara's avatar
Guillaume Lazzara committed
49
#include <scribo/make/debug_filename.hh>
50
51


Guillaume Lazzara's avatar
Guillaume Lazzara committed
52
#include <scribo/debug/usage.hh>
53

Guillaume Lazzara's avatar
Guillaume Lazzara committed
54
55
const char *args_desc[][2] =
{
56
57
58
59
60
61
  { "input.pbm", "A binary image. 'True' for objects, 'False' for the "
    "background." },
  { "hlmax", "Maximum distance between two grouped objects while browsing "
    "on the left." },
  { "hrmax", "Maximum distance between two grouped objects while browsing "
    "on the right." },
Guillaume Lazzara's avatar
Guillaume Lazzara committed
62
63
64
65
66
  { "prefix", "Output names prefix" },
  {0, 0}
};

int main(int argc, char *argv[])
67
68
69
70
{
  using namespace scribo;
  using namespace mln;

Guillaume Lazzara's avatar
Guillaume Lazzara committed
71
72
  if (argc != 5)
    return scribo::debug::usage(argv,
73
74
				"Group potential text objects using a double"
				"validation link.",
Guillaume Lazzara's avatar
Guillaume Lazzara committed
75
				"input.pbm hlmax hrmax prefix",
76
				args_desc);
Guillaume Lazzara's avatar
Guillaume Lazzara committed
77

78

Guillaume Lazzara's avatar
Guillaume Lazzara committed
79
  scribo::make::internal::debug_filename_prefix = argv[4];
Guillaume Lazzara's avatar
Guillaume Lazzara committed
80

81
82
83
84
  image2d<bool> input;
  io::pbm::load(input, argv[1]);

  value::label_16 nbboxes;
85
  typedef image2d<value::label_16> L;
86
87
  component_set<L>
    text = primitive::extract::components(input, c8(), nbboxes);
88

89
  text = filter::components_small(text, 4);
Guillaume Lazzara's avatar
Guillaume Lazzara committed
90

91
92
93
94
  object_links<L> left_link
    = primitive::link::with_single_left_link(text, atoi(argv[2]));
  object_links<L> right_link
    = primitive::link::with_single_right_link(text, atoi(argv[3]));
95
96
97

  std::cout << "BEFORE - nbboxes = " << nbboxes << std::endl;

Guillaume Lazzara's avatar
Guillaume Lazzara committed
98
  scribo::debug::save_linked_bboxes_image(input,
99
					  left_link, right_link,
Guillaume Lazzara's avatar
Guillaume Lazzara committed
100
101
					  literal::red, literal::cyan, literal::yellow,
					  literal::green,
102
					  anchor::MassCenter,
Guillaume Lazzara's avatar
Guillaume Lazzara committed
103
					  scribo::make::debug_filename("links.ppm"));
Guillaume Lazzara's avatar
Guillaume Lazzara committed
104

105
  // With validation.
106
  object_groups<L> groups
107
    = primitive::group::from_double_link(left_link, right_link);
108

109
  component_set<L> grouped_text = primitive::group::apply(groups);
Guillaume Lazzara's avatar
Guillaume Lazzara committed
110

111
  io::ppm::save(mln::labeling::colorize(value::rgb8(),
112
113
					grouped_text.labeled_image(),
					grouped_text.nelements()),
Guillaume Lazzara's avatar
Guillaume Lazzara committed
114
		scribo::make::debug_filename("label_color.ppm"));
115

116
117
  std::cout << "AFTER double grouping - nbboxes = "
	    << grouped_text.nelements() << std::endl;
118

119
120
121
  scribo::debug::save_bboxes_image(input, grouped_text,
				   scribo::make::debug_filename("bboxes.ppm"),
				   literal::red);
122
123

}