flooding.hh 11.4 KB
Newer Older
1
2
// Copyright (C) 2008, 2009, 2012 EPITA Research and Development
// Laboratory (LRDE)
3
//
4
// This file is part of Olena.
5
//
6
7
8
9
10
// Olena is free software: you can redistribute it and/or modify it under
// the terms of the GNU General Public License as published by the Free
// Software Foundation, version 2 of the License.
//
// Olena is distributed in the hope that it will be useful,
11
12
13
14
15
// 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
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
22
23
24
25
// 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.
26
27
28
29

#ifndef MLN_MORPHO_WATERSHED_FLOODING_HH
# define MLN_MORPHO_WATERSHED_FLOODING_HH

30
/// \file
31
///
32
/// \brief Meyer's Watershed Transform (WST) algorithm.
33
///
34
35
/// The Watershed Transform algorithm from Meyer using a hierarchical
/// queue.
36
///
37
38
39
40
/// 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.
41
42
43
44
45
46
47
48
49
50

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

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

53
54
55
56
57
58
59
60
61
62

namespace mln
{

  namespace morpho
  {

    namespace watershed
    {

63
64
65
66
67
68
69
70
71
72
73
74
75
76
      /*!  \brief 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.
       *
       * \ingroup mlnmorphowst
       */
77
      template <typename L, typename I, typename N>
78
      mln_ch_value(I, L)
79
80
81
      flooding(const Image<I>& input, const Neighborhood<N>& nbh,
	       L& n_basins);

82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
      /*! \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.
       *
       *  \ingroup mlnmorphowst
       */
101
102
103
    template <typename L, typename I, typename N>
    mln_ch_value(I, L)
    flooding(const Image<I>& input, const Neighborhood<N>& nbh);
104
105
106
107
108
109
110
111
112
113
114
115
116
117



# ifndef MLN_INCLUDE_ONLY


      // Implementations.

      namespace impl
      {

	namespace generic
	{

118
	  template <typename L, typename I, typename N>
119
	  mln_ch_value(I, L)
120
121
	  flooding(const Image<I>& input_, const Neighborhood<N>& nbh_,
		   L& n_basins)
122
	  {
Guillaume Lazzara's avatar
Guillaume Lazzara committed
123
	    mln_trace("morpho::watershed::impl::generic::flooding");
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
151
152
153
154
	    /* 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);
155
	    for_all(p)
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
	      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();
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
205
206
207
208
		// 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;
			}
		  }
	      }
209

210
211
212
	    return output;
	  }

213
	} // end of namespace mln::morpho::watershed::impl::generic
214
215
216



217
	// Fastest version.
218

219
220
	template <typename I, typename N, typename L>
	mln_ch_value(I, L)
221
222
	flooding_fastest(const Image<I>& input_, const Neighborhood<N>& nbh_,
			 L& n_basins)
223
	{
Guillaume Lazzara's avatar
Guillaume Lazzara committed
224
	  mln_trace("morpho::watershed::impl::flooding_fastest");
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
269
270
271
	  /* 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;
		  }
272

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

327
328
	  return output;
	}
329
330
331
332
333
334
335
336
337
338
339
340
341
342


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



      // Dispatch.

      namespace internal
      {

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


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

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

377
      } // end of namespace mln::morpho::watershed::internal
378
379


380
      // Facades.
381

382
      template <typename L, typename I, typename N>
383
384
      inline
      mln_ch_value(I, L)
385
      flooding(const Image<I>& input, const Neighborhood<N>& nbh, L& n_basins)
386
      {
Guillaume Lazzara's avatar
Guillaume Lazzara committed
387
	mln_trace("morpho::watershed::flooding");
388
389
390

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

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

	return output;
      }

397
398
399
400
401
402
403
404
    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);
    }

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