1 ## Copyright (C) 2010 Soren Hauberg
3 ## This program is free software; you can redistribute it and/or modify
4 ## it under the terms of the GNU General Public License as published by
5 ## the Free Software Foundation; either version 3 of the License, or
6 ## (at your option) any later version.
8 ## This program is distributed in the hope that it will be useful,
9 ## but WITHOUT ANY WARRANTY; without even the implied warranty of
10 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 ## GNU General Public License for more details.
13 ## You should have received a copy of the GNU General Public License
14 ## along with this program; If not, see <http://www.gnu.org/licenses/>.
17 ## @deftypefn {Function File} {@var{cc} = } bwconncomp (@var{BW})
18 ## @deftypefnx {Function File} {@var{cc} = } bwconncomp (@var{BW}, @var{connectivity})
19 ## Trace the boundaries of objects in a binary image.
21 ## @code{bwconncomp} traces the boundaries of objects in a binary image @var{BW}
22 ## and returns information about them in a structure with the following fields.
26 ## The connectivity used in the boundary tracing.
28 ## The size of the image @var{BW}.
30 ## The number of objects in the image @var{BW}.
32 ## A cell array containing where each element corresponds to an object in @var{BW}.
33 ## Each element is represented as a vector of linear indices of the boundary of
37 ## The connectivity used in the tracing is by default 4, but can be changed
38 ## by setting the @var{connectivity} input parameter to 8. Sadly, this is not
40 ## @seealso{bwlabel, bwboundaries, ind2sub}
43 function CC = bwconncomp (bw, N = 4)
46 error ("bwconncomp: not enough input arguments");
48 if (!ismatrix (bw) || ndims (bw) != 2)
49 error ("bwconncomp: first input argument must be a NxM matrix");
51 if (!isscalar (N) || !any (N == [4])) #, 8]))
52 error ("bwconncomp: second input argument must be 4");
56 B = bwboundaries (bw, N);
58 ## Convert from (x, y) index to linear indexing
59 P = cell (numel (B), 1);
61 P {k} = sub2ind (size (bw), B {k} (:, 2), B {k} (:, 1));
65 CC = struct ("Connectivity", N, "ImageSize", size (bw), "NumObjects", numel (B));