morpho.hh 14.2 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
// Copyright (C) 2009 EPITA Research and Development Laboratory (LRDE)
//
// This file is part of Olena.
//
// 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,
// 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 Olena.  If not, see <http://www.gnu.org/licenses/>.
//
// As a special exception, you may use this file as part of a free
// software project 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 APPS_GRAPH_MORPHO_MORPHO_HH
# define APPS_GRAPH_MORPHO_MORPHO_HH

/** \file apps/graph-morpho/morpho.hh
30
    \brief Morphological operators on graphs.
31
32
33
34
35
36
37
38

    Reference:

      Jean Cousty, Laurent Najman and Jean Serra.  Some morphological
      operators in graph spaces.  In: Proceedings of the Ninth
      International Symposium on Mathematical Morphology (ISMM), 2009,
      Groningen, The Netherlands.  */

39
# include <mln/core/alias/complex_image.hh>
40
# include <mln/core/image/image2d.hh>
41

42
43
# include <mln/core/image/dmorph/image_if.hh>

44
45
# include <mln/core/image/dmorph/extension_ima.hh>

46
# include <mln/core/routine/extend.hh>
47
48
49
50
51
52
# include <mln/core/routine/duplicate.hh>

# include <mln/core/site_set/p_n_faces_piter.hh>
# include <mln/core/image/complex_neighborhoods.hh>
# include <mln/core/image/complex_neighborhood_piter.hh>

53
54
55
56
# include <mln/world/inter_pixel/dim2/is_pixel.hh>
# include <mln/world/inter_pixel/dim2/is_edge.hh>
# include <mln/world/inter_pixel/neighb2d.hh>

57
58
# include <mln/data/paste.hh>

59
60
# include <mln/morpho/dilation.hh>
# include <mln/morpho/erosion.hh>
61

62
63
# include <mln/topo/is_n_face.hh>

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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
/*---------------.
| Graph traits.  |
`---------------*/

namespace trait
{
  /// Graph traits.
  template <typename I>
  struct graph
  {
    /* Empty by default.

       Specializations must provide these static member functions:

         static F1 is_vertex()
         static F2 is_edge()
         static W1 v2e()
         static W2 e2v()

       where F1, F2, W1 and W2 are types depending on I.
    */
  };

  // ----------------------------------------------------------- //
  // Graph traits for (mln::image2d-based) cubical 2-complexes.  //
  // ----------------------------------------------------------- //

  /// Graph traits for mln::image2d.
  template <typename T>
  struct graph< mln::image2d<T> >
  {
    // Return a functor saying whether a psite is a vertex or not.
    static
    const mln::world::inter_pixel::dim2::is_pixel& is_vertex()
    {
      static mln::world::inter_pixel::dim2::is_pixel is_vertex_fun;
      return is_vertex_fun;
    }

    // Return a functor saying whether a psite is a vertex or not.
    static
    const mln::world::inter_pixel::dim2::is_edge& is_edge()
    {
      static mln::world::inter_pixel::dim2::is_edge is_edge_fun;
      return is_edge_fun;
    }

    // Return a window containing the edges adjacent to a vertex.
    static
    const mln::window2d& v2e()
    {
      return mln::world::inter_pixel::v2e().win();
    }

    // Return a window containing the vertices adjacent to an edge.
    static
    const mln::world::inter_pixel::dbl_window2d& e2v()
    {
      return mln::world::inter_pixel::e2v().win();
    }
  };

  // ---------------------------------------- //
  // Graph traits for (general) 1-complexes.  //
  // ---------------------------------------- //

  /// Graph traits for 1-complexes images.
  template <typename G, typename V>
  struct graph< mln::complex_image<1, G, V> >
  {
    // Return a functor saying whether a psite is a vertex or not.
    static
    const mln::topo::is_n_face<0>& is_vertex()
    {
      static mln::topo::is_n_face<0> is_vertex_fun;
      return is_vertex_fun;
    }

    // Return a functor saying whether a psite is a vertex or not.
    static
    const mln::topo::is_n_face<1>& is_edge()
    {
      static mln::topo::is_n_face<1> is_edge_fun;
      return is_edge_fun;
    }

