$extrastylesheet
Olena  User documentation 2.1
An Image Processing Platform
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
line_graph_image_wst.cc
1 // Copyright (C) 2007, 2008, 2009, 2011 EPITA Research and Development
2 // Laboratory (LRDE)
3 //
4 // This file is part of Olena.
5 //
6 // Olena is free software: you can redistribute it and/or modify it under
7 // the terms of the GNU General Public License as published by the Free
8 // Software Foundation, version 2 of the License.
9 //
10 // Olena is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 // General Public License for more details.
14 //
15 // 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 // file, or you compile this file and link it with other files to produce
22 // an executable, this file does not by itself cause the resulting
23 // executable to be covered by the GNU General Public License. This
24 // exception does not however invalidate any other reasons why the
25 // executable file might be covered by the GNU General Public License.
26 
30 
31 #include <mln/core/alias/point2d.hh>
32 
33 #include <mln/util/graph.hh>
34 #include <mln/fun/i2v/array.hh>
35 #include <mln/util/site_pair.hh>
36 #include <mln/core/image/edge_image.hh>
37 
38 #include <mln/morpho/watershed/flooding.hh>
39 
40 
41 int main()
42 {
43  using namespace mln;
44 
45  /*-------------.
46  | Line graph. |
47  `-------------*/
48 
49  /* This graph is from Jean Cousty's PhD thesis, page 76.
50 
51  0 1 2 3 (rows)
52  ,------------------------
53  | 0 10 5
54  0 | o-----o-----o-----o
55  | | | | |
56  | 2| 4| 6| 0|
57  | | | | |
58  1 | o-----o-----o-----o
59  3 5 2
60  (cols)
61 
62  In G, vertices and edges are numbered following in the classical
63  foward order. */
64  util::graph g;
65 
66  // Populate the graph with 8 vertices.
67  g.add_vertices(8);
68 
69  // Populate the graph with edges.
70  g.add_edge(0, 1);
71  g.add_edge(1, 2);
72  g.add_edge(2, 3);
73 
74  g.add_edge(0, 4);
75  g.add_edge(1, 5);
76  g.add_edge(2, 6);
77  g.add_edge(3, 7);
78 
79  g.add_edge(4, 5);
80  g.add_edge(5, 6);
81  g.add_edge(6, 7);
82 
83  // Sites associated to edges.
84  typedef util::site_pair<point2d> P;
85  typedef fun::i2v::array<P> fsite_t;
86  fsite_t sites(10);
87  sites(0) = P(point2d(0,0), point2d(0,1)); // Site associated to edge 0.
88  sites(1) = P(point2d(0,1), point2d(0,2)); // Site associated to edge 1.
89  sites(2) = P(point2d(0,2), point2d(0,3)); // Site associated to edge 2.
90  sites(3) = P(point2d(0,0), point2d(1,0)); // Site associated to edge 7.
91  sites(4) = P(point2d(0,1), point2d(1,1)); // Site associated to edge 8.
92  sites(5) = P(point2d(0,2), point2d(1,2)); // Site associated to edge 9.
93  sites(6) = P(point2d(0,3), point2d(1,3)); // Site associated to edge 3.
94  sites(7) = P(point2d(1,0), point2d(1,1)); // Site associated to edge 4.
95  sites(8) = P(point2d(1,1), point2d(1,2)); // Site associated to edge 5.
96  sites(9) = P(point2d(1,2), point2d(1,3)); // Site associated to edge 6.
97 
98  // Edge values.
99  typedef fun::i2v::array<unsigned> edge_values_t;
100  edge_values_t edge_values(10);
101 
102  static const unsigned values[] = { 0, 10, 5, 2, 4, 6, 0, 3, 5, 2 };
103  for (unsigned i = 0; i < edge_values.size(); ++i)
104  edge_values(i) = values[i];
105 
107  ima_t ima(g, sites, edge_values);
108 
109 
110  /*------------.
111  | Iterators. |
112  `------------*/
113 
114  // Reference values.
115  const unsigned ima_ref[] = { 0, 10, 5, 2, 4, 6, 0, 3, 5, 2 };
116  const unsigned ima_wst[] = { 2, 0, 1, 2, 2, 1, 1, 2, 0, 1 };
117 
118  unsigned i = 0;
119 
120  // Manual iteration over the domain of IMA.
121  mln_piter_(ima_t) p(ima.domain());
122  for_all (p)
123  mln_assertion(ima_ref[i++] == ima(p));
124 
125  // Elementary neighborhood of an edge.
126  typedef ima_t::nbh_t nbh_t;
127  nbh_t nbh;
128 
129  unsigned nbasins;
130  typedef mln_ch_value_(ima_t, unsigned) wshed_t;
131  wshed_t wshed = morpho::watershed::flooding(ima, nbh, nbasins);
132  mln_assertion(nbasins == 2);
133 
134  i = 0;
135  // Manual iteration over the domain of WSHED.
136  mln_piter_(wshed_t) pw(wshed.domain());
137  for_all (pw)
138  mln_assertion(ima_wst[i++] == wshed(pw));
139 }