$extrastylesheet
Olena  User documentation 2.1
An Image Processing Platform
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
apps/papers/levillain.09.ismm/complex.cc
1 // Copyright (C) 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 
26 #include <cstdlib>
27 
28 #include <string>
29 
30 #include <mln/core/image/complex_image.hh>
31 #include <mln/core/image/complex_neighborhoods.hh>
32 #include <mln/core/image/complex_windows.hh>
33 
34 #include <mln/value/int_u8.hh>
35 #include <mln/value/label_16.hh>
36 
37 #include <mln/data/fill.hh>
38 #include <mln/math/diff_abs.hh>
39 #include <mln/literal/zero.hh>
40 
41 #include <mln/io/off/load.hh>
42 #include <mln/io/off/save.hh>
43 #include <mln/labeling/colorize.hh>
44 
45 #include "chain.hh"
46 
47 
48 // FIXME: Should be rewritten using a diff_abs-based accumulator
49 // taking values from triangles (and checking that exactly two values
50 // where taken, of course).
51 template <unsigned D, typename G, typename V>
52 inline
54 gradient_on_edges(const mln::complex_image<D, G, V>& input)
55 {
56  typedef mln::complex_image<D, G, V> ima_t;
57  ima_t output (input.domain());
59 
60  // Values on edges.
62  // Edge-to-triangle adjacency.
64  e2t_t e2t;
65  mln_niter(e2t_t) t(e2t, e);
66  // Iterate on edges (1-faces).
67  for_all(e)
68  {
69  t.start();
70  // An edge should be adjacent to at least one triangle.
71  if (!t.is_valid())
72  abort();
73  V v1 = input(t);
74  t.next();
75  // If E is adjacent to two triangles, compute the absolute
76  // difference between their values.
77  if (t.is_valid())
78  {
79  V v2 = input(t);
80  output(e) = mln::math::diff_abs(v1, v2);
81  // There should be no more adjacent triangles.
82  t.next();
83  mln_assertion(!t.is_valid());
84  }
85  }
86  return output;
87 }
88 
89 
90 int main(int argc, char* argv[])
91 {
92  if (argc != 4)
93  {
94  std::cerr << "usage: " << argv[0] << " input.off lambda output.off"
95  << std::endl;
96  std::exit(1);
97  }
98  std::string input_filename = argv[1];
99  unsigned lambda = atoi(argv[2]);
100  std::string output_filename = argv[3];
101 
102  using namespace mln;
103 
104  typedef float val;
105  typedef value::label_16 label;
106  // Input and output types.
107  typedef mln::float_2complex_image3df input;
108  typedef mln_ch_value_(input, label) output;
109  static const unsigned dim = input::dim;
110  typedef mln_geom_(input) geom;
113  label nbasins;
114 
115  // Load, process, save.
116  /* FIXME: The domain of IMA should be limited to 2-faces. Alas, we
117  do not have (yet) an mln::pcomplex_subset site set type. Anyway,
118  this should not alter the results, only slow the computation
119  down. */
120  input ima;
121  io::off::load(ima, input_filename);
122 
123  // Gradient on edges.
124  input g = gradient_on_edges(ima);
125  output s = chain(ima, nbh, lambda, nbasins);
126  io::off::save(labeling::colorize(value::rgb8(), s, nbasins),
127  output_filename);
128 }