]> Creatis software - CreaPhase.git/blob - octave_packages/image-1.0.15/bwconncomp.m
Add a useful package (from Source forge) for octave
[CreaPhase.git] / octave_packages / image-1.0.15 / bwconncomp.m
1 ## Copyright (C) 2010 Soren Hauberg
2 ##
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.
7 ##
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.
12 ##
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/>.
15
16 ## -*- texinfo -*-
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.
20 ##
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.
23 ##
24 ## @table @t
25 ## @item Connectivity
26 ## The connectivity used in the boundary tracing.
27 ## @item ImageSize
28 ## The size of the image @var{BW}.
29 ## @item NumObjects
30 ## The number of objects in the image @var{BW}.
31 ## @item PixelIdxList
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
34 ## the given object.
35 ## @end table
36 ##
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
39 ## yet implemented.
40 ## @seealso{bwlabel, bwboundaries, ind2sub}
41 ## @end deftypefn
42
43 function CC = bwconncomp (bw, N = 4)
44   ## Check input
45   if (nargin < 1)
46     error ("bwconncomp: not enough input arguments");
47   endif
48   if (!ismatrix (bw) || ndims (bw) != 2)
49     error ("bwconncomp: first input argument must be a NxM matrix");
50   endif
51   if (!isscalar (N) || !any (N == [4])) #, 8]))
52     error ("bwconncomp: second input argument must be 4");
53   endif
54   
55   ## Trace boundaries
56   B = bwboundaries (bw, N);
57
58   ## Convert from (x, y) index to linear indexing
59   P = cell (numel (B), 1);
60   for k = 1:numel (B)
61     P {k} = sub2ind (size (bw), B {k} (:, 2), B {k} (:, 1));
62   endfor
63   
64   ## Return result
65   CC = struct ("Connectivity", N, "ImageSize", size (bw), "NumObjects", numel (B));
66   CC.PixelIdxList = P;
67 endfunction