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