flooding.hh 10.9 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
// Copyright (C) 2008 EPITA Research and Development Laboratory (LRDE)
//
// This file is part of the Olena Library.  This library is free
// software; you can redistribute it and/or modify it under the terms
// of the GNU General Public License version 2 as published by the
// Free Software Foundation.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
// General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this library; see the file COPYING.  If not, write to
// the Free Software Foundation, 51 Franklin Street, Fifth Floor,
// Boston, MA 02111-1307, USA.
//
// As a special exception, you may use this file as part of a free
// software library without restriction.  Specifically, if other files
// instantiate templates or use macros or inline functions from this
// file, or you compile this file and link it with other files to
// produce an executable, this file does not by itself cause the
// resulting executable to be covered by the GNU General Public
// License.  This exception does not however invalidate any other
// reasons why the executable file might be covered by the GNU General
// Public License.

#ifndef MLN_MORPHO_WATERSHED_FLOODING_HH
# define MLN_MORPHO_WATERSHED_FLOODING_HH

/// \file mln/morpho/watershed/flooding.hh
///
/// Meyer's Watershed Transform (WST) algorithm.
///
///    The Watershed Transform algorithm from Meyer using a hierarchical
///    queue.
///
///    Reference:
///      Fernand Meyer.  Un algorithme optimal de ligne de partage des
///      eaux. In: Actes du 8me Congrs AFCET, Lyon-Villeurbanne, France
///      (1991), pages 847--859.

# include <mln/trait/ch_value.hh>

// FIXME: See below.
# include <mln/util/greater_psite.hh>
# include <mln/morpho/includes.hh>
# include <mln/literal/zero.hh>
# include <mln/labeling/regional_minima.hh>

# include <mln/core/site_set/p_queue_fast.hh>
# include <mln/core/site_set/p_priority.hh>

54
55
# include <mln/extension/adjust_fill.hh>

56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144

namespace mln
{

  namespace morpho
  {

    namespace watershed
    {

      /*
	FIXME:
	Provide also a version of the algorithm taking an image
	of minima as input.

	FIXME:
	See also the interface of the Shortest-Path Watershed
	Transform, which proposes to lower-complete the image before
	processing it.  Then, add a reference to
	mln/morpho/lower_completion.hh.
      */

      /// Meyer's Watershed Transform (WST) algorithm.
      ///
      /// \param[in]  input    The input image.
      /// \param[in]  nbh      The connexity of markers.
      /// \param[out] n_basins The number of basins.
      ///
      /// \li \p L is the type of labels, used to number the watershed
      /// itself (with the minimal value), and the basins.
      /// \li \p I is the exact type of the input image.
      /// \li \p N is the exact type of the neighborhood used to express
      /// \a input's connexity.

      template <typename I, typename N, typename L>
      mln_ch_value(I, L)
	flooding(const Image<I>& input, const Neighborhood<N>& nbh,
		 L& n_basins);



# ifndef MLN_INCLUDE_ONLY


      // Implementations.

      namespace impl
      {

	namespace generic
	{

