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