annotate inst/bwboundaries.m @ 915:8c8ed7c4ab83 default tip

* bwlabeln.cc (bwlabel_nd): Fix small bug in comment
author Jordi Gutiérrez Hermoso <jordigh@octave.org>
date Tue, 18 Nov 2014 11:30:57 -0500
parents c6be7812523a
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
709
c6be7812523a maint: fix Søren's name
carandraug
parents: 561
diff changeset
1 ## Copyright (C) 2010 Søren Hauberg <soren@hauberg.org>
561
c45838839d86 maint: update license to GPLv3 and mention non GPL files
carandraug
parents: 439
diff changeset
2 ## Copyright (C) Andrew Kelly, IPS Radio & Space Services
390
870ecf0abb54 New function: bwboundaries
hauberg
parents:
diff changeset
3 ##
561
c45838839d86 maint: update license to GPLv3 and mention non GPL files
carandraug
parents: 439
diff changeset
4 ## This program is free software; you can redistribute it and/or modify it under
c45838839d86 maint: update license to GPLv3 and mention non GPL files
carandraug
parents: 439
diff changeset
5 ## the terms of the GNU General Public License as published by the Free Software
c45838839d86 maint: update license to GPLv3 and mention non GPL files
carandraug
parents: 439
diff changeset
6 ## Foundation; either version 3 of the License, or (at your option) any later
c45838839d86 maint: update license to GPLv3 and mention non GPL files
carandraug
parents: 439
diff changeset
7 ## version.
390
870ecf0abb54 New function: bwboundaries
hauberg
parents:
diff changeset
8 ##
561
c45838839d86 maint: update license to GPLv3 and mention non GPL files
carandraug
parents: 439
diff changeset
9 ## This program is distributed in the hope that it will be useful, but WITHOUT
c45838839d86 maint: update license to GPLv3 and mention non GPL files
carandraug
parents: 439
diff changeset
10 ## ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
c45838839d86 maint: update license to GPLv3 and mention non GPL files
carandraug
parents: 439
diff changeset
11 ## FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
c45838839d86 maint: update license to GPLv3 and mention non GPL files
carandraug
parents: 439
diff changeset
12 ## details.
390
870ecf0abb54 New function: bwboundaries
hauberg
parents:
diff changeset
13 ##
561
c45838839d86 maint: update license to GPLv3 and mention non GPL files
carandraug
parents: 439
diff changeset
14 ## You should have received a copy of the GNU General Public License along with
c45838839d86 maint: update license to GPLv3 and mention non GPL files
carandraug
parents: 439
diff changeset
15 ## this program; if not, see <http://www.gnu.org/licenses/>.
390
870ecf0abb54 New function: bwboundaries
hauberg
parents:
diff changeset
16
391
892dbc2ce951 Document bwboundaries
hauberg
parents: 390
diff changeset
17 ## -*- texinfo -*-
439
d13dba2ec9c2 New version of bwboundaries (from A Kelly)
hauberg
parents: 437
diff changeset
18 ## @deftypefn {Function File} {@var{boundaries} = } bwboundaries(@var{BW})
d13dba2ec9c2 New version of bwboundaries (from A Kelly)
hauberg
parents: 437
diff changeset
19 ## @deftypefnx {Function File} {@var{boundaries} = } bwboundaries(@var{BW}, @var{conn})
d13dba2ec9c2 New version of bwboundaries (from A Kelly)
hauberg
parents: 437
diff changeset
20 ## @deftypefnx {Function File} {@var{boundaries} = } bwboundaries(@var{BW}, @var{conn}, @var{holes})
d13dba2ec9c2 New version of bwboundaries (from A Kelly)
hauberg
parents: 437
diff changeset
21 ## @deftypefnx {Function File} {[@var{boundaries}, @var{labels}] = } bwboundaries(@dots{})
d13dba2ec9c2 New version of bwboundaries (from A Kelly)
hauberg
parents: 437
diff changeset
22 ## @deftypefnx {Function File} {[@var{boundaries}, @var{labels}, @var{num_labels}] = } bwboundaries(@dots{})
391
892dbc2ce951 Document bwboundaries
hauberg
parents: 390
diff changeset
23 ## Trace the boundaries of the objects in a binary image.
892dbc2ce951 Document bwboundaries
hauberg
parents: 390
diff changeset
24 ##
892dbc2ce951 Document bwboundaries
hauberg
parents: 390
diff changeset
25 ## @var{boundaries} is a cell array in which each element is the boundary of an
439
d13dba2ec9c2 New version of bwboundaries (from A Kelly)
hauberg
parents: 437
diff changeset
26 ## object in the binary image @var{BW}. The clockwise boundary of each object is
d13dba2ec9c2 New version of bwboundaries (from A Kelly)
hauberg
parents: 437
diff changeset
27 ## computed by the @code{boundary} function.
391
892dbc2ce951 Document bwboundaries
hauberg
parents: 390
diff changeset
28 ##
439
d13dba2ec9c2 New version of bwboundaries (from A Kelly)
hauberg
parents: 437
diff changeset
29 ## By default the boundaries are computed using 8-connectivity. This can be
d13dba2ec9c2 New version of bwboundaries (from A Kelly)
hauberg
parents: 437
diff changeset
30 ## changed to 4-connectivity by setting @var{conn} to 4.
391
892dbc2ce951 Document bwboundaries
hauberg
parents: 390
diff changeset
31 ##
892dbc2ce951 Document bwboundaries
hauberg
parents: 390
diff changeset
32 ## By default @code{bwboundaries} computes all boundaries in the image, i.e.
892dbc2ce951 Document bwboundaries
hauberg
parents: 390
diff changeset
33 ## both interior and exterior object boundaries. This behaviour can be changed
439
d13dba2ec9c2 New version of bwboundaries (from A Kelly)
hauberg
parents: 437
diff changeset
34 ## through the @var{holes} input argument. If this is @t{'holes'},
d13dba2ec9c2 New version of bwboundaries (from A Kelly)
hauberg
parents: 437
diff changeset
35 ## both boundary types are considered. If it is instead @t{'noholes'}, only exterior
391
892dbc2ce951 Document bwboundaries
hauberg
parents: 390
diff changeset
36 ## boundaries will be traced.
892dbc2ce951 Document bwboundaries
hauberg
parents: 390
diff changeset
37 ##
439
d13dba2ec9c2 New version of bwboundaries (from A Kelly)
hauberg
parents: 437
diff changeset
38 ## If two or more output arguments are requested, the algorithm also returns
d13dba2ec9c2 New version of bwboundaries (from A Kelly)
hauberg
parents: 437
diff changeset
39 ## the labelled image computed by @code{bwlabel} in @var{labels}. The number
391
892dbc2ce951 Document bwboundaries
hauberg
parents: 390
diff changeset
40 ## of labels in this image is optionally returned in @var{num_labels}.
439
d13dba2ec9c2 New version of bwboundaries (from A Kelly)
hauberg
parents: 437
diff changeset
41 ## @seealso{boundary, bwlabel}
391
892dbc2ce951 Document bwboundaries
hauberg
parents: 390
diff changeset
42 ## @end deftypefn
892dbc2ce951 Document bwboundaries
hauberg
parents: 390
diff changeset
43
439
d13dba2ec9c2 New version of bwboundaries (from A Kelly)
hauberg
parents: 437
diff changeset
44 function [bound, labels, num_labels] = bwboundaries (bw, conn=8, holes="holes")
d13dba2ec9c2 New version of bwboundaries (from A Kelly)
hauberg
parents: 437
diff changeset
45 # check arguments
390
870ecf0abb54 New function: bwboundaries
hauberg
parents:
diff changeset
46 if (nargin < 1)
870ecf0abb54 New function: bwboundaries
hauberg
parents:
diff changeset
47 error ("bwboundaries: not enough input arguments");
870ecf0abb54 New function: bwboundaries
hauberg
parents:
diff changeset
48 endif
870ecf0abb54 New function: bwboundaries
hauberg
parents:
diff changeset
49 if (!ismatrix (bw) || ndims (bw) != 2)
870ecf0abb54 New function: bwboundaries
hauberg
parents:
diff changeset
50 error ("bwboundaries: first input argument must be a NxM matrix");
870ecf0abb54 New function: bwboundaries
hauberg
parents:
diff changeset
51 endif
439
d13dba2ec9c2 New version of bwboundaries (from A Kelly)
hauberg
parents: 437
diff changeset
52 if (!isscalar (conn) || (conn != 4 && conn != 8))
d13dba2ec9c2 New version of bwboundaries (from A Kelly)
hauberg
parents: 437
diff changeset
53 error ("bwboundaries: second input argument must be 4 or 8");
390
870ecf0abb54 New function: bwboundaries
hauberg
parents:
diff changeset
54 endif
439
d13dba2ec9c2 New version of bwboundaries (from A Kelly)
hauberg
parents: 437
diff changeset
55 if (!ischar (holes) || !any (strcmpi (holes, {'holes', 'noholes'})))
d13dba2ec9c2 New version of bwboundaries (from A Kelly)
hauberg
parents: 437
diff changeset
56 error ("bwboundaries: third input must be either \'holes\' or \'noholes\'");
390
870ecf0abb54 New function: bwboundaries
hauberg
parents:
diff changeset
57 endif
870ecf0abb54 New function: bwboundaries
hauberg
parents:
diff changeset
58
870ecf0abb54 New function: bwboundaries
hauberg
parents:
diff changeset
59 bw = logical (bw);
870ecf0abb54 New function: bwboundaries
hauberg
parents:
diff changeset
60
439
d13dba2ec9c2 New version of bwboundaries (from A Kelly)
hauberg
parents: 437
diff changeset
61 # process each connected region separately
d13dba2ec9c2 New version of bwboundaries (from A Kelly)
hauberg
parents: 437
diff changeset
62 [labels, num_labels] = bwlabel (bw, conn);
d13dba2ec9c2 New version of bwboundaries (from A Kelly)
hauberg
parents: 437
diff changeset
63 bound = cell (num_labels, 1);
d13dba2ec9c2 New version of bwboundaries (from A Kelly)
hauberg
parents: 437
diff changeset
64 for k = 1:num_labels
d13dba2ec9c2 New version of bwboundaries (from A Kelly)
hauberg
parents: 437
diff changeset
65 bound {k} = __boundary__ ((labels == k), conn);
390
870ecf0abb54 New function: bwboundaries
hauberg
parents:
diff changeset
66 endfor
870ecf0abb54 New function: bwboundaries
hauberg
parents:
diff changeset
67
439
d13dba2ec9c2 New version of bwboundaries (from A Kelly)
hauberg
parents: 437
diff changeset
68 # compute internal boundaries as well?
d13dba2ec9c2 New version of bwboundaries (from A Kelly)
hauberg
parents: 437
diff changeset
69 if (strcmpi (holes, "holes"))
d13dba2ec9c2 New version of bwboundaries (from A Kelly)
hauberg
parents: 437
diff changeset
70 filled = bwfill (bw, "holes", conn);
390
870ecf0abb54 New function: bwboundaries
hauberg
parents:
diff changeset
71 holes = (filled & !bw);
439
d13dba2ec9c2 New version of bwboundaries (from A Kelly)
hauberg
parents: 437
diff changeset
72 [intBounds, intLabels, numIntLabels] = bwboundaries (holes, conn, "noholes");
d13dba2ec9c2 New version of bwboundaries (from A Kelly)
hauberg
parents: 437
diff changeset
73
d13dba2ec9c2 New version of bwboundaries (from A Kelly)
hauberg
parents: 437
diff changeset
74 bound (end+1 : end+numIntLabels, 1) = intBounds;
d13dba2ec9c2 New version of bwboundaries (from A Kelly)
hauberg
parents: 437
diff changeset
75 intLabels (intLabels != 0) += num_labels;
d13dba2ec9c2 New version of bwboundaries (from A Kelly)
hauberg
parents: 437
diff changeset
76 labels += intLabels;
390
870ecf0abb54 New function: bwboundaries
hauberg
parents:
diff changeset
77 endif
870ecf0abb54 New function: bwboundaries
hauberg
parents:
diff changeset
78 endfunction
439
d13dba2ec9c2 New version of bwboundaries (from A Kelly)
hauberg
parents: 437
diff changeset
79
d13dba2ec9c2 New version of bwboundaries (from A Kelly)
hauberg
parents: 437
diff changeset
80 %!demo
d13dba2ec9c2 New version of bwboundaries (from A Kelly)
hauberg
parents: 437
diff changeset
81 %! ## Generate a simple image
d13dba2ec9c2 New version of bwboundaries (from A Kelly)
hauberg
parents: 437
diff changeset
82 %! bw = false (100);
d13dba2ec9c2 New version of bwboundaries (from A Kelly)
hauberg
parents: 437
diff changeset
83 %! bw (10:30, 40:80) = true;
d13dba2ec9c2 New version of bwboundaries (from A Kelly)
hauberg
parents: 437
diff changeset
84 %! bw (40:45, 40:80) = true;
d13dba2ec9c2 New version of bwboundaries (from A Kelly)
hauberg
parents: 437
diff changeset
85 %!
d13dba2ec9c2 New version of bwboundaries (from A Kelly)
hauberg
parents: 437
diff changeset
86 %! ## Find boundaries
d13dba2ec9c2 New version of bwboundaries (from A Kelly)
hauberg
parents: 437
diff changeset
87 %! bounds = bwboundaries (bw);
d13dba2ec9c2 New version of bwboundaries (from A Kelly)
hauberg
parents: 437
diff changeset
88 %!
d13dba2ec9c2 New version of bwboundaries (from A Kelly)
hauberg
parents: 437
diff changeset
89 %! ## Plot result
d13dba2ec9c2 New version of bwboundaries (from A Kelly)
hauberg
parents: 437
diff changeset
90 %! imshow (bw);
d13dba2ec9c2 New version of bwboundaries (from A Kelly)
hauberg
parents: 437
diff changeset
91 %! hold on
d13dba2ec9c2 New version of bwboundaries (from A Kelly)
hauberg
parents: 437
diff changeset
92 %! for k = 1:numel (bounds)
d13dba2ec9c2 New version of bwboundaries (from A Kelly)
hauberg
parents: 437
diff changeset
93 %! plot (bounds {k} (:, 2), bounds {k} (:, 1), 'r', 'linewidth', 2);
d13dba2ec9c2 New version of bwboundaries (from A Kelly)
hauberg
parents: 437
diff changeset
94 %! endfor
d13dba2ec9c2 New version of bwboundaries (from A Kelly)
hauberg
parents: 437
diff changeset
95 %! hold off