    // Return a window containing the edges adjacent to a vertex.
    static
    const mln::complex_higher_window<1, G>& v2e()
    {
      static mln::complex_higher_window<1, G> v2e_win;
      return v2e_win;
    }

    // Return a window containing the vertices adjacent to an edge.
    static
    const mln::complex_lower_window<1, G>& e2v()
    {
      static mln::complex_lower_window<1, G> e2v_win;
      return e2v_win;
    }
  };

} // end of namespace trait


171
172
173
/*----------------------------.
| Vertices-edges combinator.  |
`----------------------------*/
174
175

/// Combine the vertices and the edges of two images to create a new
176
/// graph image (``operator'' \f$\ovee\f$).
177
178
179
template <typename I>
inline
mln_concrete(I)
180
combine(const mln::Image<I>& vertices_, const mln::Image<I>& edges_)
181
{
182
  typedef trait::graph<I> T;
183
184
  const I& vertices = mln::exact(vertices_);
  const I& edges = mln::exact(edges_);
185
186
187

  mln_precondition(vertices.domain() == edges.domain());
  mln_concrete(I) output;
188
  mln::initialize(output, vertices);
189
190
191
192
  mln::data::fill(output, false);
  mln::data::paste(vertices | T::is_vertex(), output);
  mln::data::paste(edges | T::is_edge(), output);
  return output;
193
194
195
196
197
198
199
}


/*-------------------------.
| Dilations and erosions.  |
`-------------------------*/

200
201
202
// ----------------------------- //
// Core dilations and erosions.  //
// ----------------------------- //
203

204
/* Note: When writing
205

206
     dilation(ima | vertices);
207

208
209
   `vertices' is a predicate on sites (p2b function), which
   is not efficient, since both vertices and edges will be browsed.
210

211
212
213
   It would be very nice if `vertices' could be an actual site set,
   so that `ima | vertices' creates a morpher smart enough to
   browse /only/ vertices.  */
214
215


216
217
218
219
/// Dilation from edges to vertices (\f$\delta^\bullet\f$).
template <typename I>
inline
mln_concrete(I)
220
dilation_e2v(const mln::Image<I>& input)
221
{
222
223
224
225
226
227
228
229
230
231
  typedef trait::graph<I> T;

  mln_concrete(I) output;
  mln::initialize(output, mln::exact(input));
  mln::data::fill(output, false);
  mln::data::paste(mln::morpho::dilation(mln::extend(input | T::is_vertex(),
						       input),
					   T::v2e()),
		     output);
  return output;
232
233
234
235
236
237
}

/// Erosion from vertices to edges (\f$\epsilon^\times\f$).
template <typename I>
inline
mln_concrete(I)
238
erosion_v2e(const mln::Image<I>& input)
239
{
240
241
242
243
244
245
246
247
248
249
  typedef trait::graph<I> T;

  mln_concrete(I) output;
  mln::initialize(output, mln::exact(input));
  mln::data::fill(output, false);
  mln::data::paste(mln::morpho::erosion(mln::extend(input | T::is_edge(),
						      input),
					  T::e2v()),
		     output);
  return output;
250
251
252
253
254
255
}

/// Erosion from edges to vertices (\f$\epsilon^\bullet\f$).
template <typename I>
inline
mln_concrete(I)
256
erosion_e2v(const mln::Image<I>& input)
257
{
258
259
260
261
262
263
264
265
266
267
  typedef trait::graph<I> T;

  mln_concrete(I) output;
  mln::initialize(output, mln::exact(input));
  mln::data::fill(output, false);
  mln::data::paste(mln::morpho::erosion(mln::extend(input | T::is_vertex(),
						      input),
					   T::v2e()),
		     output);
  return output;
268
269
270
271
272
273
}

