]> Creatis software - CreaPhase.git/blob - octave_packages/image-1.0.15/fchcode.m
Add a useful package (from Source forge) for octave
[CreaPhase.git] / octave_packages / image-1.0.15 / fchcode.m
1 ## Copyright (C) 2010 Andrew Kelly, IPS Radio & Space Services
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{fcc} = } fchcode (@var{bound})
18 ## Determine the Freeman chain code for a boundary.
19 ##
20 ## @code{fchcode} computes the Freeman chain code for the @var{n}-connected
21 ## boundary @var{bound}. @var{n} must be either 8 or 4.
22 ##
23 ## @var{bound} is a K-by-2 matrix containing the row/column coordinates of points
24 ## on the boundary. Optionally, the first point can be repeated as the last point,
25 ## resulting in a (K+1)-by-2 matrix.
26 ##
27 ## @var{fcc} is a structure containing the following elements.
28 ##
29 ## @example
30 ##  x0y0   = Row/column coordinates where the code starts (1-by-2) 
31 ##  fcc    = Freeman chain code (1-by-K)
32 ##  diff   = First difference of fcc (1-by-K)
33 ## @end example
34 ##
35 ## The code uses the following directions.
36 ##
37 ## @example
38 ##  3 2 1
39 ##  4 . 0
40 ##  5 6 7
41 ## @end example
42 ##
43 ## @seealso{bwboundaries}
44 ## @end deftypefn
45
46 function fcc = fchcode (bound)
47
48   # ensure the boundary start and end points are the same
49   if (!isempty (bound) && !isequal (bound (1, :), bound (end, :)))
50     bound = [bound; bound(1, :)];
51   endif
52
53   # number of boundary points
54   n = max (0, rows (bound)-1);
55
56   # structure in which to return results
57   fcc = struct (\
58     'x0y0', zeros (1, n), \
59     'fcc',  zeros (1, n), \
60     'diff', zeros (1, n) \
61   );
62
63   # an empty boundary?
64   if (isempty (bound))
65     return;
66   endif
67
68   # direction map
69   dir = [3,  2,  1; \
70          4, NaN, 0; \
71          5,  6,  7];
72   
73   # coordinates
74   ROW = 1;
75   COL = 2;
76
77   # direction changes as row/column indexes into DIR
78   ch = 2 + diff (bound, 1, ROW);
79   
80   # starting point
81   fcc.x0y0 = bound (1, :);
82
83   # chain code
84   fcc.fcc = dir (sub2ind (size (dir), ch (:, ROW), ch (:, COL)))';
85
86   # chain code difference
87   fcc.diff = mod (diff ([fcc.fcc, fcc.fcc(1)]), 8);
88 endfunction