-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathshaping.m
More file actions
254 lines (208 loc) · 4.41 KB
/
Copy pathshaping.m
File metadata and controls
254 lines (208 loc) · 4.41 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
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
clear
%% Load an image showing a few easily distinguishable objects
A = imread('abc.jpg');
figure(1), imshow(A),title('Original Image');
%convert RGB to Lum. Y(greyscale)
Agray = rgb2gray(A);
figure(2), subplot(1,2,1),imshow(Agray),title('Luminance Y');
%Lum. to thresholded image
Abw = im2bw(Agray);
subplot(1,2,2),imshow(Abw),title('Black and White');
% median filter clean up the image
%Abw = medfilt2(Abw);
%subplot(1,3,3),imshow(Abw),title('Cleaned up');
%% Morphology
[x,y] = size(Abw);
s = ones(5,5);
S = 25;
s = s/25;
Abw = double(Abw);
Temp = ones((x+4),(y+4));
Temp(5:(x+4),5:(y+4)) = Abw;
Ac = conv2(Temp,s);
Ac = Ac(5:(x+4),5:(y+4));
figure(3), subplot(3,2,1),imshow(Ac),title('Convolution');
%Dilation
ADil = Ac;
for i = 1:x
for j = 1:y
if Ac(i,j) < 1
ADil(i,j) = 0;
else
ADil(i,j) = 1;
end
end
end
subplot(3,2,2), imshow(ADil),title('Dilation');
%Erosion
AEro = Ac;
for i = 1:x
for j = 1:y
if Ac(i,j) < 1/S
AEro(i,j) = 0;
else
AEro(i,j) = 1;
end
end
end
subplot(3,2,3), imshow(AEro),title('Erosion');
%Majority
AMaj = Ac;
for i = 1:x
for j = 1:y
if Ac(i,j) < 2/S
AMaj(i,j) = 0;
else
AMaj(i,j) = 1;
end
end
end
subplot(3,2,4), imshow(AMaj),title('Majority');
%Opening
Temp1 = ones((x+4),(y+4));
Temp1(5:(x+4),5:(y+4)) = AEro;
AEroconv = conv2(Temp1,s);
AEroconv = AEroconv(5:(x+4),5:(y+4));
AOpen = Ac;
for i = 1:x
for j = 1:y
if AEroconv(i,j) < 1
AOpen(i,j) = 0;
else
AOpen(i,j) = 1;
end
end
end
subplot(3,2,5), imshow(AOpen),title('Opening');
%Closing
Temp2 = ones((x+4),(y+4));
Temp2(5:(x+4),5:(y+4)) = ADil;
ADilconv = conv2(Temp2,s);
ADilconv = ADilconv(5:(x+4),5:(y+4));
AClo = Ac;
for i = 1:x
for j = 1:y
if ADilconv(i,j) < 1/S
AClo(i,j) = 0;
else
AClo(i,j) = 1;
end
end
end
subplot(3,2,6), imshow(AClo),title('Closing');
%% Forward/
%Forward
[x,y] = size(Abw);
Abw = double(Abw);
Temp3 = Abw;
A4wr = Temp3;
for i = 1:x
for j = 1:y
if Temp3(i,j) == 0
A4wr(i,j) = 1;
elseif Temp3(i,j) == 1
A4wr(i,j) =0;
end
end
end
%%
for i = 2:x
for j = 2:y
if A4wr(i,j) ~= 0
A4wr(i,j) = min(A4wr(i-1,j),A4wr(i,j-1))+1;
end
end
end
A4wrmax = max(max(A4wr));
A4wr = A4wr/A4wrmax;
%%
for i = 1:x
for j = 1:y
if A4wr(i,j) == 0
A4wr(i,j) = 1;
elseif A4wr(i,j) == 1
A4wr(i,j) =0;
end
end
end
figure(4), subplot(1,3,1),imshow(A4wr),title('Forward');
%% ASE
%South east
[x,y] = size(Abw);
Abw = double(Abw);
Temp4 = Abw;
Ase = Temp4;
for i = 1:x
for j = 1:y
if Temp4(i,j) == 0
Ase(i,j) = 1;
elseif Temp4(i,j) == 1
Ase(i,j) =0;
end
end
end
%% A in South and East
for i = (x-1):-1:1
for j = (y-1):-1:1
if Ase(i,j) ~= 0
Ase(i,j) = min(Ase(i+1,j),A4wr(i,j+1))+1;
end
end
end
Asemax = max(max(Ase));
Ase = Ase/Asemax;
%%
for i = 1:x
for j = 1:y
if Ase(i,j) == 0
Ase(i,j) = 1;
elseif Ase(i,j) == 1
Ase(i,j) =0;
end
end
end
subplot(1,3,2),imshow(Ase),title('Ase');
%% Backward
Abacw = min(Ase,A4wr);
subplot(1,3,3),imshow(Abacw),title('A backward');
%% Connected components
Temp5 = ones(x+2,y+2);
Temp5(2:x+1,2:y+1) = Abw;
Aconn1 = Temp5;
k = 1;
for i = 2:x+1
for j = 2:y+1
if Temp5(i,j) == Temp5(i,j-1)
Aconn1(i,j) = Aconn1(i,j-1);
elseif Temp5(i,j) == Temp5(i-1,j)
Aconn1(i,j) = Aconn1(i-1,j);
else
k = k+1;
Aconn1(i,j) = k;
end
end
end
Aconn = Aconn1;
Aconn1 = Aconn1(2:x+1,2:y+1);
Aconn1max = max(max(Aconn1));
for i = 1:x
for j = 1:y
if Aconn1(i,j) == 1
Aconn1(i,j) = Aconn1max+1;
end
end
end
Aconn1 = Aconn1/(Aconn1max+1);
figure(5), subplot(1,2,1),imshow(Aconn1),title('Connected components 1st pass');
% Merge adjacent runs
for i = x+1:-1:2
for j = y+1:-1:2
if Aconn(i+1,j) ~= Aconn(i,j) && Temp5(i+1,j) == Temp5(i,j)
Aconn(find(Aconn == Aconn(i+1,j))) = Aconn(i,j);
end
end
end
Aconn = Aconn(2:x+1,2:y+1);
Aconnmax = max(max(Aconn));
Aconn = Aconn/10;
subplot(1,2,2), imshow(Aconn),title('Connected components Merged');