$extrastylesheet
Olena  User documentation 2.1
An Image Processing Platform
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
graph_image_morpho.cc
1 // Copyright (C) 2007, 2008, 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 
29 
30 #include <mln/accu/shape/bbox.hh>
31 #include <mln/core/alias/box2d.hh>
32 #include <mln/core/alias/point2d.hh>
33 
34 #include <mln/morpho/dilation.hh>
35 #include <mln/morpho/erosion.hh>
36 
38 #include <mln/core/image/vertex_image.hh>
39 #include <mln/fun/i2v/array.hh>
40 #include <mln/util/graph.hh>
41 #include <mln/make/vertex_image.hh>
42 
43 #include <mln/debug/draw_graph.hh>
44 #include <mln/debug/iota.hh>
45 #include <mln/debug/println.hh>
46 
47 
48 int main()
49 {
50  using namespace mln;
51 
52  /*--------.
53  | Graph. |
54  `--------*/
55 
56  /* The graph is as follows:
57 
58  0 1 2 3 4
59  .-----------
60  |
61  0 | 0 2
62  1 | \ / |
63  2 | 1 |
64  3 | \ |
65  4 | 3-4
66 
67  */
68 
69  // Points associated to vertices.
70  typedef fun::i2v::array<point2d> fsite_t;
71  fsite_t sites(5);
72  sites(0) = point2d(0,0); // Point associated to vertex 0.
73  sites(1) = point2d(2,2); // Point associated to vertex 1.
74  sites(2) = point2d(0,4); // Point associated to vertex 2.
75  sites(3) = point2d(4,3); // Point associated to vertex 3.
76  sites(4) = point2d(4,4); // Point associated to vertex 4.
77 
78  // Graph.
79  util::graph g;
80 
81  // Populate the graph with vertices.
82  g.add_vertices(sites.size());
83 
84  // Populate the graph with edges.
85  g.add_edge(0, 1);
86  g.add_edge(1, 2);
87  g.add_edge(1, 3);
88  g.add_edge(3, 4);
89  g.add_edge(4, 2);
90 
91 
92  /*--------------.
93  | Graph image. |
94  `--------------*/
95 
96  // Graph values.
97  typedef fun::i2v::array<unsigned> viota_t;
98  viota_t iota(g.v_nmax());
99  for (unsigned i = 0; i < iota.size(); ++i)
100  iota(i) = 10 + i;
101 
103  ima_t ima = make::vertex_image(g, sites, iota);
104 
105  /*-------------------------------------.
106  | Image representation/visualization. |
107  `-------------------------------------*/
108 
109  // Compute the bounding box of IMA.
110  /* FIXME: mln::graph_image should automatically feature a bbox when
111  its parameter P is akin to a point. */
113  for (unsigned i = 0; i < sites.size(); ++i)
114  a.take(sites(i));
115  box2d bbox = a.to_result();
116  // Print the image.
117  /* FIXME: Unfortunately, displaying graph images is not easy right
118  now (2008-02-05). We could use
119 
120  debug::println(ima);
121 
122  but there's not specialization working for graph_image; the one
123  selected by the compiler is based on a 2-D bbox, and expects the
124  interface of graph_image to work with points (not psites).
125  Moreover, this implementation only shows *values*, not the graph
126  itslef.
127 
128  An alternative is to use draw::graph (which, again, is misnamed),
129  but it doesn't show the values, only the vertices and edges of the
130  graph.
131 
132  The current solution is a mix between draw::graph and hand-made
133  iterations. */
134  image2d<int> ima_rep(bbox);
135 
136  /*--------------------------.
137  | Processing graph images. |
138  `--------------------------*/
139 
140  // Elementary window of a vertex.
141  ima_t::win_t win;
142 
143  ima_t ima_dil = morpho::dilation(ima, win);
144  debug::draw_graph(ima_rep, ima_dil.domain(), pw::cst(9), pw::cst(2));
145  debug::println(ima_rep);
146 
147  ima_t ima_ero = morpho::erosion(ima, win);
148  debug::draw_graph(ima_rep, ima_ero.domain(), pw::cst(9), pw::cst(2));
149  debug::println(ima_rep);
150 }