필터 지우기
필터 지우기

Zhang-Suen thinning algorithm in Matlab?????

조회 수: 14 (최근 30일)
ANCHIT
ANCHIT 2014년 6월 5일
댓글: UNNI KRISHNAN M S 2019년 12월 11일
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');

답변 (2개)

Alexander Abramenko
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
UNNI KRISHNAN M S 2019년 12월 11일
Dear Alexander,
Cud u kindly explain the working of this code line by line....???...pls help

댓글을 달려면 로그인하십시오.


linbo jin
linbo jin 2014년 10월 25일
Skeletonization by Zhang-Suen Thinning Algorithm, Python and Matlab Implementation: github code
  댓글 수: 1
Nasreen Ehsan
Nasreen Ehsan 2016년 4월 6일
How we implement Huang Wan Liu thinning algorithm in Matlab?

댓글을 달려면 로그인하십시오.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by