1 ## Copyright (C) 2010 Soren Hauberg
2 ## Modified December 2010 by Andrew Kelly, IPS Radio & Space Services
4 ## This program is free software; you can redistribute it and/or modify
5 ## it under the terms of the GNU General Public License as published by
6 ## the Free Software Foundation; either version 3 of the License, or
7 ## (at your option) any later version.
9 ## This program is distributed in the hope that it will be useful,
10 ## but WITHOUT ANY WARRANTY; without even the implied warranty of
11 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 ## GNU General Public License for more details.
14 ## You should have received a copy of the GNU General Public License
15 ## along with this program; If not, see <http://www.gnu.org/licenses/>.
18 ## @deftypefn {Function File} {@var{boundaries} = } bwboundaries(@var{BW})
19 ## @deftypefnx {Function File} {@var{boundaries} = } bwboundaries(@var{BW}, @var{conn})
20 ## @deftypefnx {Function File} {@var{boundaries} = } bwboundaries(@var{BW}, @var{conn}, @var{holes})
21 ## @deftypefnx {Function File} {[@var{boundaries}, @var{labels}] = } bwboundaries(@dots{})
22 ## @deftypefnx {Function File} {[@var{boundaries}, @var{labels}, @var{num_labels}] = } bwboundaries(@dots{})
23 ## Trace the boundaries of the objects in a binary image.
25 ## @var{boundaries} is a cell array in which each element is the boundary of an
26 ## object in the binary image @var{BW}. The clockwise boundary of each object is
27 ## computed by the @code{boundary} function.
29 ## By default the boundaries are computed using 8-connectivity. This can be
30 ## changed to 4-connectivity by setting @var{conn} to 4.
32 ## By default @code{bwboundaries} computes all boundaries in the image, i.e.
33 ## both interior and exterior object boundaries. This behaviour can be changed
34 ## through the @var{holes} input argument. If this is @t{'holes'},
35 ## both boundary types are considered. If it is instead @t{'noholes'}, only exterior
36 ## boundaries will be traced.
38 ## If two or more output arguments are requested, the algorithm also returns
39 ## the labelled image computed by @code{bwlabel} in @var{labels}. The number
40 ## of labels in this image is optionally returned in @var{num_labels}.
41 ## @seealso{boundary, bwlabel}
44 function [bound, labels, num_labels] = bwboundaries (bw, conn=8, holes="holes")
47 error ("bwboundaries: not enough input arguments");
49 if (!ismatrix (bw) || ndims (bw) != 2)
50 error ("bwboundaries: first input argument must be a NxM matrix");
52 if (!isscalar (conn) || (conn != 4 && conn != 8))
53 error ("bwboundaries: second input argument must be 4 or 8");
55 if (!ischar (holes) || !any (strcmpi (holes, {'holes', 'noholes'})))
56 error ("bwboundaries: third input must be either \'holes\' or \'noholes\'");
61 # process each connected region separately
62 [labels, num_labels] = bwlabel (bw, conn);
63 bound = cell (num_labels, 1);
65 bound {k} = __boundary__ ((labels == k), conn);
68 # compute internal boundaries as well?
69 if (strcmpi (holes, "holes"))
70 filled = bwfill (bw, "holes", conn);
71 holes = (filled & !bw);
72 [intBounds, intLabels, numIntLabels] = bwboundaries (holes, conn, "noholes");
74 bound (end+1 : end+numIntLabels, 1) = intBounds;
75 intLabels (intLabels != 0) += num_labels;
81 %! ## Generate a simple image
83 %! bw (10:30, 40:80) = true;
84 %! bw (40:45, 40:80) = true;
87 %! bounds = bwboundaries (bw);
92 %! for k = 1:numel (bounds)
93 %! plot (bounds {k} (:, 2), bounds {k} (:, 1), 'r', 'linewidth', 2);