Milena (Olena)  User documentation 2.0a Id
 All Classes Namespaces Functions Variables Typedefs Enumerator Groups Pages
line_gradient.hh
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 
26 #ifndef MLN_MORPHO_LINE_GRADIENT_HH
27 # define MLN_MORPHO_LINE_GRADIENT_HH
28 
32 
33 # include <functional>
34 
35 # include <map>
36 # include <vector>
37 
38 # include <mln/math/abs.hh>
39 
40 # include <mln/core/image/image2d.hh>
41 # include <mln/core/image/edge_image.hh>
42 # include <mln/core/alias/window2d.hh>
43 
44 # include <mln/util/graph.hh>
45 # include <mln/util/site_pair.hh>
46 
47 
48 // FIXME: Generalize to other (input) images as well (image1d,
49 // image3d, etc.).
50 
51 namespace mln
52 {
53 
54  namespace morpho
55  {
56 
59  /* FIXME: Currently, the adjacency is set to 4-c and cannot be
60  changed. */
61  template <typename V>
62  edge_image<util::site_pair<point2d>, V, util::graph>
63  line_gradient(const mln::image2d<V>& ima);
64 
65 
66 # ifndef MLN_INCLUDE_ONLY
67 
68  template <typename V>
69  edge_image<util::site_pair<point2d>, V, util::graph>
71  {
72  trace::entering("morpho::line_gradient");
73  mln_precondition(ima.is_valid());
74 
75  // FIXME: Precondition: Ensure the image is scalar.
76  util::graph g;
77 
78  // Vertices.
79  image2d<unsigned> vpsite(ima.domain());
80  fun::i2v::array<point2d> fv2p(ima.domain().nsites());
81  fun::i2v::array<V> vertex_values(ima.domain().nsites());
82 
83  mln_fwd_piter(image2d<V>) p(ima.domain());
84  for_all (p)
85  {
86  g.add_vertex();
87  unsigned id = g.v_nmax() - 1;
88  vpsite(p) = id;
89  fv2p(id) = p;
90  }
91 
92  // Edges.
93  // FIXME: The creation of this window should be generic.
94  window2d next_c4_win;
95  next_c4_win.insert(0, 1).insert(1, 0);
96  typedef fun::i2v::array<V> edge_values_t;
97  typedef fun::i2v::array< util::site_pair<point2d> > edge_sites_t;
98  edge_values_t edge_values;
99  edge_sites_t edge_sites;
100  mln_fwd_qiter_(window2d) q(next_c4_win, p);
101  for_all (p)
102  for_all (q)
103  if (ima.domain().has(q))
104  {
105  g.add_edge(vpsite(p), vpsite(q));
106  // The computed value is a norm of the gradient between P and Q.
107  edge_values.append(math::abs(ima(p) - ima(q)));
108  edge_sites.append(util::site_pair<point2d>(p, q));
109  }
110 
112  lg_ima(g, edge_sites, edge_values);
113 
114  trace::exiting("morpho::line_gradient");
115  return lg_ima;
116  }
117 
118 # endif // ! MLN_INCLUDE_ONLY
119 
120  } // end of namespace mln::morpho
121 
122 } // end of namespace mln
123 
124 
125 #endif // ! MLN_MORPHO_LINE_GRADIENT_HH