1## Copyright (C) 2014 Carnë Draug <carandraug@octave.org>
2##
3## This program is free software; you can redistribute it and/or
4## modify it under the terms of the GNU General Public License as
5## published by the Free Software Foundation; either version 3 of the
6## License, or (at your option) any later version.
7##
8## This program is distributed in the hope that it will be useful, but
9## WITHOUT ANY WARRANTY; without even the implied warranty of
10## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11## General Public License for more details.
12##
13## You should have received a copy of the GNU General Public License
14## along with this program; if not, see
15## <http://www.gnu.org/licenses/>.
16
17## -*- texinfo -*-
18## @deftypefn  {Function File} {} imregionalmax (@var{img})
19## @deftypefnx {Function File} {} imregionalmax (@var{img}, @var{conn})
20## Compute regional maxima.
21##
22## Returns a logical matrix, same size as the input @var{img}, with the
23## regional maxima.
24##
25## The optional argument @var{conn}, defines the connectivity.  It can
26## be a scalar value or a boolean matrix (see @code{conndef} for details).
27## Defaults to @code{conndef (ndims (@var{img}), "maximal")}
28##
29## Regional maxima should not be mistaken with local maxima.  Local maxima
30## are pixels whose value is greater or equal to all of its neighbors.
31## A regional maxima is the connected component of pixels whose values are
32## all higher than the neighborhood of the maxima (the connected component,
33## not its individual pixels).
34## All pixels belonging to a regional maximum are local maxima, but the
35## inverse is not true.
36##
37## @seealso{immaximas, imreconstruct, imregionalmin}
38## @end deftypefn
39
40function bw = imregionalmax (img, conn)
41  if (nargin < 1 || nargin > 2)
42    print_usage ();
43  endif
44
45  if (nargin < 2)
46    conn = conndef (ndims (img), "maximal");
47  else
48    conn = conndef (conn);
49  endif
50
51  if (islogical (img))
52    bw = img;
53  else
54    ## we could probably still make this more efficient
55    if (isfloat (img))
56      recon = imreconstruct (img, img + __eps__ (img), conn);
57    else
58      recon = imreconstruct (img, img + 1, conn);
59    endif
60    bw = (recon == img);
61  endif
62endfunction
63
64%!test
65%! a = [
66%!    7    3    9    3   10    3
67%!    4    2    3   10    1    3
68%!    1    4    6    9    4   10
69%!    8    7    9    3    4    8
70%!    5    9    3    3    8    9
71%!    3    6    9    4    1   10];
72%!
73%! a4 = [
74%!    1    0    1    0    1    0
75%!    0    0    0    1    0    0
76%!    0    0    0    0    0    1
77%!    1    0    1    0    0    0
78%!    0    1    0    0    0    0
79%!    0    0    1    0    0    1];
80%! assert (imregionalmax (a, 4), logical (a4))
81%! a8 = [
82%!    1    0    0    0    1    0
83%!    0    0    0    1    0    0
84%!    0    0    0    0    0    1
85%!    0    0    0    0    0    0
86%!    0    0    0    0    0    0
87%!    0    0    0    0    0    1];
88%! assert (imregionalmax (a, 8), logical (a8))
89%! assert (imregionalmax (a), logical (a8))
90
91%!test
92%! ## test float input images <bug #51724>
93%! im0 = peaks ();
94%! im1 = im0 ./ 100;
95%! max_pos_expected = [1000; 1214; 1691; 2353];
96%! max0 = imregionalmax (im0);
97%! max0_pos = find (max0);
98%! max1 = imregionalmax (im1);
99%! assert (max1, max0)
100%! assert (max0_pos, max_pos_expected)
101