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

22 vues (au cours des 30 derniers jours)
ANCHIT
ANCHIT le 5 Juin 2014
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');

Réponses (2)

Alexander Abramenko
Alexander Abramenko le 8 Août 2014
Modifié(e) : Alexander Abramenko le 8 Août 2014
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 commentaire
UNNI KRISHNAN M S
UNNI KRISHNAN M S le 11 Déc 2019
Dear Alexander,
Cud u kindly explain the working of this code line by line....???...pls help

Connectez-vous pour commenter.


linbo jin
linbo jin le 25 Oct 2014
Skeletonization by Zhang-Suen Thinning Algorithm, Python and Matlab Implementation: github code
  1 commentaire
Nasreen Ehsan
Nasreen Ehsan le 6 Avr 2016
How we implement Huang Wan Liu thinning algorithm in Matlab?

Connectez-vous pour commenter.

Catégories

En savoir plus sur Wireless Communications dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by