Mercurial > hg > octave-image
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 |
rev | line source |
---|---|
709 | 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 | 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 | 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 | 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 | 16 |
391 | 17 ## -*- texinfo -*- |
439 | 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{}) | |
391 | 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 | |
439 | 26 ## object in the binary image @var{BW}. The clockwise boundary of each object is |
27 ## computed by the @code{boundary} function. | |
391 | 28 ## |
439 | 29 ## By default the boundaries are computed using 8-connectivity. This can be |
30 ## changed to 4-connectivity by setting @var{conn} to 4. | |
391 | 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 | |
439 | 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 | |
391 | 36 ## boundaries will be traced. |
37 ## | |
439 | 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 | |
391 | 40 ## of labels in this image is optionally returned in @var{num_labels}. |
439 | 41 ## @seealso{boundary, bwlabel} |
391 | 42 ## @end deftypefn |
43 | |
439 | 44 function [bound, labels, num_labels] = bwboundaries (bw, conn=8, holes="holes") |
45 # check arguments | |
390 | 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 | |
439 | 52 if (!isscalar (conn) || (conn != 4 && conn != 8)) |
53 error ("bwboundaries: second input argument must be 4 or 8"); | |
390 | 54 endif |
439 | 55 if (!ischar (holes) || !any (strcmpi (holes, {'holes', 'noholes'}))) |
56 error ("bwboundaries: third input must be either \'holes\' or \'noholes\'"); | |
390 | 57 endif |
58 | |
59 bw = logical (bw); | |
60 | |
439 | 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); | |
390 | 66 endfor |
67 | |
439 | 68 # compute internal boundaries as well? |
69 if (strcmpi (holes, "holes")) | |
70 filled = bwfill (bw, "holes", conn); | |
390 | 71 holes = (filled & !bw); |
439 | 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; | |
390 | 77 endif |
78 endfunction | |
439 | 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 |