	  template <typename I, typename N, typename L>
	  mln_ch_value(I, L)
	    flooding(const Image<I>& input_, const Neighborhood<N>& nbh_,
		     L& n_basins)
	  {
	    trace::entering("morpho::watershed::impl::generic::flooding");
	    /* FIXME: Ensure the input image has scalar values.  */

	    const I input = exact(input_);
	    const N nbh = exact(nbh_);

	    typedef L marker;
	    const marker unmarked = literal::zero;

	    typedef mln_value(I) V;
	    const V max = mln_max(V);

	    // Initialize the output with the markers (minima components).
	    mln_ch_value(I, marker) output =
	      labeling::regional_minima (input, nbh, n_basins);

	    typedef mln_psite(I) psite;

	    // Ordered queue.
	    typedef p_queue_fast<psite> Q;
	    p_priority<V, Q> queue;

	    // In_queue structure to avoid processing sites several times.
	    mln_ch_value(I, bool) in_queue;
	    initialize(in_queue, input);
	    data::fill(in_queue, false);

	    // Insert every neighbor P of every marked area in a
	    // hierarchical queue, with a priority level corresponding to
	    // the grey level input(P).
	    mln_piter(I) p(output.domain());
	    mln_niter(N) n(nbh, p);
145
	    for_all(p)
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
	      if (output(p) == unmarked)
		for_all(n)
		  if (output.domain().has(n) && output(n) != unmarked)
		    {
		      queue.push(max - input(p), p);
		      in_queue(p) = true;
		      break;
		    }

	    /* Until the queue is empty, extract a psite P from the
	       hierarchical queue, at the highest priority level, that is,
	       the lowest level.  */
	    while (! queue.is_empty())
	      {
		psite p = queue.front();
		queue.pop();
162

163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
		// Last seen marker adjacent to P.
		marker adjacent_marker = unmarked;
		// Has P a single adjacent marker?
		bool single_adjacent_marker_p = true;
		mln_niter(N) n(nbh, p);
		for_all(n)
		  if (output.domain().has(n) && output(n) != unmarked)
		    {
		      if (adjacent_marker == unmarked)
			{
			  adjacent_marker = output(n);
			  single_adjacent_marker_p = true;
			}
		      else
			if (adjacent_marker != output(n))
			  {
			    single_adjacent_marker_p = false;
			    break;
			  }
		    }
		/* If the neighborhood of P contains only psites with the
		   same label, then P is marked with this label, and its
		   neighbors that are not yet marked are put into the
		   hierarchical queue.  */
		if (single_adjacent_marker_p)
		  {
		    output(p) = adjacent_marker;
		    for_all(n)
		      if (output.domain().has(n) && output(n) == unmarked
			  && ! in_queue(n))
			{
			  queue.push(max - input(n), n);
			  in_queue(n) = true;
			}
		  }
	      }
199

200
201
202
203
	    trace::exiting("morpho::watershed::impl::generic::flooding");
	    return output;
	  }

204
	} // end of namespace mln::morpho::watershed::impl::generic
205
206
207



208
	// Fastest version.
209

210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
	template <typename I, typename N, typename L>
	mln_ch_value(I, L)
	  flooding_fastest(const Image<I>& input_, const Neighborhood<N>& nbh_,
			   L& n_basins)
	{
	  trace::entering("morpho::watershed::impl::flooding_fastest");
	  /* FIXME: Ensure the input image has scalar values.  */

	  const I input = exact(input_);
	  const N nbh = exact(nbh_);

	  typedef L marker;
	  const marker unmarked = literal::zero;

	  typedef mln_value(I) V;
	  const V max = mln_max(V);

	  extension::adjust_fill(input, nbh, max);

	  // Initialize the output with the markers (minima components).
	  typedef mln_ch_value(I, L) O;
	  O output = labeling::regional_minima(input, nbh, n_basins);
	  extension::fill(output, unmarked);

	  // Ordered queue.
	  typedef p_queue_fast<unsigned> Q;
	  p_priority<V, Q> queue;

	  // FIXME:  With  typedef std::pair<V, unsigned> VU;
	  //               std::priority_queue<VU> queue;
	  // we do not get the same results!!!

	  // In_queue structure to avoid processing sites several times.
	  mln_ch_value(I, bool) in_queue;
	  initialize(in_queue, input);
	  data::fill(in_queue, false);
	  extension::fill(in_queue, true);

	  // Insert every neighbor P of every marked area in a
	  // hierarchical queue, with a priority level corresponding to
	  // the grey level input(P).
	  mln_pixter(const O)    p_out(output);
	  mln_nixter(const O, N) n_out(p_out, nbh);
	  for_all(p_out)
	    if (p_out.val() == unmarked)
	      for_all(n_out)
		if (n_out.val() != unmarked)
		  {
		    unsigned po = p_out.offset();
		    queue.push(max - input.element(po), po);
		    in_queue.element(po) = true;
		    break;
		  }
263

264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
	  /* Until the queue is empty, extract a psite P from the
	     hierarchical queue, at the highest priority level, that is,
	     the lowest level.  */
	  util::array<int> dp = offsets_wrt(input, nbh);
	  const unsigned n_nbhs = dp.nelements();
	  while (! queue.is_empty())
	    {
	      unsigned p = queue.front();
	      queue.pop();

	      // Last seen marker adjacent to P.
	      marker adjacent_marker = unmarked;
	      // Has P a single adjacent marker?
	      bool single_adjacent_marker_p = true;
	      for (unsigned i = 0; i < n_nbhs; ++i)
		{
		  unsigned n = p + dp[i];
		  if (output.element(n) != unmarked) // In the border, output is unmarked so n is ignored.
282
283
284
		    {
		      if (adjacent_marker == unmarked)
			{
285
			  adjacent_marker = output.element(n);
286
287
288
			  single_adjacent_marker_p = true;
			}
		      else
289
			if (adjacent_marker != output.element(n))
290
291
292
293
294
			  {
			    single_adjacent_marker_p = false;
			    break;
			  }
		    }
295
296
297
298
299
300
301
302
303
304
305
306
307
		}
	      /* If the neighborhood of P contains only psites with the
		 same label, then P is marked with this label, and its
		 neighbors that are not yet marked are put into the
		 hierarchical queue.  */
	      if (single_adjacent_marker_p)
		{
		  output.element(p) = adjacent_marker;
		  for (unsigned i = 0; i < n_nbhs; ++i)
		    {
		      unsigned n = p + dp[i];
		      if (output.element(n) == unmarked
			  && ! in_queue.element(n)) // In the border, in_queue is true so n is ignored.
308
			{
309
310
			  queue.push(max - input.element(n), n);
			  in_queue.element(n) = true;
311
			}
312
313
314
		    }
		}
	    }
315

316
317
318
	  trace::exiting("morpho::watershed::impl::flooding_fastest");
	  return output;
	}
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345


      } // end of namespace mln::morpho::watershed::impl



