1 ## Copyright (C) 2010 Andrew Kelly, IPS Radio & Space Services
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.
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.
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/>.
17 ## @deftypefn {Function File} {@var{fcc} = } fchcode (@var{bound})
18 ## Determine the Freeman chain code for a boundary.
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.
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.
27 ## @var{fcc} is a structure containing the following elements.
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)
35 ## The code uses the following directions.
43 ## @seealso{bwboundaries}
46 function fcc = fchcode (bound)
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, :)];
53 # number of boundary points
54 n = max (0, rows (bound)-1);
56 # structure in which to return results
58 'x0y0', zeros (1, n), \
59 'fcc', zeros (1, n), \
60 'diff', zeros (1, n) \
77 # direction changes as row/column indexes into DIR
78 ch = 2 + diff (bound, 1, ROW);
81 fcc.x0y0 = bound (1, :);
84 fcc.fcc = dir (sub2ind (size (dir), ch (:, ROW), ch (:, COL)))';
86 # chain code difference
87 fcc.diff = mod (diff ([fcc.fcc, fcc.fcc(1)]), 8);