$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-segm.cc
1 // Copyright (C) 2008, 2009, 2010, 2011, 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 
31 
32 // Factor with mesh-complex-pinv-curv-segm.cc.
33 
34 #include <cstdlib>
35 #include <cmath>
36 
37 #include <utility>
38 #include <iostream>
39 
40 #include <mln/core/image/complex_image.hh>
41 #include <mln/core/image/complex_neighborhoods.hh>
42 
43 #include <mln/morpho/closing/area.hh>
44 #include <mln/morpho/watershed/flooding.hh>
45 
46 #include <mln/math/max.hh>
47 #include <mln/math/sqr.hh>
48 
49 #include <mln/literal/white.hh>
50 
51 #include <mln/io/off/load.hh>
52 #include <mln/io/off/save.hh>
53 
54 #include "misc.hh"
55 
56 
57 int main(int argc, char* argv[])
58 {
59  if (argc != 4)
60  {
61  std::cerr << "usage: " << argv[0] << " input.off lambda output.off"
62  << std::endl;
63  std::exit(1);
64  }
65 
66  std::string input_filename = argv[1];
67  unsigned lambda = atoi(argv[2]);
68  std::string output_filename = argv[3];
69 
70  /*----------------.
71  | Complex image. |
72  `----------------*/
73 
74  // Image type.
75  typedef mln::float_2complex_image3df float_ima_t;
76  // Dimension of the image (and therefore of the complex).
77  static const unsigned D = float_ima_t::dim;
78  // Geometry of the image.
79  typedef mln_geom_(float_ima_t) G;
80 
81  mln::bin_2complex_image3df bin_input;
82  mln::io::off::load(bin_input, input_filename);
83  std::pair<float_ima_t, float_ima_t> curv =
84  mln::geom::mesh_curvature(bin_input.domain());
85 
86  // Compute the pseudo_inverse curvature at each vertex.
87  float_ima_t float_ima(bin_input.domain());
88  mln::p_n_faces_fwd_piter<D, G> v(float_ima.domain(), 0);
89  for_all(v)
90  {
91  // Max curvature.
92  float_ima(v) = mln::math::max(mln::math::sqr(curv.first(v)),
93  mln::math::sqr(curv.second(v)));
94  }
95 
96  // Values on edges.
97  mln::p_n_faces_fwd_piter<D, G> e(float_ima.domain(), 1);
98  typedef mln::complex_lower_neighborhood<D, G> adj_vertices_nbh_t;
99  adj_vertices_nbh_t adj_vertices_nbh;
100  mln_niter_(adj_vertices_nbh_t) adj_v(adj_vertices_nbh, e);
101  // Iterate on edges (1-faces).
102  for_all(e)
103  {
104  float s = 0.0f;
105  unsigned n = 0;
106  // Iterate on vertices (0-faces).
107  for_all(adj_v)
108  {
109  s += float_ima(adj_v);
110  ++n;
111  }
112  float_ima(e) = s / n;
113  // An edge should be adjacent to exactly two vertices.
114  mln_invariant(n <= 2);
115  }
116 
117  // Convert the float image into an unsigned image because some
118  // algorithms do not handle float images well.
119  /* FIXME: This is bad: float images should be handled as-is. At
120  least, we should use a decent conversion, using an optimal affine
121  transformation (stretching/shrinking) or any other kind of
122  interpolation. */
123  typedef mln::unsigned_2complex_image3df ima_t;
124  ima_t ima(float_ima.domain());
125  mln_piter_(ima_t) p(float_ima.domain());
126  for_all(p)
127  ima(p) = 1000 * float_ima(p);
128 
129  /*-----------------.
130  | Simplification. |
131  `-----------------*/
132 
134  typedef
135  mln::complex_higher_dim_connected_n_face_neighborhood<D, G>
136  adj_edges_nbh_t;
137  adj_edges_nbh_t adj_edges_nbh;
138 
139  ima_t closed_ima = mln::morpho::closing::area(ima, adj_edges_nbh, lambda);
140 
141  /*------.
142  | WST. |
143  `------*/
144 
145  /* FIXME: Note that the WST is doing too much work, since we have
146  not constrained the domain of the image to 1-faces only.
147  It would be great if we could use something like this:
148 
149  closed_ima | mln::p_faces<1, D, G>(closed_ima.domain())
150 
151  as input of the WST. */
152 
153  // Compute the WST on edges.
154  typedef unsigned wst_val_t;
155  wst_val_t nbasins;
156  typedef mln::unsigned_2complex_image3df wst_ima_t;
157  wst_ima_t wshed =
158  mln::morpho::watershed::flooding(closed_ima, adj_edges_nbh, nbasins);
159  std::cout << "nbasins = " << nbasins << std::endl;
160 
161  // Label polygons (i.e., propagate labels from edges to polygons).
162  typedef mln::complex_higher_neighborhood<D, G> adj_polygons_nbh_t;
163  adj_polygons_nbh_t adj_polygons_nbh;
164  mln_niter_(adj_polygons_nbh_t) adj_p(adj_polygons_nbh, e);
165  for_all(e)
166  if (wshed(e) != 0)
167  for_all(adj_p)
168  wshed(adj_p) = wshed(e);
169 
170  /*---------.
171  | Output. |
172  `---------*/
173 
174  mln::rgb8_2complex_image3df output(wshed.domain());
175  mln::data::fill(output, mln::literal::white);
176 
177  // FIXME: Use a colorize functor instead.
178  // Choose random colors for each basin number.
179  std::vector<mln::value::rgb8> basin_color (nbasins + 1);
180  for (unsigned i = 0; i <= nbasins; ++i)
181  basin_color[i] = mln::value::rgb8(random() % 256,
182  random() % 256,
183  random() % 256);
184  mln_piter_(ima_t) f(wshed.domain());
185  for_all(f)
186  output(f) = basin_color[wshed(f)];
187 
188  mln::io::off::save(output, output_filename);
189 }