/// Dilation from vertices to edges (\f$\delta^\times\f$).
template <typename I>
inline
mln_concrete(I)
274
dilation_v2e(const mln::Image<I>& input)
275
{
276
277
278
279
280
281
282
283
284
285
  typedef trait::graph<I> T;

  mln_concrete(I) output;
  mln::initialize(output, mln::exact(input));
  mln::data::fill(output, false);
  mln::data::paste(mln::morpho::dilation(mln::extend(input | T::is_edge(),
						       input),
					   T::e2v()),
		     output);
  return output;
286
287
288
}


289
290
291
292
// ------------------------------ //
// Other dilations and erosions.  //
// ------------------------------ //

293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
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
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
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
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
/// Vertex dilation (\f$delta\f$).
template <typename I>
inline
mln_concrete(I)
dilation_vertex(const mln::Image<I>& input)
{
  return dilation_e2v(dilation_v2e(input));
}

/// Vertex erosion (\f$epsilon\f$).
template <typename I>
inline
mln_concrete(I)
erosion_vertex(const mln::Image<I>& input)
{
  return erosion_e2v(erosion_v2e(input));
}


/// Edge dilation (\f$Delta\f$).
template <typename I>
inline
mln_concrete(I)
dilation_edge(const mln::Image<I>& input)
{
  return dilation_v2e(dilation_e2v(input));
}

/// Edge erosion (\f$Epsilon\f$).
template <typename I>
inline
mln_concrete(I)
erosion_edge(const mln::Image<I>& input)
{
  return erosion_v2e(erosion_e2v(input));
}


/// Graph dilation (\f$delta \ovee Delta\f$).
template <typename I>
inline
mln_concrete(I)
dilation_graph(const mln::Image<I>& input)
{
  return combine(dilation_vertex(input), dilation_edge(input));
}

/// Graph erosion (\f$epsilon \ovee Epsilon\f$).
template <typename I>
inline
mln_concrete(I)
erosion_graph(const mln::Image<I>& input)
{
  return combine(erosion_vertex(input), erosion_edge(input));
}


/*-------------------------.
| Additional adjunctions.  |
`-------------------------*/

template <typename I>
inline
mln_concrete(I)
alpha1(const mln::Image<I>& input)
{
  mln_concrete(I) vertices;
  mln::initialize(vertices, input);
  mln::data::fill(vertices, true);
  return combine(vertices, input);
}

template <typename I>
inline
mln_concrete(I)
beta1(const mln::Image<I>& input)
{
  return combine(dilation_e2v(input), input);
}

template <typename I>
inline
mln_concrete(I)
alpha2(const mln::Image<I>& input)
{
  return combine(input, erosion_v2e(input));
}

template <typename I>
inline
mln_concrete(I)
beta2(const mln::Image<I>& input)
{
  mln_concrete(I) edges;
  mln::initialize(edges, input);
  mln::data::fill(edges, false);
  return combine(input, edges);
}

template <typename I>
inline
mln_concrete(I)
alpha3(const mln::Image<I>& input)
{
  return combine(erosion_e2v(input), erosion_v2e(erosion_e2v(input)));
}

template <typename I>
inline
mln_concrete(I)
beta3(const mln::Image<I>& input)
{
  return combine(dilation_e2v(dilation_v2e(input)), dilation_v2e(input));
}


/*------------------------.
| Openings and closings.  |
`------------------------*/

/// Vertex opening (\f$\gamma_1\f$).
template <typename I>
inline
mln_concrete(I)
opening_vertex(const mln::Image<I>& input)
{
  return dilation_vertex(erosion_vertex(input));
}

/// Vertex closing (\f$\phi_1\f$).
template <typename I>
inline
mln_concrete(I)
closing_vertex(const mln::Image<I>& input)
{
  return erosion_vertex(dilation_vertex(input));
}


/// Edge opening (\f$\Gamma_1\f$).
template <typename I>
inline
mln_concrete(I)
opening_edge(const mln::Image<I>& input)
{
  return dilation_edge(erosion_edge(input));
}

/// Edge closing (\f$\Phi_1\f$).
template <typename I>
inline
mln_concrete(I)
closing_edge(const mln::Image<I>& input)
{
  return erosion_edge(dilation_edge(input));
}


