flooding.hh 11.5 KB
Newer Older
1
// Copyright (C) 2008, 2009 EPITA Research and Development Laboratory (LRDE)
2
//
3
// This file is part of Olena.
4
//
5
6
7
8
9
// 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,
10
11
12
13
14
// 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
15
// along with Olena.  If not, see <http://www.gnu.org/licenses/>.
16
17
//
// As a special exception, you may use this file as part of a free
18
// software project without restriction.  Specifically, if other files
19
// instantiate templates or use macros or inline functions from this
20
21
22
23
24
// 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.
25
26
27
28

#ifndef MLN_MORPHO_WATERSHED_FLOODING_HH
# define MLN_MORPHO_WATERSHED_FLOODING_HH

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

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

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

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.

74
      template <typename L, typename I, typename N>
75
      mln_ch_value(I, L)
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
      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);
98
99
100
101
102
103
104
105
106
107
108
109
110
111



# ifndef MLN_INCLUDE_ONLY


      // Implementations.

      namespace impl
      {

	namespace generic
	{

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

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

208
	} // end of namespace mln::morpho::watershed::impl::generic
209
210
211



212
	// Fastest version.
213

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

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

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


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



      // Dispatch.

      namespace internal
      {

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


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

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

373
      } // end of namespace mln::morpho::watershed::internal
374
375


376
      // Facades.
377

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

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

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

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

394
395
396
397
398
399
400
401
    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);
    }

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