Zhang-Suen thinning algorithm in Matlab?????
14 次查看(过去 30 天)
显示 更早的评论
I want to write a code Zhang-Suen thinning algorithm in matlab. Actally I had written the code but i dont know why it is entering in some sort of never ending loop.
My image is like a black part on the white background (White==1 and Black pixel ==0)
I am foolowing the steps given inthi link for the algorithm.
Can some one help me to implement it or any code for tis algo. in matlab.
My code is:--
Q is my binary image matrix;
N=1;
M=1;
while (M+N)~=0
M=0;
N=0;
for i=2:size(Q,1)-1
for j=2:size(Q,2)-1
Ap=0;
Bp=0;
if Q(i-1,j)==1 && Q(i-1,j+1)==0 %%p2 to p3
Ap=Ap+1;
end
if Q(i-1,j+1)==1 && Q(i,j+1)==0 %%p3 to p4
Ap=Ap+1;
end
if Q(i,j+1)==1 && Q(i+1,j+1)==0 %%p4 to p5
Ap=Ap+1;
end
if Q(i+1,j+1)==1 && Q(i+1,j)==0 %%p5 to p6
Ap=Ap+1;
end
if Q(i+1,j)==1 && Q(i+1,j-1)==0 %%p6 to p7
Ap=Ap+1;
end
if Q(i+1,j-1)==1 && Q(i,j-1)==0 %%p7 to p8
Ap=Ap+1;
end
if Q(i,j-1)==1 && Q(i-1,j-1)==0 %%p8 to p9
Ap=Ap+1;
end
if Q(i-1,j-1)==1 && Q(i,j)==0 %%p9 to p2
Ap=Ap+1;
end
if Q(i-1,j-1)==0 %%p9
Bp=Bp+1;
end
if Q(i-1,j)==0 %%p2
Bp=Bp+1;
end
if Q(i-1,j+1)==0 %%p3
Bp=Bp+1;
end
if Q(i,j-1)==0 %%p8
Bp=Bp+1;
end
if Q(i,j+1)==0 %%p4
Bp=Bp+1;
end
if Q(i+1,j-1)==0 %%p7
Bp=Bp+1;
end
if Q(i+1,j)==0 %%p6
Bp=Bp+1;
end
if Q(i+1,j+1)==0 %%p5
Bp=Bp+1;
end
if Q(i,j)==0
if 2<=Bp<=6
if Ap==1
if Q(i-1,j)==1 ||Q(i+1,j)==1 ||Q(i,j+1)==1
if Q(i,j-1)==1 ||Q(i+1,j)==1 ||Q(i,j+1)==1
if Q(i+1,j-1)~=1
Q(i,j)==1;
N=N+1;
end
end
end
end
end
end
end
end
for i=2:size(Q,1)-1
for j=2:size(Q,2)-1
Ap=0;
Bp=0;
if Q(i-1,j)==1 && Q(i-1,j+1)==0 %%p2 to p3
Ap=Ap+1;
end
if Q(i-1,j+1)==1 && Q(i,j+1)==0 %%p3 to p4
Ap=Ap+1;
end
if Q(i,j+1)==1 && Q(i+1,j+1)==0 %%p4 to p5
Ap=Ap+1;
end
if Q(i+1,j+1)==1 && Q(i+1,j)==0 %%p5 to p6
Ap=Ap+1;
end
if Q(i+1,j)==1 && Q(i+1,j-1)==0 %%p6 to p7
Ap=Ap+1;
end
if Q(i+1,j-1)==1 && Q(i,j-1)==0 %%p7 to p8
Ap=Ap+1;
end
if Q(i,j-1)==1 && Q(i-1,j-1)==0 %%p8 to p9
Ap=Ap+1;
end
if Q(i-1,j-1)==1 && Q(i,j)==0 %%p9 to p2
Ap=Ap+1;
end
if Q(i-1,j-1)==0 %%p9
Bp=Bp+1;
end
if Q(i-1,j)==0 %%p2
Bp=Bp+1;
end
if Q(i-1,j+1)==0 %%p3
Bp=Bp+1;
end
if Q(i,j-1)==0 %%p8
Bp=Bp+1;
end
if Q(i,j+1)==0 %%p4
Bp=Bp+1;
end
if Q(i+1,j-1)==0 %%p7
Bp=Bp+1;
end
if Q(i+1,j)==0 %%p6
Bp=Bp+1;
end
if Q(i+1,j+1)==0 %%p5
Bp=Bp+1;
end
if Q(i,j)==0
if 2<=Bp<=6
if Ap==1
if Q(i-1,j)==1 ||Q(i,j-1)==1 ||Q(i,j+1)==1
if Q(i-1,j)==1 ||Q(i+1,j)==1 ||Q(i,j-1)==1
if Q(i+1,j-1)~=1
Q(i,j)==1;
N=N+1;
end
end
end
end
end
end
end
end
end
figure,imshow(Q),title('Thinning by Zang Suen Algo');
0 个评论
回答(2 个)
Alexander Abramenko
2014-8-8
编辑:Alexander Abramenko
2014-8-8
Assume black pixels are one and white pixels zero, and that the input image is a rectangular N by M array of ones and zeroes.
I=rgb2gray(imread('new.bmp'));
BW = not(im2bw(I));
imshow(not(BW),[]);
figure;
continue_it = 1;
while continue_it
BW_old=BW;
BW_del=zeros(size(BW));
for i=2:size(BW,1)-1
for j = 2:size(BW,2)-1
P = [BW(i,j) BW(i-1,j) BW(i-1,j+1) BW(i,j+1) BW(i+1,j+1) BW(i+1,j) BW(i+1,j-1) BW(i,j-1) BW(i-1,j-1) BW(i-1,j)];
if P(2)*P(4)*P(6)==0 && P(4)*P(6)*P(8)==0 && sum(P(2:end-1))<=6 && sum(P(2:end-1)) >=2
A = 0;
for k = 2:size(P(:),1)-1
if P(k) == 0 && P(k+1)==1
A = A+1;
end%if
end%for
if (A==1)
BW_del(i,j)=1;
end%if
end%if
end%for
end%for
BW(find(BW_del==1))=0;
for i=2:size(BW,1)-1
for j = 2:size(BW,2)-1
P = [BW(i,j) BW(i-1,j) BW(i-1,j+1) BW(i,j+1) BW(i+1,j+1) BW(i+1,j) BW(i+1,j-1) BW(i,j-1) BW(i-1,j-1) BW(i-1,j)];
if P(2)*P(4)*P(8)==0 && P(2)*P(6)*P(8)==0 && sum(P(2:end-1))<=6 && sum(P(2:end-1)) >=2
A = 0;
for k = 2:size(P(:),1)-1
if P(k) == 0 && P(k+1)==1
A = A+1;
end%if
end%for
if (A==1)
BW_del(i,j)=1;
end%if
end%if
end%for
end%for
BW(find(BW_del==1))=0;
if prod(BW_old(:)==BW(:))
continue_it=0;
end%if
end%while
imshow(not(BW),[])
1 个评论
UNNI KRISHNAN M S
2019-12-11
Dear Alexander,
Cud u kindly explain the working of this code line by line....???...pls help
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!