-
Notifications
You must be signed in to change notification settings - Fork 0
/
thin_zhangSuen.m
182 lines (149 loc) · 5.21 KB
/
thin_zhangSuen.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
function [thin_image] = thin_zhangSuen(binary_image)
%THIN_ZHANGSUEN
% Step One:循环所有前景像素点,对符合如下条件的像素点标记为删除:
% 1. 2 <= N(p1) <=6
% 2. S(P1) = 1
% 3. P2 P4 P6 至少有1个为1
% 4. P4 P6 P8 至少有1个为1
% P9 P2 P3
% P8 P1 P4
% P7 P6 P5
% 其中,N(P1)表示8个邻居中为1的数目
% S(P1)表示从P2-P9-P2(顺时针)转一圈,从0到1变化的次数
% Step Two:循环所有前景像素点,对符合如下条件的像素点标记为删除:
% 1. 2 <= N(p1) <=6
% 2. S(P1) = 1
% 3. P2 P4 P8 至少有1个为1
% 4. P2 P6 P8 至少有1个为1
% https://blog.csdn.net/Ibelievesunshine/article/details/105069015
height = size(binary_image, 1);
width = size(binary_image, 2);
% inverse, 算法把0作为前景,1作为后景
thin_image = binary_image;
thin_image = 1 - thin_image;
while true
s1 = [];
s2 = [];
% Step 1
for i = 2 : height - 1
for j = 2 : width - 1
% 不是前景点不理会
if thin_image(i, j) > 0
continue;
end
% condition 1
flag1 = sum(thin_image(i - 1 : i + 1, j - 1 : j + 1), 'all');
if flag1 < 2 || flag1 > 6
continue;
end
% condition 2
flag2 = 0;
if(thin_image(i - 1, j + 1) - thin_image(i - 1, j) == 1)
flag2 = flag2 + 1;
end
if(thin_image(i, j + 1) - thin_image(i - 1, j + 1) == 1)
flag2 = flag2 + 1;
end
if(thin_image(i + 1, j + 1) - thin_image(i, j + 1) == 1)
flag2 = flag2 + 1;
end
if(thin_image(i + 1, j) - thin_image(i + 1, j + 1) == 1)
flag2 = flag2 + 1;
end
if(thin_image(i + 1, j - 1) - thin_image(i + 1, j) == 1)
flag2 = flag2 + 1;
end
if(thin_image(i, j - 1) - thin_image(i + 1, j - 1) == 1)
flag2 = flag2 + 1;
end
if(thin_image(i - 1, j - 1) - thin_image(i, j - 1) == 1)
flag2 = flag2 + 1;
end
if(thin_image(i - 1, j) - thin_image(i - 1, j - 1) == 1)
flag2 = flag2 + 1;
end
if(thin_image(i - 1, j) - thin_image(i - 1, j - 1) == 1)
flag2 = flag2 + 1;
end
if flag2 ~= 1
continue;
end
% condition 3
if(thin_image(i - 1, j) + thin_image(i, j + 1) + thin_image(i + 1, j) < 1)
continue;
end
% condition 4
if(thin_image(i, j + 1) + thin_image(i + 1, j) + thin_image(i, j - 1) < 1)
continue;
end
s1 = [s1; [i, j]];
end
end
for k = 1 : size(s1, 1)
thin_image(s1(k, 1), s1(k, 2)) = 1;
end
% Step 2
for i = 2 : height - 1
for j = 2 : width - 1
% 不是前景点不理会
if thin_image(i, j) > 0
continue;
end
% condition 1
flag1 = sum(thin_image(i - 1 : i + 1, j - 1 : j + 1), 'all');
if flag1 < 2 || flag1 > 6
continue;
end
% condition 2
flag2 = 0;
if(thin_image(i - 1, j + 1) - thin_image(i - 1, j) == 1)
flag2 = flag2 + 1;
end
if(thin_image(i, j + 1) - thin_image(i - 1, j + 1) == 1)
flag2 = flag2 + 1;
end
if(thin_image(i + 1, j + 1) - thin_image(i, j + 1) == 1)
flag2 = flag2 + 1;
end
if(thin_image(i + 1, j) - thin_image(i + 1, j + 1) == 1)
flag2 = flag2 + 1;
end
if(thin_image(i + 1, j - 1) - thin_image(i + 1, j) == 1)
flag2 = flag2 + 1;
end
if(thin_image(i, j - 1) - thin_image(i + 1, j - 1) == 1)
flag2 = flag2 + 1;
end
if(thin_image(i - 1, j - 1) - thin_image(i, j - 1) == 1)
flag2 = flag2 + 1;
end
if(thin_image(i - 1, j) - thin_image(i - 1, j - 1) == 1)
flag2 = flag2 + 1;
end
if(thin_image(i - 1, j) - thin_image(i - 1, j - 1) == 1)
flag2 = flag2 + 1;
end
if flag2 ~= 1
continue;
end
% condition 3
if(thin_image(i - 1, j) + thin_image(i, j + 1) + thin_image(i, j - 1) < 1)
continue;
end
% condition 4
if(thin_image(i - 1, j) + thin_image(i + 1, j) + thin_image(i, j - 1) < 1)
continue;
end
s2 = [s2; [i, j]];
end
end
for k = 1 : size(s2, 1)
thin_image(s2(k, 1), s2(k, 2)) = 1;
end
% 如果两步中都没有点要删除,结束循环
if size(s1, 1) == 0 && size(s2, 1) == 0
break;
end
end
thin_image = 1 - thin_image;
end