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