      // Dispatch.

      namespace internal
      {

	template <typename I, typename N, typename L>
	inline
	mln_ch_value(I, L)
	  flooding_dispatch(metal::false_,
			    const Image<I>& input, const Neighborhood<N>& nbh, L& n_basins)
	{
	  return impl::generic::flooding(input, nbh, n_basins);
	}


	template <typename I, typename N, typename L>
	inline
	mln_ch_value(I, L)
	  flooding_dispatch(metal::true_,
			    const Image<I>& input, const Neighborhood<N>& nbh, L& n_basins)
	{
346
 	  return impl::flooding_fastest(input, nbh, n_basins);
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
	}

	template <typename I, typename N, typename L>
	inline
	mln_ch_value(I, L)
	  flooding_dispatch(const Image<I>& input, const Neighborhood<N>& nbh, L& n_basins)
	{
	  enum {
	    test = mlc_equal(mln_trait_image_speed(I),
			     trait::image::speed::fastest)::value
	    &&
	    mln_is_simple_neighborhood(N)::value
	  };
	  return flooding_dispatch(metal::bool_<test>(),
				   input, nbh, n_basins);
	}

364
      } // end of namespace mln::morpho::watershed::internal
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393


      // Facade.

      template <typename I, typename N, typename L>
      inline
      mln_ch_value(I, L)
	flooding(const Image<I>& input, const Neighborhood<N>& nbh, L& n_basins)
      {
	trace::entering("morpho::watershed::flooding");

	// FIXME: internal::flooding_tests(input, nbh, n_basins);

	mln_ch_value(I, L) output = internal::flooding_dispatch(input, nbh, n_basins);

	trace::exiting("morpho::watershed::flooding");
	return output;
      }

# endif // ! MLN_INCLUDE_ONLY

    } // end of namespace mln::morpho::watershed

  } // end of namespace mln::morpho

} // end of namespace mln


#endif // ! MLN_MORPHO_WATERSHED_FLOODING_HH