$extrastylesheet
Olena  User documentation 2.1
An Image Processing Platform
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
lena_line_graph_image_wst1.cc
1 // Copyright (C) 2008, 2009, 2010, 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 /* FIXME: We should factor as much things as possible between
28  tests/morpho/lena_line_graph_image_wst1.cc and
29  tests/morpho/lena_line_graph_image_wst2.cc, starting from conversion
30  routines. */
31 
48 #include <map>
49 #include <vector>
50 
51 #include <mln/util/ord.hh>
52 
53 #include <mln/core/image/image2d.hh>
54 #include <mln/core/alias/point2d.hh>
55 #include <mln/core/alias/window2d.hh>
56 #include <mln/core/alias/neighb2d.hh>
57 
58 #include <mln/convert/to_window.hh>
59 
60 #include <mln/util/graph.hh>
61 #include <mln/fun/i2v/array.hh>
62 #include <mln/util/site_pair.hh>
63 #include <mln/core/image/edge_image.hh>
64 
65 #include <mln/morpho/gradient.hh>
66 #include <mln/morpho/closing/area.hh>
67 #include <mln/morpho/watershed/flooding.hh>
68 
69 #include <mln/value/int_u8.hh>
70 #include <mln/value/rgb8.hh>
71 #include <mln/literal/black.hh>
72 #include <mln/literal/colors.hh>
73 
74 #include <mln/io/pgm/load.hh>
75 #include <mln/io/ppm/save.hh>
76 
77 #include <mln/math/max.hh>
78 
79 #include "tests/data.hh"
80 
81 
82 int main()
83 {
84  using namespace mln;
85  using value::int_u8;
86  using value::rgb8;
87 
88  /*--------.
89  | Input. |
90  `--------*/
91 
92  typedef int_u8 input_val_t;
94  io::pgm::load(input, MLN_IMG_DIR "/tiny.pgm");
95 
96  // In this test, the gradient is directly computed on the input
97  // image, not on the edges of the line graph image.
100 
101  // Simplify the input image.
102  image2d<input_val_t> work(input.domain());
103  work = morpho::closing::area(gradient, c4(), 10);
104 
105  /*-------------.
106  | Line graph. |
107  `-------------*/
108 
109  // FIXME: Inlined conversion, to be reifed into a routine.
110 
111  util::graph g;
112 
113  // Points.
114  image2d<unsigned> equiv_vertex;
115  initialize(equiv_vertex, work);
116 
117  // Vertices.
118  mln_fwd_piter_(image2d<input_val_t>) p(work.domain());
119  for_all(p)
120  equiv_vertex(p) = g.add_vertex();
121 
122  // Edges.
123  window2d next_c4_win;
124  next_c4_win.insert(0, 1).insert(1, 0);
125  typedef fun::i2v::array<int> edge_values_t;
126  typedef fun::i2v::array< util::site_pair<point2d> > edge_sites_t;
127  edge_values_t edge_values;
128  edge_sites_t edge_sites;
129  mln_fwd_qiter_(window2d) q(next_c4_win, p);
130  for_all (p)
131  for_all(q)
132  if (work.domain().has(q))
133  {
134  g.add_edge(equiv_vertex(p), equiv_vertex(q));
135  edge_values.append(math::max(work(p), work(q)));
136  edge_sites.append(util::site_pair<point2d>(p, q));
137  }
138 
139  // Line graph point set.
141  lg_ima_t lg_ima(g, edge_sites, edge_values);
142 
143  /*------.
144  | WST. |
145  `------*/
146 
147  // Elementary neighborhood of an edge.
148  typedef lg_ima_t::nbh_t nbh_t;
149  nbh_t nbh;
150 
151  // Perform a Watershed Transform.
152  int_u8 nbasins;
153  typedef edge_image<util::site_pair<point2d>,int_u8,util::graph> wshed_t;
154  wshed_t wshed = morpho::watershed::flooding(lg_ima, nbh, nbasins);
155  mln_assertion(nbasins == 5u);
156 
157  /*---------.
158  | Output. |
159  `---------*/
160 
161  // FIXME: Inlined conversion, to be reifed into a routine.
162 
163  // Save the result in gray levels (data) + color (wshed).
164 
165  // Data.
166  typedef rgb8 output_val_t;
167  typedef image2d<output_val_t> output_t;
168  point2d output_pmin = input.domain().pmin();
169  point2d output_pmax(input.domain().pmax()[0] * 2,
170  input.domain().pmax()[1] * 2);
171  output_t output(box2d(output_pmin, output_pmax));
172  data::fill(output, literal::black);
173  // Reuse the piter on INPUT.
174  for_all(p)
175  {
176  // Equivalent of P in OUTPUT.
177  point2d q(p[0] * 2, p[1] * 2);
178  input_val_t v = input(p);
179  /* FIXME: Use a conversion function from input_val_t to
180  output_val_t instead of an explicit construction. */
181  output(q) = output_val_t(v, v, v);
182  }
183  // Interpolate missing points in OUTPUT.
184  mln_piter_(output_t) p_out(output.domain());
185  for_all(p_out)
186  {
187  // Process points on even rows and odd columns
188  if (p_out[0] % 2 == 0 && p_out[1] % 2 == 1)
189  output(p_out) = (output(p_out + left) + output(p_out + right)) / 2;
190  // Process points on odd rows and even columns
191  if (p_out[0] % 2 == 1 && p_out[1] % 2 == 0)
192  output(p_out) = (output(p_out + up) + output(p_out + down)) / 2;
193  // Process points on odd rows and odd columns
194  if (p_out[0] % 2 == 1 && p_out[1] % 2 == 1)
195  output(p_out) =
196  (output(p_out + dpoint2d(-1, -1)) +
197  output(p_out + dpoint2d(-1, +1)) +
198  output(p_out + dpoint2d(+1, -1)) +
199  output(p_out + dpoint2d(+1, +1))) / 4;
200  }
201  // Draw the watershed.
202  /* FIXME: We should draw the watershed on another image and
203  superimpose it on OUTPUT instead of drawing it directly into
204  OUTPUT. */
205  mln_piter_(wshed_t) pw(wshed.domain());
206  for_all(pw)
207  {
208  if (wshed(pw) == 0u)
209  {
210  mln_psite_(lg_ima_t) pp(pw);
211  // Equivalent of the line (edge) PP in OUTPUT.
212  int row1 = pp.first()[0] * 2;
213  int col1 = pp.first()[1] * 2;
214  int row2 = pp.second()[0] * 2;
215  int col2 = pp.second()[1] * 2;
216  point2d q((row1 + row2) / 2, (col1 + col2) / 2);
217  // Print the watershed in red.
218  output(q) = literal::red;
219  }
220  }
221  // Fill the holes, so that the watershed looks connected.
222  /* FIXME: This approach is bad: it creates thick lines of watershed.
223  We should probably solve this when we ``paint'' the watershed
224  over the ``doubled'' image.
225 
226  A better approach is probably to iterate over the set of vertices,
227  and ``connect'' edges according to patterns (vertically or
228  horizontally connected egdes member of the watershed, etc.). */
229  // Reuse the piter on OUTPUT.
230  for_all (p_out)
231  // Only handle points on odd rows and columns.
232  if (p_out[0] % 2 == 1 && p_out[1] % 2 == 1)
233  {
234  // Count the number of adjacent watershed points. If there are
235  // two or more, consider, create a watershed point.
236  /* FIXME: Iterating over a c4 window would be more elegant, of
237  course. */
238  unsigned nwsheds =
239  (output.has(p_out + up ) && output(p_out + up ) == literal::red) +
240  (output.has(p_out + down ) && output(p_out + down ) == literal::red) +
241  (output.has(p_out + left ) && output(p_out + right) == literal::red) +
242  (output.has(p_out + right) && output(p_out + left ) == literal::red);
243  if (nwsheds >= 2)
244  output(p_out) = literal::red;
245  }
246  io::ppm::save(output, "lena_line_graph_image_wst1-out.ppm");
247 }