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} {} imregionalmin (@var{img})
19## @deftypefnx {Function File} {} imregionalmin (@var{img}, @var{conn})
20## Compute regional minima.
21##
22## Returns a logical matrix, same size as the input @var{img}, with the
23## regional minima.
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 minima should not be mistaken with local minima.  Local minima
30## are pixels whose value is less or equal to all of its neighbors.
31## A regional minima is the connected component of pixels whose values are
32## all less than the neighborhood of the minima (the connected component,
33## not its individual pixels).
34## All pixels belonging to a regional minima are local minima, but the
35## inverse is not true.
36##
37## @seealso{imreconstruct, imregionalmax}
38## @end deftypefn
39
40function bw = imregionalmin (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 (isfloat (img))
52    img = - img;
53  else
54    img = imcomplement (img);
55  endif
56  bw = imregionalmax (img, conn);
57endfunction
58
59%!test
60%! a = [
61%!    7    3    9    3   10    3
62%!    4    2    3   10    1    3
63%!    1    4    6    9    4   10
64%!    8    7    9    3    4    8
65%!    5    9    3    3    8    9
66%!    3    6    9    4    1   10];
67%!
68%! a4 = logical ([
69%!    0    0    0    1    0    0
70%!    0    1    0    0    1    0
71%!    1    0    0    0    0    0
72%!    0    0    0    1    0    0
73%!    0    0    1    1    0    0
74%!    1    0    0    0    1    0]);
75%! assert (imregionalmin (a, 4), a4)
76%! assert (imregionalmin (uint8 (a), 4), a4)
77%! assert (imregionalmin (int8 (a), 4), a4)
78%!
79%! a8 = logical ([
80%!    0    0    0    0    0    0
81%!    0    0    0    0    1    0
82%!    1    0    0    0    0    0
83%!    0    0    0    0    0    0
84%!    0    0    0    0    0    0
85%!    1    0    0    0    1    0]);
86%! assert (imregionalmin (a), a8)
87%! assert (imregionalmin (a, 8), a8)
88%! assert (imregionalmin (uint8 (a), 8), a8)
89%! assert (imregionalmin (int8 (a), 8), a8)
90
91%!test
92%! a = [
93%!   4   8   5  -1   8   7
94%!  -1   4   0   7   1   1
95%!   6   1   2   6   7   0
96%!   6   1   5  -2   5   9
97%!   1   4  -1   0   0   2
98%!   4   6   1   0   7   1];
99%!
100%! a4 = logical ([
101%!   0   0   0   1   0   0
102%!   1   0   1   0   0   0
103%!   0   1   0   0   0   1
104%!   0   1   0   1   0   0
105%!   1   0   1   0   0   0
106%!   0   0   0   0   0   1]);
107%! assert (imregionalmin (a, 4), a4)
108%! assert (imregionalmin (int8 (a), 4), a4)
109%!
110%! a8 = logical ([
111%!   0   0   0   1   0   0
112%!   1   0   0   0   0   0
113%!   0   0   0   0   0   1
114%!   0   0   0   1   0   0
115%!   0   0   0   0   0   0
116%!   0   0   0   0   0   0]);
117%! assert (imregionalmin (a), a8)
118%! assert (imregionalmin (a, 8), a8)
119%! assert (imregionalmin (int8 (a), 8), a8)
120
121%!test
122%! ## test float input images <bug #51724>
123%! im0 = peaks ();
124%! im1 = im0 ./ 100;
125%! max_pos_expected = [1; 49; 664; 1286; 1302; 2401];
126%! max0 = imregionalmin (im0);
127%! max0_pos = find (max0);
128%! max1 = imregionalmin (im1);
129%! assert (max1, max0)
130%! assert (max0_pos, max_pos_expected)
131