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
	    /* 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);

135
136
137
138
	    // Initialize the output with the markers (minima
	    // components).
	    typedef mln_ch_value(I, L) O;
	    O output = labeling::regional_minima(input, nbh, n_basins);
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155

	    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);
156
	    for_all(p)
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
	      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();
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
209
		// 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;
			}
		  }
	      }
210

211
212
213
	    return output;
	  }

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



218
	// Fastest version.
219

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

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

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


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



      // Dispatch.

      namespace internal
      {

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


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

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

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


381
      // Facades.
382

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

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

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

	return output;
      }

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

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