-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmk_chain.m
37 lines (32 loc) · 953 Bytes
/
mk_chain.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
function [chain] = mk_chain(image_path)
% mk_chain Generate an 8 connected chain from an image
%
% input: an image path - should contain a single connected contour
% output: [Nx2] 8 connected chain of N coordinates
%
image = imread(image_path);
image = image(:,:,1);
%get first connected component
[start_r,start_c] = find(image,1,'first');
%as bwtraceboundary needs an intial direction, choose the
%first one that works
if image(start_r+1,start_c+1) == 255
dir = 'SE';
elseif image(start_r,start_c+1) == 255
dir = 'E';
elseif image(start_r-1,start_c+1) == 255
dir = 'NE';
elseif image(start_r-1,start_c) == 255
dir = 'N';
elseif image(start_r-1,start_c-1) == 255
dir = 'NW';
elseif image(start_r,start_c-1) == 255
dir = 'W';
elseif image(start_r+1,start_c-1) == 255
dir = 'SW';
elseif image(start_r+1,start_c) == 255
dir = 'S';
else
assert(0);
end
chain = bwtraceboundary(image,[start_r,start_c],dir);