]> Creatis software - CreaPhase.git/blob - octave_packages/image-1.0.15/bwboundaries.m
Add a useful package (from Source forge) for octave
[CreaPhase.git] / octave_packages / image-1.0.15 / bwboundaries.m
1 ## Copyright (C) 2010 Soren Hauberg
2 ## Modified December 2010 by Andrew Kelly, IPS Radio & Space Services
3 ##
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.
8 ##
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.
13 ##
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/>.
16
17 ## -*- texinfo -*-
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.
24 ##
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.
28 ##
29 ## By default the boundaries are computed using 8-connectivity. This can be 
30 ## changed to 4-connectivity by setting @var{conn} to 4.
31 ##
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.
37 ##
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}
42 ## @end deftypefn
43
44 function [bound, labels, num_labels] = bwboundaries (bw, conn=8, holes="holes")
45   # check arguments
46   if (nargin < 1)
47     error ("bwboundaries: not enough input arguments");
48   endif
49   if (!ismatrix (bw) || ndims (bw) != 2)
50     error ("bwboundaries: first input argument must be a NxM matrix");
51   endif
52   if (!isscalar (conn) || (conn != 4 && conn != 8))
53     error ("bwboundaries: second input argument must be 4 or 8");
54   endif
55   if (!ischar (holes) || !any (strcmpi (holes, {'holes', 'noholes'})))
56     error ("bwboundaries: third input must be either \'holes\' or \'noholes\'");
57   endif
58   
59   bw = logical (bw);
60   
61   # process each connected region separately
62   [labels, num_labels] = bwlabel (bw, conn);
63   bound = cell (num_labels, 1);
64   for k = 1:num_labels
65     bound {k} = __boundary__ ((labels == k), conn);
66   endfor
67   
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");
73
74     bound (end+1 : end+numIntLabels, 1) = intBounds;
75     intLabels (intLabels != 0) += num_labels;
76     labels += intLabels;
77   endif
78 endfunction
79
80 %!demo
81 %! ## Generate a simple image
82 %! bw = false (100);
83 %! bw (10:30, 40:80) = true;
84 %! bw (40:45, 40:80) = true;
85 %!
86 %! ## Find boundaries
87 %! bounds = bwboundaries (bw);
88 %!
89 %! ## Plot result
90 %! imshow (bw);
91 %! hold on
92 %! for k = 1:numel (bounds)
93 %!   plot (bounds {k} (:, 2), bounds {k} (:, 1), 'r', 'linewidth', 2);
94 %! endfor
95 %! hold off