flooding.hh 11.6 KB
Newer Older
1
// Copyright (C) 2008, 2009 EPITA Research and Development Laboratory (LRDE)
2
//
3
// This file is part of the Milena Library.  This library is free
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
// 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

31
/// \file
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
///
/// 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>

# 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>

52
53
# include <mln/extension/adjust_fill.hh>

54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75

namespace mln
{

  namespace morpho
  {

    namespace watershed
    {

      /// 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.

76
      template <typename L, typename I, typename N>
77
      mln_ch_value(I, L)
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
      flooding(const Image<I>& input, const Neighborhood<N>& nbh,
	       L& n_basins);

      /// \brief Meyer's Watershed Transform (WST) algorithm, with no
      /// count of basins.
      /// 
      /// \param[in]  input   The input image.
      /// \param[in]  nbh     The connexity of markers.
      /// 
      /// \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.
      /// 
      /// Note that the first parameter, \p L, is not automatically
      /// valued from the type of the actual argument during implicit
      /// instantiation: you have to explicitly pass this parameter at
      /// call sites.
    template <typename L, typename I, typename N>
    mln_ch_value(I, L)
    flooding(const Image<I>& input, const Neighborhood<N>& nbh);
100
101
102
103
104
105
106
107
108
109
110
111
112
113



# ifndef MLN_INCLUDE_ONLY


      // Implementations.

      namespace impl
      {

	namespace generic
	{

114
	  template <typename L, typename I, typename N>
115
	  mln_ch_value(I, L)
116
117
	  flooding(const Image<I>& input_, const Neighborhood<N>& nbh_,
		   L& n_basins)
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
145
146
147
148
149
150
	  {
	    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);
151
	    for_all(p)
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
	      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();
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
199
200
201
202
203
204
		// 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;
			}
		  }
	      }
205

206
207
208
209
	    trace::exiting("morpho::watershed::impl::generic::flooding");
	    return output;
	  }

210
	} // end of namespace mln::morpho::watershed::impl::generic
211
212
213



214
	// Fastest version.
215

216
217
	template <typename I, typename N, typename L>
	mln_ch_value(I, L)
218
219
	flooding_fastest(const Image<I>& input_, const Neighborhood<N>& nbh_,
			 L& n_basins)
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
263
264
265
266
267
268
	{
	  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;
		  }
269

270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
	  /* 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];
287
288
		  // In the border, output is unmarked so N is ignored.
		  if (output.element(n) != unmarked)
289
290
291
		    {
		      if (adjacent_marker == unmarked)
			{
292
			  adjacent_marker = output.element(n);
293
294
295
			  single_adjacent_marker_p = true;
			}
		      else
296
			if (adjacent_marker != output.element(n))
297
298
299
300
301
			  {
			    single_adjacent_marker_p = false;
			    break;
			  }
		    }
302
303
304
305
306
307
308
309
310
311
312
313
		}
	      /* 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
314
315
			  // In the border, in_queue is true so N is ignored.
			  && ! in_queue.element(n))
316
			{
317
318
			  queue.push(max - input.element(n), n);
			  in_queue.element(n) = true;
319
			}
320
321
322
		    }
		}
	    }
323

324
325
326
	  trace::exiting("morpho::watershed::impl::flooding_fastest");
	  return output;
	}
327
328
329
330
331
332
333
334
335
336
337
338
339
340


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



      // Dispatch.

      namespace internal
      {

	template <typename I, typename N, typename L>
	inline
	mln_ch_value(I, L)
341
342
343
	flooding_dispatch(metal::false_,
			  const Image<I>& input, const Neighborhood<N>& nbh,
			  L& n_basins)
344
345
346
347
348
349
350
351
	{
	  return impl::generic::flooding(input, nbh, n_basins);
	}


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

	template <typename I, typename N, typename L>
	inline
	mln_ch_value(I, L)
362
363
	flooding_dispatch(const Image<I>& input, const Neighborhood<N>& nbh,
			  L& n_basins)
364
365
366
367
368
369
370
371
372
373
374
	{
	  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);
	}

375
      } // end of namespace mln::morpho::watershed::internal
376
377


378
      // Facades.
379

380
      template <typename L, typename I, typename N>
381
382
      inline
      mln_ch_value(I, L)
383
      flooding(const Image<I>& input, const Neighborhood<N>& nbh, L& n_basins)
384
385
386
387
388
      {
	trace::entering("morpho::watershed::flooding");

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

389
390
	mln_ch_value(I, L) output =
	  internal::flooding_dispatch(input, nbh, n_basins);
391
392
393
394
395

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

396
397
398
399
400
401
402
403
    template <typename L, typename I, typename N>
    mln_ch_value(I, L)
    flooding(const Image<I>& input, const Neighborhood<N>& nbh)
    {
      L nbasins;
      return flooding<L>(input, nbh, nbasins);
    }

404
405
406
407
408
409
410
411
412
413
# 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