$extrastylesheet
Olena  User documentation 2.1
An Image Processing Platform
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
demos/graph/region_adjacency_graph.cc
1 // Copyright (C) 2009, 2012 EPITA Research and Development Laboratory
2 // (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 
30 
31 
32 #include <iostream>
33 
34 #include <mln/core/image/image2d.hh>
35 #include <mln/core/alias/neighb2d.hh>
36 #include <mln/core/alias/window2d.hh>
37 #include <mln/core/image/dmorph/image_if.hh>
38 #include <mln/core/var.hh>
39 
40 #include <mln/accu/center.hh>
41 #include <mln/accu/stat/mean.hh>
42 
43 #include <mln/io/ppm/save.hh>
44 #include <mln/io/ppm/load.hh>
45 #include <mln/io/pgm/load.hh>
46 #include <mln/io/pgm/save.hh>
47 
48 #include <mln/value/rgb8.hh>
49 #include <mln/value/int_u8.hh>
50 #include <mln/value/label_16.hh>
51 
52 #include <mln/data/transform.hh>
53 
54 #include <mln/labeling/wrap.hh>
55 #include <mln/labeling/mean_values.hh>
56 
57 #include <mln/convert/to_fun.hh>
58 
59 #include <mln/morpho/gradient.hh>
60 #include <mln/morpho/closing/area.hh>
61 #include <mln/morpho/watershed/flooding.hh>
62 #include <mln/morpho/elementary/dilation.hh>
63 
64 #include <mln/make/vertex_image.hh>
65 #include <mln/make/edge_image.hh>
66 #include <mln/make/region_adjacency_graph.hh>
67 
68 #include <mln/math/diff_abs.hh>
69 
70 #include <mln/debug/draw_graph.hh>
71 #include <mln/debug/filename.hh>
72 #include <mln/data/transform.hh>
73 #include <mln/fun/v2v/rgb_to_int_u.hh>
74 
75 namespace mln
76 {
77 
79  struct dist : Function_vv2v< dist >
80  {
81 
82  typedef value::int_u8 result;
83 
84  result operator()(const value::rgb8& c1, const value::rgb8& c2) const
85  {
86  unsigned d = math::diff_abs(c1.red(), c2.red());
87  unsigned d_;
88  d_ = math::diff_abs(c1.green(), c2.green());
89 
90  if (d_ > d)
91  d = d_;
92 
93  d_ = math::diff_abs(c1.blue(), c2.blue());
94 
95  if (d_ > d)
96  d = d_;
97  return d;
98  }
99 
100  };
101 
103  template <typename I, typename V>
104  struct edge_to_color : Function_v2v< edge_to_color<I,V> >
105  {
106  typedef V result;
107 
108  edge_to_color(const I& ima)
109  : ima_(ima)
110  {
111  }
112 
113  V
114  operator()(const unsigned& e) const
115  {
116  return convert::to<V>(ima_(e));
117  }
118 
119  I ima_;
120  };
121 
122 
124  template <typename I, typename V, typename E>
125  inline
126  image2d<mln_value(I)>
127  make_debug_graph_image(const I& input,
128  const V& ima_v, const E& ima_e,
129  const value::rgb8& bg)
130  {
132  initialize(ima, input);
133 
134  data::fill(ima, bg);
135  debug::draw_graph(ima, ima_v.domain(),
136  pw::cst(mln_value(I)(literal::green)),
137  edge_to_color<E, mln_value(I)>(ima_e));
138 
140  dpoint2d tl(-3,-3);
141  dpoint2d br(3,3);
142  mln_piter(V) p(ima_v.domain());
143  for_all(p)
144  if (p.id() != 0)
145  {
146  box2d b(p + tl, p + br);
147  b.crop_wrt(ima.domain());
148  data::fill((ima | b).rw(), convert::to<mln_value(I)>(ima_v(p)));
149  }
150 
151  return ima;
152  }
153 
154 }
155 
156 int main(int argc, char *argv[])
157 {
158  using namespace mln;
159  using value::int_u8;
160  using value::rgb8;
161  using value::label_16;
162 
163  if (argc != 3 && argc != 4)
164  {
165  std::cout << "Usage: " << argv[0] << " <input.*> <output_prefix>"
166  << " [closing_area_value (default:25)]" << std::endl;
167  return 1;
168  }
169 
171  image2d<rgb8> input_ppm;
172  io::ppm::load(input_ppm, argv[1]);
173 
176  input_pgm = data::transform(input_ppm, fun::v2v::rgb_to_int_u<8>());
177 
180 
181  unsigned area_value = 25;
182  if (argc == 4)
183  area_value = atoi(argv[3]);
184 
186  image2d<int_u8> grad = morpho::gradient(input_pgm, win_c4p());
187  io::pgm::save(grad, debug::filename("grad_c4p.pgm"));
188 
190  image2d<int_u8> clo = morpho::closing::area(grad, c4(), area_value);
191  io::pgm::save(clo, debug::filename("clo_a100.pgm"));
192 
194  label_16 nbasins;
195  image2d<label_16> wshed = morpho::watershed::flooding(clo, c4(), nbasins);
196 
199  io::ppm::save(labeling::mean_values(input_ppm, wshed, nbasins),
200  debug::filename("wshed_mean_colors.ppm"));
201 
202 
204  data::fill((input_ppm | (pw::value(wshed) == 0u)).rw(), literal::yellow);
205  io::ppm::save(input_ppm, debug::filename("wshed_color.ppm"));
206 
207 
209  util::graph g = make::region_adjacency_graph(wshed, c4(), nbasins);
210 
211 
216  wshed, nbasins);
217 
221  accu_mean_t;
223  mean_values = labeling::compute(accu_mean_t(), input_ppm, wshed, nbasins);
224 
226  mln_VAR(ima_v, make::vertex_image(g, basin_centers, mean_values));
227 
229  mln_VAR(ima_e, make::edge_image(ima_v, dist()));
230 
231  io::ppm::save(make_debug_graph_image(input_ppm, ima_v, ima_e, literal::white),
232  debug::filename("wst_rag_graph_image_white.ppm"));
233 }