Milena (Olena)  User documentation 2.0a Id
 All Classes Namespaces Functions Variables Typedefs Enumerator Groups Pages
demos/graph/region_adjacency_graph.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 
29 
30 
31 #include <iostream>
32 
33 #include <mln/core/image/image2d.hh>
34 #include <mln/core/alias/neighb2d.hh>
35 #include <mln/core/alias/window2d.hh>
36 #include <mln/core/image/dmorph/image_if.hh>
37 #include <mln/core/var.hh>
38 
39 #include <mln/accu/center.hh>
40 #include <mln/accu/stat/mean.hh>
41 
42 #include <mln/io/ppm/save.hh>
43 #include <mln/io/ppm/load.hh>
44 #include <mln/io/pgm/load.hh>
45 #include <mln/io/pgm/save.hh>
46 
47 #include <mln/value/rgb8.hh>
48 #include <mln/value/int_u8.hh>
49 #include <mln/value/label_16.hh>
50 
51 #include <mln/data/transform.hh>
52 
53 #include <mln/labeling/wrap.hh>
54 #include <mln/labeling/mean_values.hh>
55 
56 #include <mln/convert/to_fun.hh>
57 
58 #include <mln/morpho/gradient.hh>
59 #include <mln/morpho/closing/area.hh>
60 #include <mln/morpho/watershed/flooding.hh>
61 #include <mln/morpho/elementary/dilation.hh>
62 
63 #include <mln/make/vertex_image.hh>
64 #include <mln/make/edge_image.hh>
65 #include <mln/make/region_adjacency_graph.hh>
66 
67 #include <mln/math/diff_abs.hh>
68 
69 #include <mln/debug/draw_graph.hh>
70 
71 namespace mln
72 {
73 
75  struct dist : Function_vv2v< dist >
76  {
77 
78  typedef value::int_u8 result;
79 
80  result operator()(const value::rgb8& c1, const value::rgb8& c2) const
81  {
82  unsigned d = math::diff_abs(c1.red(), c2.red());
83  unsigned d_;
84  d_ = math::diff_abs(c1.green(), c2.green());
85 
86  if (d_ > d)
87  d = d_;
88 
89  d_ = math::diff_abs(c1.blue(), c2.blue());
90 
91  if (d_ > d)
92  d = d_;
93  return d;
94  }
95 
96  };
97 
99  template <typename I, typename V>
100  struct edge_to_color : Function_v2v< edge_to_color<I,V> >
101  {
102  typedef V result;
103 
104  edge_to_color(const I& ima)
105  : ima_(ima)
106  {
107  }
108 
109  V
110  operator()(const unsigned& e) const
111  {
112  return convert::to<V>(ima_(e));
113  }
114 
115  I ima_;
116  };
117 
118 
120  template <typename I, typename V, typename E>
121  inline
122  image2d<mln_value(I)>
123  make_debug_graph_image(const I& input,
124  const V& ima_v, const E& ima_e,
125  const value::rgb8& bg)
126  {
128  initialize(ima, input);
129 
130  data::fill(ima, bg);
131  debug::draw_graph(ima, ima_v.domain(),
132  pw::cst(mln_value(I)(literal::green)),
133  edge_to_color<E, mln_value(I)>(ima_e));
134 
136  dpoint2d tl(-3,-3);
137  dpoint2d br(3,3);
138  mln_piter(V) p(ima_v.domain());
139  for_all(p)
140  if (p.id() != 0)
141  {
142  box2d b(p + tl, p + br);
143  b.crop_wrt(ima.domain());
144  data::fill((ima | b).rw(), convert::to<mln_value(I)>(ima_v(p)));
145  }
146 
147  return ima;
148  }
149 
150 }
151 
152 int main()
153 {
154  using namespace mln;
155  using value::int_u8;
156  using value::rgb8;
157  using value::label_16;
158 
159  image2d<int_u8> input_pgm;
160  io::pgm::load(input_pgm, "house.pgm");
161 
162  image2d<rgb8> input_ppm;
163  io::ppm::load(input_ppm, "house.ppm");
164 
165 
167  image2d<int_u8> grad = morpho::gradient(input_pgm, win_c4p());
168  io::pgm::save(grad, "tmp_grad_c4p.pgm");
169 
171  image2d<int_u8> clo = morpho::closing::area(grad, c4(), 25);
172  io::pgm::save(clo, "tmp_clo_a100.pgm");
173 
175  label_16 nbasins;
176  image2d<label_16> wshed = morpho::watershed::flooding(clo, c4(), nbasins);
177 
180  io::ppm::save(labeling::mean_values(input_ppm, wshed, nbasins),
181  "tmp_wshed_mean_colors.ppm");
182 
183 
185  data::fill((input_ppm | (pw::value(wshed) == 0u)).rw(), literal::yellow);
186  io::ppm::save(input_ppm, "tmp_wshed_color.ppm");
187 
188 
190  util::graph g = make::region_adjacency_graph(wshed, c4(), nbasins);
191 
192 
197  wshed, nbasins);
198 
202  accu_mean_t;
204  mean_values = labeling::compute(accu_mean_t(), input_ppm, wshed, nbasins);
205 
207  mln_VAR(ima_v, make::vertex_image(g, basin_centers, mean_values));
208 
210  mln_VAR(ima_e, make::edge_image(ima_v, dist()));
211 
212  io::ppm::save(make_debug_graph_image(input_ppm, ima_v, ima_e, literal::white),
213  "tmp_wst_rag_graph_image_white.ppm");
214 }