Milena (Olena)  User documentation 2.0a Id
 All Classes Namespaces Functions Variables Typedefs Enumerator Groups Pages
lena_line_graph_image_wst2.cc
1 // Copyright (C) 2008, 2009, 2010 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 
51 #include <mln/core/image/image2d.hh>
52 #include <mln/core/alias/point2d.hh>
53 #include <mln/core/alias/window2d.hh>
54 #include <mln/core/alias/neighb2d.hh>
55 
57 #include <mln/core/image/edge_image.hh>
58 #include <mln/core/var.hh>
59 #include <mln/fun/i2v/array.hh>
60 #include <mln/util/graph.hh>
61 
62 #include <mln/morpho/line_gradient.hh>
63 #include <mln/morpho/closing/area_on_vertices.hh>
64 #include <mln/morpho/meyer_wst.hh>
65 #include <mln/data/stretch.hh>
66 
67 #include <mln/value/int_u8.hh>
68 #include <mln/value/int_u16.hh>
69 #include <mln/value/rgb8.hh>
70 #include <mln/literal/black.hh>
71 #include <mln/literal/colors.hh>
72 
73 #include <mln/io/pgm/load.hh>
74 #include <mln/io/ppm/save.hh>
75 
76 #include <mln/math/max.hh>
77 #include <mln/math/abs.hh>
78 
79 #include "tests/data.hh"
80 
81 
82 
83 int main()
84 {
85  using namespace mln;
86  using value::int_u8;
87  using value::int_u16;
88  using value::rgb8;
89 
90  /*--------.
91  | Input. |
92  `--------*/
93 
94  typedef int_u8 input_val_t;
96  io::pgm::load(input, MLN_IMG_DIR "/small.pgm");
97 
98  /*----------------.
99  | Line gradient. |
100  `----------------*/
101 
102  // Line graph image.
103  typedef edge_image<util::site_pair<point2d>,input_val_t,util::graph> lg_ima_t;
104  lg_ima_t lg_ima = morpho::line_gradient(input);
105 
106  /*-----------------.
107  | Simplification. |
108  `-----------------*/
109 
110  typedef lg_ima_t::nbh_t nbh_t;
111  nbh_t nbh;
112 
113  lg_ima_t closed_lg_ima = morpho::closing::area_on_vertices(lg_ima, nbh, 20);
114 
115  /*------.
116  | WST. |
117  `------*/
118 
119  // Perform a Watershed Transform.
120  unsigned nbasins;
121  typedef edge_image<util::site_pair<point2d>,unsigned,util::graph> wshed_t;
122  wshed_t wshed = morpho::meyer_wst(closed_lg_ima, nbh, nbasins);
123  mln_assertion(nbasins == 46);
124 
125  /*---------.
126  | Output. |
127  `---------*/
128 
129  // FIXME: Inlined conversion, to be reifed into a routine.
130 
131  // Save the result in gray levels (data) + color (wshed).
132 
133  // Data.
134  typedef rgb8 output_val_t;
135  typedef image2d<output_val_t> output_t;
136  point2d output_pmin = input.domain().pmin();
137  point2d output_pmax(input.domain().pmax()[0] * 2,
138  input.domain().pmax()[1] * 2);
139  output_t output(box2d(output_pmin, output_pmax));
140  data::fill(output, literal::black);
141  mln_fwd_piter_(image2d<input_val_t>) p(input.domain());
142  for_all(p)
143  {
144  // Equivalent of P in OUTPUT.
145  point2d q(p[0] * 2, p[1] * 2);
146  input_val_t v = input(p);
147  /* FIXME: Use a conversion function from input_val_t to
148  output_val_t instead of an explicit construction. */
149  output(q) = output_val_t(v, v, v);
150  }
151  // Interpolate missing points in OUTPUT.
152  mln_piter_(output_t) p_out(output.domain());
153  for_all(p_out)
154  {
155  // Process points on even rows and odd columns
156  if (p_out[0] % 2 == 0 && p_out[1] % 2 == 1)
157  output(p_out) = (output(p_out + left) + output(p_out + right)) / 2;
158  // Process points on odd rows and even columns
159  if (p_out[0] % 2 == 1 && p_out[1] % 2 == 0)
160  output(p_out) = (output(p_out + up) + output(p_out + down)) / 2;
161  // Process points on odd rows and odd columns
162  if (p_out[0] % 2 == 1 && p_out[1] % 2 == 1)
163  output(p_out) =
164  (output(p_out + dpoint2d(-1, -1)) +
165  output(p_out + dpoint2d(-1, +1)) +
166  output(p_out + dpoint2d(+1, -1)) +
167  output(p_out + dpoint2d(+1, +1))) / 4;
168  }
169  // Draw the watershed.
170  /* FIXME: We should draw the watershed on another image and
171  superimpose it on OUTPUT instead of drawing it directly into
172  OUTPUT. */
173  mln_piter_(wshed_t) pw(wshed.domain());
174  for_all(pw)
175  {
176  if (wshed(pw) == 0)
177  {
178  mln_psite_(lg_ima_t) pp(pw);
179  // Equivalent of the line (edge) PP in OUTPUT.
180  int row1 = pp.first()[0] * 2;
181  int col1 = pp.first()[1] * 2;
182  int row2 = pp.second()[0] * 2;
183  int col2 = pp.second()[1] * 2;
184  point2d q((row1 + row2) / 2, (col1 + col2) / 2);
185  // Print the watershed in red.
186  output(q) = literal::red;
187  }
188  }
189  // Fill the holes, so that the watershed looks connected.
190  /* FIXME: This approach is bad: it creates thick lines of watershed.
191  We should probably solve this when we ``paint'' the watershed
192  over the ``doubled'' image.
193 
194  A better approach is probably to iterate over the set of vertices,
195  and ``connect'' edges according to patterns (vertically or
196  horizontally connected egdes member of the watershed, etc.). */
197  // Reuse the piter on OUTPUT.
198  for_all (p_out)
199  // Only handle points on odd rows and columns.
200  if (p_out[0] % 2 == 1 && p_out[1] % 2 == 1)
201  {
202  // Count the number of adjacent watershed points. If there are
203  // two or more, consider, create a watershed point.
204  /* FIXME: Iterating over a c4 window would be more elegant, of
205  course. */
206  unsigned nwsheds =
207  (output.has(p_out + up ) && output(p_out + up ) == literal::red) +
208  (output.has(p_out + down ) && output(p_out + down ) == literal::red) +
209  (output.has(p_out + left ) && output(p_out + right) == literal::red) +
210  (output.has(p_out + right) && output(p_out + left ) == literal::red);
211  if (nwsheds >= 2)
212  output(p_out) = literal::red;
213  }
214  io::ppm::save(output, "lena_line_graph_image_wst2-out.ppm");
215 }