$extrastylesheet
Olena  User documentation 2.1
An Image Processing Platform
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
mesh-complex-max-curv-skel.cc
1 // Copyright (C) 2008, 2009, 2010, 2013 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 
32 
33 // FIXME: Factor with mesh-complex-pinv-curv-skel.cc.
34 
35 #include <iostream>
36 
37 #include <mln/core/image/complex_image.hh>
38 #include <mln/core/image/complex_neighborhoods.hh>
39 
40 #include <mln/core/image/dmorph/image_if.hh>
41 #include <mln/core/image/dmorph/mutable_extension_ima.hh>
42 #include <mln/core/routine/mutable_extend.hh>
43 #include <mln/data/paste.hh>
44 
45 #include <mln/value/label_16.hh>
46 
47 #include <mln/labeling/regional_minima.hh>
48 #include <mln/morpho/closing/area.hh>
49 
50 #include <mln/topo/is_n_face.hh>
51 #include <mln/topo/is_simple_cell.hh>
52 #include <mln/topo/detach_cell.hh>
53 #include <mln/topo/skeleton/breadth_first_thinning.hh>
54 
55 #include <mln/io/off/load.hh>
56 /* FIXME: Remove as soon as mln::io::off::save is able to save a
57  morphed mln::complex_image (i.e., seen through image_if). */
58 #include "save_bin_alt.hh"
59 
60 #include "misc.hh"
61 
62 
63 int
64 main(int argc, char* argv[])
65 {
66  if (argc != 4)
67  {
68  std::cerr << "usage: " << argv[0] << " input.off lambda output.off"
69  << std::endl;
70  std::exit(1);
71  }
72 
73  std::string input_filename = argv[1];
74  unsigned lambda = atoi(argv[2]);
75  std::string output_filename = argv[3];
76 
77  /*----------------.
78  | Complex image. |
79  `----------------*/
80 
81  // Curvature image type.
82  typedef mln::float_2complex_image3df float_ima_t;
83  // Dimension of the image (and therefore of the complex).
84  static const unsigned D = float_ima_t::dim;
85  // Geometry of the image.
86  typedef mln_geom_(float_ima_t) G;
87 
88  mln::bin_2complex_image3df bin_input;
89  mln::io::off::load(bin_input, input_filename);
90  std::pair<float_ima_t, float_ima_t> curv =
91  mln::geom::mesh_curvature(bin_input.domain());
92 
93  // Compute the pseudo_inverse curvature at each vertex.
94  float_ima_t float_ima(bin_input.domain());
95  mln::p_n_faces_fwd_piter<D, G> v(float_ima.domain(), 0);
96  for_all(v)
97  {
98  // Max curvature.
99  float_ima(v) = mln::math::max(mln::math::sqr(curv.first(v)),
100  mln::math::sqr(curv.second(v)));
101  }
102 
103  // Values on triangles.
104  mln::p_n_faces_fwd_piter<D, G> t(float_ima.domain(), 2);
105  // For each triangle (2-face) T, iterate on the the set of vertices
106  // (0-faces) transitively adjacent to T.
107  typedef mln::complex_m_face_neighborhood<D, G> adj_vertices_nbh_t;
108  adj_vertices_nbh_t adj_vertices_nbh;
109  mln_niter_(adj_vertices_nbh_t) adj_v(adj_vertices_nbh, t);
110  /* FIXME: We should be able to pass this value (m) either at the
111  construction of the neighborhood or at the construction of the
112  iterator. */
113  adj_v.iter().set_m(0);
114  // Iterate on triangles (2-faces).
115  for_all(t)
116  {
117  float s = 0.0f;
118  unsigned n = 0;
119  // Iterate on vertices (0-faces).
120  for_all(adj_v)
121  {
122  s += float_ima(adj_v);
123  ++n;
124  }
125  float_ima(t) = s / n;
126  // A triangle should be adjacent to exactly two vertices.
127  mln_invariant(n <= 3);
128  }
129 
130  // Convert the float image into an unsigned image because some
131  // algorithms do not handle float images well.
132  /* FIXME: This is bad: float images should be handled as-is. At
133  least, we should use a decent conversion, using an optimal affine
134  transformation (stretching/shrinking) or any other kind of
135  interpolation. */
136  typedef mln::unsigned_2complex_image3df ima_t;
137  ima_t ima(float_ima.domain());
138  // Process only triangles, as edges and vertices are set afterwards
139  // (see FIXME below).
140  for_all(t)
141  ima(t) = 1000 * float_ima(t);
142 
143  /* FIXME: Workaround: Set maximal values on vertices and edges to
144  exclude them from the set of minimal values. */
145  for_all(v)
146  ima(v) = mln_max(mln_value_(ima_t));
147  mln::p_n_faces_fwd_piter<D, G> e(float_ima.domain(), 1);
148  for_all(e)
149  ima(e) = mln_max(mln_value_(ima_t));
150 
151  /*-----------------.
152  | Simplification. |
153  `-----------------*/
154 
156  typedef mln::complex_lower_dim_connected_n_face_neighborhood<D, G> nbh_t;
157  nbh_t nbh;
158 
159  ima_t closed_ima = mln::morpho::closing::area(ima, nbh, lambda);
160 
161  /*---------------.
162  | Local minima. |
163  `---------------*/
164 
165  typedef mln::value::label_16 label_t;
166  label_t nminima;
167 
168  /* FIXME: We should use something like `ima_t | p_n_faces(2)' instead
169  of `ima_t' here. Or better: `input' should only associate data
170  to 2-faces. */
171  typedef mln_ch_value_(ima_t, label_t) label_ima_t;
172  label_ima_t minima =
173  mln::labeling::regional_minima(closed_ima, nbh, nminima);
174 
175  typedef mln::complex_higher_neighborhood<D, G> higher_nbh_t;
176  higher_nbh_t higher_nbh;
177 
178  // Propagate minima values from triangles to edges.
179  // FIXME: Factor this inside a function.
180  mln_niter_(higher_nbh_t) adj_t(higher_nbh, e);
181  for_all(e)
182  {
183  label_t ref_adj_minimum = mln::literal::zero;
184  for_all(adj_t)
185  if (minima(adj_t) == mln::literal::zero)
186  {
187  // If E is adjacent to a non-minimal triangle, then it must
188  // not belong to a minima.
189  ref_adj_minimum = mln::literal::zero;
190  break;
191  }
192  else
193  {
194  if (ref_adj_minimum == mln::literal::zero)
195  // If this is the first minimum seen, use it as a reference.
196  ref_adj_minimum = minima(adj_t);
197  else
198  // If this is not the first time a minimum is encountered,
199  // ensure it is REF_ADJ_MINIMUM.
200  mln_assertion(minima(adj_t) == ref_adj_minimum);
201  }
202  minima(e) = ref_adj_minimum;
203  }
204 
205  // Likewise from edges to edges to vertices.
206  mln_niter_(higher_nbh_t) adj_e(higher_nbh, v);
207  for_all(v)
208  {
209  label_t ref_adj_minimum = mln::literal::zero;
210  for_all(adj_e)
211  if (minima(adj_e) == mln::literal::zero)
212  {
213  // If V is adjacent to a non-minimal triangle, then it must
214  // not belong to a minima.
215  ref_adj_minimum = mln::literal::zero;
216  break;
217  }
218  else
219  {
220  if (ref_adj_minimum == mln::literal::zero)
221  // If this is the first minimum seen, use it as a reference.
222  ref_adj_minimum = minima(adj_e);
223  else
224  // If this is not the first time a minimum is encountered,
225  // ensure it is REF_ADJ_MINIMUM.
226  mln_assertion(minima(adj_e) == ref_adj_minimum);
227  }
228  minima(v) = ref_adj_minimum;
229  }
230 
231  /*-----------------------.
232  | Initial binary image. |
233  `-----------------------*/
234 
235  /* Careful: creating ``holes'' in the surface obviously changes its
236  topology, but it may also split a single connected component in
237  two or more components, resulting in a disconnected skeleton. We
238  may want to improve this step either by forbidding any splitting,
239  or by incrementally ``digging'' a regional minima as long as no
240  splitting occurs. */
241 
242  typedef mln_ch_value_(ima_t, bool) bin_ima_t;
243  bin_ima_t surface(minima.domain());
244  mln::data::fill(surface, true);
245  // Dig ``holes'' in the surface surface by setting minima faces to false.
246  // FIXME: Use fill with an image_if instead, when available/working.
247  mln_piter_(bin_ima_t) f(minima.domain());
248  for_all(f)
249  if (minima(f) != mln::literal::zero)
250  surface(f) = false;
251 
252  /*-----------.
253  | Skeleton. |
254  `-----------*/
255 
256  // ---------------- //
257  // Skeleton image. //
258  // ---------------- //
259 
260  // Predicate type: is a face a triangle (2-face)?
261  typedef mln::topo::is_n_face<mln_psite_(bin_ima_t), D> is_a_triangle_t;
262  is_a_triangle_t is_a_triangle;
263  // Surface image type, of which domain is restricted to triangles.
264  typedef mln::image_if<bin_ima_t, is_a_triangle_t> bin_triangle_only_ima_t;
265  // Surface image type, of which iteration (not domain) is restricted
266  // to triangles.
267  typedef mln::mutable_extension_ima<bin_triangle_only_ima_t, bin_ima_t>
268  bin_triangle_ima_t;
269 
270  // ------------------------ //
271  // Simple point predicate. //
272  // ------------------------ //
273 
274  // Neighborhood type returning the set of (n-1)- and (n+1)-faces
275  // adjacent to a an n-face.
276  typedef mln::complex_lower_higher_neighborhood<D, G> adj_nbh_t;
277  // Neighborhood type returning the set of (n-1)-faces adjacent to a
278  // an n-face.
279  typedef mln::complex_lower_neighborhood<D, G> lower_adj_nbh_t;
280  // Neighborhood type returning the set of (n+1)-faces adjacent to a
281  // an n-face.
282  typedef mln::complex_higher_neighborhood<D, G> higher_adj_nbh_t;
283  // Predicate type: is a triangle (2-face) simple?
284  typedef mln::topo::is_simple_cell< bin_triangle_ima_t,
285  adj_nbh_t,
286  lower_adj_nbh_t,
287  higher_adj_nbh_t >
288  is_simple_triangle_t;
289  is_simple_triangle_t is_simple_triangle;
290 
291  // ------------------------------- //
292  // Simple point detach procedure. //
293  // ------------------------------- //
294 
295  // Type of adjacency relationships between faces of immediately
296  // lower and higher dimensions.
297  adj_nbh_t adj_nbh;
298  // Functor detaching a cell.
299  mln::topo::detach_cell<bin_triangle_ima_t, adj_nbh_t> detach(adj_nbh);
300 
301  mln_concrete_(bin_ima_t) skel;
302  mln::initialize(skel, surface);
303  mln::data::paste
304  (mln::topo::skeleton::breadth_first_thinning
305  (mln::mutable_extend((surface | is_a_triangle).rw(), surface),
306  nbh,
307  is_simple_triangle,
308  detach),
309  skel);
310 
311 
312  /*---------.
313  | Output. |
314  `---------*/
315 
316  /* FIXME: This does not work (yet).
317  Use workaround mln::io::off::save_bin_alt instead (bad!)
318 
319  Moreover, even if it worked, it would not have the same meaning
320  as mln::io::off::save_bin_alt. Maybe the latter is useful, after
321  all. But we need to factor it with the code of
322  mln::io::off::save, anyway. */
323 #if 0
324  mln::io::off::save(skel | mln::pw::value(skel) == mln::pw::cst(true),
325  output_filename);
326 #endif
327  mln::io::off::save_bin_alt(skel, output_filename);
328 }