$extrastylesheet
Olena  User documentation 2.1
An Image Processing Platform
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
complex_image_morpho.cc
1 // Copyright (C) 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 <iostream>
31 
32 #include <mln/value/int_u8.hh>
33 #include <mln/core/alias/point2d.hh>
34 
35 #include <mln/core/site_set/p_faces.hh>
36 #include <mln/core/image/complex_image.hh>
37 
38 // FIXME: Include these elsewhere? (In complex_image.hh?)
39 #include <mln/core/image/complex_windows.hh>
40 #include <mln/core/image/complex_window_piter.hh>
41 
42 #include <mln/debug/iota.hh>
43 
44 #include <mln/morpho/erosion.hh>
45 #include <mln/morpho/dilation.hh>
46 
47 /* FIXME: Factor common parts with
48  milena/tests/core/image/complex_image.cc */
49 
50 // Forward declaration.
51 template <typename I, typename W>
52 void test_morpho(const mln::Image<I>& ima, const mln::Window<W> win,
53  const std::string& comment);
54 
55 
56 int main()
57 {
58  using namespace mln;
59 
60  /*----------.
61  | Complex. |
62  `----------*/
63 
64  /* A 2-d (simplicial) complex and its adjacency graph.
65 
66  c 0 1 2 3
67  r .------------------------
68  | v0 e3 v3
69  0 | o-----------o v0----e3----v3
70  | / \ ,-----. / / \ | /
71  | / . \ \ t1/ / / \ t1 /
72  1 | e0 / / \ e1\ / / e4 e0. ,e1' `e4
73  | / /t0 \ \ ' / / t0 \ /
74  | / `-----' \ / / | \ /
75  2 | o-----------o v1----e2----v2
76  | v1 e2 v2
77 
78  v = vertex
79  e = edge
80  t = triangle
81  */
82 
83 
84  const unsigned D = 2;
85 
87 
88  // 0-faces (points).
89  topo::n_face<0, D> v0 = c.add_face();
90  topo::n_face<0, D> v1 = c.add_face();
91  topo::n_face<0, D> v2 = c.add_face();
92  topo::n_face<0, D> v3 = c.add_face();
93 
94  // 1-faces (segments).
95  topo::n_face<1, D> e0 = c.add_face(v0 + v1);
96  topo::n_face<1, D> e1 = c.add_face(v0 + v2);
97  topo::n_face<1, D> e2 = c.add_face(v1 + v2);
98  topo::n_face<1, D> e3 = c.add_face(v0 + v3);
99  topo::n_face<1, D> e4 = c.add_face(v2 + v3);
100 
101  // 2-faces (triangles).
102  topo::n_face<2, D> t0 = c.add_face(e0 + e1 + e2);
103  topo::n_face<2, D> t1 = c.add_face(e1 + e3 + e4);
104 
105 
106  /*-------------------------.
107  | Complex-based site set. |
108  `-------------------------*/
109 
110  typedef point2d P;
112  G geom;
113  geom.add_location(point2d(0,1)); // 0-face #0.
114  geom.add_location(point2d(2,0)); // 0-face #1.
115  geom.add_location(point2d(2,2)); // 0-face #2.
116  geom.add_location(point2d(0,3)); // 0-face #3.
117  p_complex<D, G> pc(c, geom);
118 
119 
120  /*----------------------.
121  | Complex-based image. |
122  `----------------------*/
123 
124  using mln::value::int_u8;
125 
126  // An image type built on a 2-complex with mln::int_u8 values on
127  // each face.
128  typedef complex_image<D, G, int_u8> ima_t;
129  ima_t ima(pc);
130  // Initialize values.
131  debug::iota(ima);
132 
133  /* Values of IMA.
134 
135  v0 e3 v3 1 8 4
136  o-----------o o-----------o
137  / \ ,-----. / / \ ,-----. /
138  / . \ \ t1/ / / . \ \ 11/ /
139  e0 / / \ e1\ / / e4 5 / / \ 6 \ / / 9
140  / /t0 \ \ ' / / /10 \ \ ' /
141  / `-----' \ / / `-----' \ /
142  o-----------o o-----------o
143  v1 e2 v2 2 7 3
144 
145  */
146 
147  // Manual iteration over the domain of IMA.
148  mln_piter_(ima_t) p(ima.domain());
149  for_all (p)
150  std::cout << "ima (" << p << ") = " << ima(p) << std::endl;
151  std::cout << std::endl << std::endl;
152 
153  /*---------------------------------------------------.
154  | Morphological operations on complex-based images. |
155  `---------------------------------------------------*/
156 
157  test_morpho(ima, complex_lower_window_p<D, G>(),
158  "lower-dimension faces");
159  test_morpho(ima, complex_higher_window_p<D, G>(),
160  "higher-dimension faces");
161  test_morpho(ima, complex_lower_higher_window_p<D, G>(),
162  "lower- and higer-dimension faces");
163 
164  test_morpho(ima, complex_lower_dim_connected_n_face_window_p<D, G>(),
165  "lower-dimension connected n-faces");
166  test_morpho(ima, complex_higher_dim_connected_n_face_window_p<D, G>(),
167  "higher-dimension connected n-faces");
168 
169  /* FIXME: Exercise elementary erosion/dilation (with neighborhoods)
170  when available. */
171 }
172 
173 
174 template <typename I, typename W>
175 void
176 test_morpho(const mln::Image<I>& ima_, const mln::Window<W> win,
177  const std::string& comment)
178 {
179  const I& ima = exact(ima_);
180  mln_assertion(ima.is_valid());
181  mln_piter(I) p(ima.domain());
182 
183  mln_concrete(I) ima_dil = mln::morpho::dilation(ima, win);
184  // Manual iteration over the domain of IMA_DIL.
185  std::cout << "Dilation using " << comment << ":" << std::endl;
186  for_all (p)
187  std::cout << " ima_dil (" << p << ") = " << ima_dil(p) << std::endl;
188  std::cout << std::endl;
189 
190  std::cout << "Erosion using " << comment << ":" << std::endl;
191  mln_concrete(I) ima_ero = mln::morpho::erosion(ima, win);
192  // Manual iteration over the domain of IMA_ERO.
193  for_all (p)
194  std::cout << " ima_ero (" << p << ") = " << ima_ero(p) << std::endl;
195  std::cout << std::endl << std::endl;
196 }