-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtestChainCode.m
More file actions
63 lines (49 loc) · 1.43 KB
/
testChainCode.m
File metadata and controls
63 lines (49 loc) · 1.43 KB
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
% @fileName testChainCode.m
% @author Melih Altun @2023
clear; clc; close all;
img = [0 0 0 0 0 0 0 0 0 0 0
0 0 1 1 1 1 1 1 1 0 0
0 1 1 1 1 1 1 1 1 1 0
0 1 1 1 1 1 1 1 1 0 0
0 1 1 1 0 0 0 0 0 0 0
0 1 1 1 1 1 1 1 0 0 0
0 1 1 1 1 1 1 1 1 0 0
0 0 1 1 1 1 1 1 1 1 0
0 0 0 0 0 0 0 1 1 1 0
0 0 0 0 0 0 0 1 1 1 0
0 0 1 1 1 1 1 1 1 1 0
0 1 1 1 1 1 1 1 1 0 0
0 0 1 1 1 1 1 1 0 0 0];
boundaryImg = getImgBoundary(img); % alternatively you can use bwboundaries() if you have image processing library
figure(1);
subplot(121);
imagesc(img);
title('Original B&W image');
subplot(122);
imagesc(boundaryImg);
title('Image boundaries');
[chCode, chMap] = findChainCode (boundaryImg);
chMap = chMap-1;
chCode = chCode-1; % 0 to 7 instead of 1 to 8
figure(2);
subplot(121);
histogram(chCode);
title('Distribution of directions');
subplot(122);
imagesc(chMap);
title('Location of directed boundaries');
function boundaryImg = getImgBoundary(bw_img)
sz = size(bw_img);
boundaryImg = zeros(sz);
imgPadded = zeros(sz(1)+2, sz(2)+2);
imgPadded(2:end-1, 2:end-1) = bw_img;
for i = 2:sz(1)+1
for j = 2:sz(2)+1
if imgPadded(i,j)
if ~imgPadded(i-1,j) || ~imgPadded(i+1,j) || ~imgPadded(i,j-1) || ~imgPadded(i,j+1)
boundaryImg(i-1, j-1) = 1;
end
end
end
end
end