我一直在尝试在MATLAB上实现Zheng-Suen细化算法。我的代码正在运行,但结果不合适。该算法正在二进制字符上实现。图像如下。任何形式的帮助将不胜感激
`I=im2double(I);` %% I = original image
[H,W]=size(I); %% height, width of image
J=I; %% J = skeletonized image
K=I; %% K = skeletonized image
B=0; %% B = number of non-zero neighbors
A=0; %% A = 0-1 patterns
CHANGE=1000; %% number of pixels changed over iteration
P=zeros(8); %% array to hold the 8-neighborhood values
%% OUTER LOOP - LOOP UNTIL NO PIXELS CHANGE %%
while (CHANGE ~= 0)
CHANGE =0;
%% FIRST SUB-ITERATION %%
for r=2:H-1 %% row
for c=2:W-1 %% column
if(J(r,c)==1)
%% find 8 neighborhood of pixel %%
`P(9) = J(r-1,c-1);
P(2) = J(r-1,c);
P(3) = J(r-1,c+1);
P(8) = J(r,c-1);
P(4) = J(r,c+1);
P(7) = J(r+1,c-1);
P(6) = J(r+1,c);
P(5) = J(r+1,c+1);
%% calculate B %%
B = P(2)+P(3)+P(4)+P(5)+P(6)+P(7)+P(8)+P(9);
%% COMPUTE A %%
A=0;
if ( P(2)==0 & P(3)==1 )A=A+1; end
if ( P(3)==0 & P(4)==1 )A=A+1; end
if ( P(4)==0 & P(5)==1 )A=A+1; end
if ( P(5)==0 & P(6)==1 )A=A+1; end
if ( P(6)==0 & P(7)==1 )A=A+1; end
if ( P(7)==0 & P(8)==1 )A=A+1; end
if ( P(8)==0 & P(9)==1 )A=A+1; end
if ( P(9)==0 & P(2)==1 )A=A+1; end
%% DECIDE IF PIXEL SHOULD BE DELETED %%
if( (B>=2) & (B<=6) & (A==1) & (P(2)*P(4)*P(6)==0) & (P(4)*P(6)*P(8)==0) )
K(r,c)=-1;
CHANGE=CHANGE+1;
end
end
end
end
%% SECOND SUB-ITERATION %%
J=K;
for r=2:H-1 %% row
for c=2:W-1 %% column
if(J(r,c)==1)
`P(9) = J(r-1,c-1);
P(2) = J(r-1,c);
P(3) = J(r-1,c+1);
P(8) = J(r,c-1);
P(4) = J(r,c+1);
P(7) = J(r+1,c-1);
P(6) = J(r+1,c);
P(5) = J(r+1,c+1);
B = P(2)+P(3)+P(4)+P(5)+P(6)+P(7)+P(8)+P(9);
A=0;
if ( P(2)==0 & P(3)==1 )A=A+1; end
if ( P(3)==0 & P(4)==1 )A=A+1; end
if ( P(4)==0 & P(5)==1 )A=A+1; end
if ( P(5)==0 & P(6)==1 )A=A+1; end
if ( P(6)==0 & P(7)==1 )A=A+1; end
if ( P(7)==0 & P(8)==1 )A=A+1; end
if ( P(8)==0 & P(9)==1 )A=A+1; end
if ( P(9)==0 & P(2)==1 )A=A+1; end
%% DECIDE IF PIXEL SHOULD BE DELETED %%
if( (B>=2) & (B<=6) & (A==1) & (P(2)*P(4)*P(8)==0) & (P(2)*P(6)*P(8)==0) )
K(r,c)=-1;
CHANGE=CHANGE+1;
end
end
end
end
CHANGE output # of changes this iteration
for r=1:H
for c=1:W
if(K(r,c)==-1)
K(r,c)=0;
end
end
end
figure, imshow(I); figure, imshow(J);
`
答案 0 :(得分:-1)
这不是答案,但我无法在上面发表评论。请提供Zheng-Suen细化算法的链接。此外,您的代码是错误的。请重新发布它和任何输出图像。