/// Graph opening (\f${\gamma \ovee \Gamma}_1\f$).
template <typename I>
inline
mln_concrete(I)
opening_graph(const mln::Image<I>& input)
{
  return combine(opening_vertex(input), opening_edge(input));
}

/// Graph closing (\f${\phi \ovee \Phi}_1\f$).
template <typename I>
inline
mln_concrete(I)
closing_graph(const mln::Image<I>& input)
{
  return combine(closing_vertex(input), closing_edge(input));
}


/*----------------------------------.
| Half-openings and half-closings.  |
`----------------------------------*/

/// Vertex half-opening (\f$\gamma_{1/2}\f$).
template <typename I>
inline
mln_concrete(I)
half_opening_vertex(const mln::Image<I>& input)
{
  return dilation_e2v(erosion_v2e(input));
}

/// Vertex half-closing (\f$\phi_{1/2}\f$).
template <typename I>
inline
mln_concrete(I)
half_closing_vertex(const mln::Image<I>& input)
{
  return erosion_e2v(dilation_v2e(input));
}


/// Edge half-opening (\f$\Gamma_{1/2}\f$).
template <typename I>
inline
mln_concrete(I)
half_opening_edge(const mln::Image<I>& input)
{
  return dilation_v2e(erosion_e2v(input));
}

/// Edge half-closing (\f$\Phi_{1/2}\f$).
template <typename I>
inline
mln_concrete(I)
half_closing_edge(const mln::Image<I>& input)
{
  return erosion_v2e(dilation_e2v(input));
}


/// Graph half-opening (\f${\gamma \ovee \Gamma}_{1/2}\f$).
template <typename I>
inline
mln_concrete(I)
half_opening_graph(const mln::Image<I>& input)
{
  return combine(half_opening_vertex(input), half_opening_edge(input));
}

/// Graph half-closing (\f${\phi \ovee \Phi}_{1/2}\f$).
template <typename I>
inline
mln_concrete(I)
half_closing_graph(const mln::Image<I>& input)
{
  return combine(half_closing_vertex(input), half_closing_edge(input));
}


/*-------------------------------------------------------.
| Parameterized openings and closings (granulometries).  |
`-------------------------------------------------------*/

/// Opening (\f${\gamma \ovee \Gamma}_{\lambda/2}\f$).
template <typename I>
inline
mln_concrete(I)
opening(const mln::Image<I>& input, unsigned lambda)
{
  unsigned i = lambda / 2;
  unsigned j = lambda % 2;
  mln_concrete(I) output = mln::duplicate(input);
  for (unsigned m = 0; m < i; ++m)
    output = erosion_graph(output);
  for (unsigned m = 0; m < j; ++m)
    output = half_opening_graph(output);
  for (unsigned m = 0; m < i; ++m)
    output = dilation_graph(output);
  return output;
}

/// Opening (\f${\phi \ovee \Phi}_{\lambda/2}\f$).
template <typename I>
inline
mln_concrete(I)
closing(const mln::Image<I>& input, unsigned lambda)
{
  unsigned i = lambda / 2;
  unsigned j = lambda % 2;
  mln_concrete(I) output = mln::duplicate(input);
  for (unsigned m = 0; m < i; ++m)
    output = dilation_graph(output);
  for (unsigned m = 0; m < j; ++m)
    output = half_closing_graph(output);
  for (unsigned m = 0; m < i; ++m)
    output = erosion_graph(output);
  return output;
}

/*-------------------------------.
| Alternate Sequential Filters.  |
`-------------------------------*/

/// Alternate Sequential Filter (ASF) (\f${ASF}_{\lambda/2}\f$).
template <typename I>
inline
mln_concrete(I)
asf(const mln::Image<I>& input, unsigned lambda)
{
  mln_concrete(I) output = mln::duplicate(input);
582
583
  for (unsigned m = 1; m <= lambda; ++m)
    output = opening(closing(output, m), m);
584
585
586
587
  return output;
}

#endif // ! APPS_GRAPH_MORPHO_MORPHO_HH