-
Notifications
You must be signed in to change notification settings - Fork 1
/
boundarytrack.m
79 lines (59 loc) · 1.54 KB
/
boundarytrack.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
% r,c = positions of boundary
% H,W = image height and width
% figN = figure number for result display (0 for no display)
% [tr,tc] = consecutive set of tracked boundary points
function [tr,tc] = boundarytrack(r,c,H,W,figN)
% next direction offsets
% 1 2 3
% 8 x 4
% 7 6 5
mr = [-1,-1,-1,0,1,1,1,0];
mc = [-1,0,1,1,1,0,-1,-1];
% set up visited array (keep track of where we have already tracked)
n = length(r);
visited=zeros(H,W);
for i = 1 : n
visited(r(i),c(i)) = 1;
end
% set up tracked output
tmpr = zeros(n,1);
tmpc = zeros(n,1);
count = 1;
tmpr(1) = r(1);
tmpc(1) = c(1);
visited(tmpr(count),tmpc(count)) = 0;
% find first connected point clockwise
lastdir = 1; % arbitrary start
notdone = 1;
while notdone
% find next untracked point
notdone = 0;
for i = 1 : 8 % try all 8 directions
nextdir = lastdir + 4 + i; % get next direction to try
while nextdir > 8
nextdir = nextdir - 8;
end
% see if nextdir is a boundary point
nr = tmpr(count)+mr(nextdir);
nc = tmpc(count)+mc(nextdir);
if visited(nr,nc) == 1
% it is a boundary point, so track to it
lastdir = nextdir;
notdone = 1;
count = count + 1;
tmpr(count) = nr;
tmpc(count) = nc;
visited(nr,nc) = 0;
break
end
end
end
tr = tmpr(1:count);
tc = tmpc(1:count);
% display result - ?
if figN > 0
figure(figN)
plot(tc,tr)
axis([0,W,0,H])
axis ij
end