[m,n]=size(P);
W=zeros(m,n);
for i=2:m-1
for j=2:n-1
count2=0;count3=0;count4=0;count5=0;count6=0;count7=0;count8=0;count9=0;
p1=P(i,j);
p2=P(i-1,j);
p3=P(i-1,j+1);
p4=P(i,j+1);
p5=P(i+1,j+1);
p6=P(i+1,j);
p7=P(i+1,j-1);
p8=P(i,j-1);
p9=P(i-1,j-1);
if p1==255
if p2==255
count2=1;
else count2=0;
end;
if p3==255
count3=1;
else count3=0;
end;
if p4==255
count4=1;
else count4=0;
end;
if p5==255
count5=1;
else count5=0;
end;
if p6==255
count6=1;
else count6=0;
end;
if p7==255
count7=1;
else count7=0;
end;
if p8==255
count8=1;
else count8=0;
end;
if p9==255
count9=1;
else count9=0;
end;
else W(i,j)=P(i,j);
end;
count=count2+count3+count4+count5+count6+count7+count8+count9;
if count<2|count>6
W(i,j)=P(i,j);
else
Q(1:8)=[p2,p3,p4,p5,p6,p7,p8,p9];
a=0;
for k=1:7
if Q(k)<Q(k+1)
a=a+1;
end;
end;
if a~=1
W(i,j)=P(i,j);
else
if p2*p4*p6~=0|p4*p6*p8~=0
W(i,j)=P(i,j);
else W(i,j)=0;
end;
end;
end;
end;
end;
[m,n]=size(W);
R=zeros(m,n);
for i=2:m-1
for j=2:n-1
p1=W(i,j);
p2=W(i-1,j);
p3=W(i-1,j+1);
p4=W(i,j+1);
p5=W(i+1,j+1);
p6=W(i+1,j);
p7=W(i+1,j-1);
p8=W(i,j-1);
p9=W(i-1,j-1);
if p1==255
if p2==255
count2=1;
else count2=0;
end;
if p3==255
count3=1;
else count3=0;
end;
if p4==255
count4=1;
else count4=0;
end;
if p5==255
count5=1;
else count5=0;
end;
if p6==255
count6=1;
else count6=0;
end;
if p7==255
count7=1;
else count7=0;
end;
if p8==255
count8=1;
else count8=0;
end;
if p9==255
count9=1;
else count9=0;
end;
else R(i,j)=W(i,j);
end;
count=count2+count3+count4+count5+count6+count7+count8+count9;
if count<2|count>6
R(i,j)=W(i,j);
else
Q(1:8)=[p2,p3,p4,p5,p6,p7,p8,p9];
a=0;
for k=1:7
if Q(k)<Q(k+1)
a=a+1;
end;
end;
if a~=1
R(i,j)=W(i,j);
else
if p2*p4*p8~=0|p2*p6*p8~=0
R(i,j)=W(i,j);
else R(i,j)=0;
end;
end;
end;
end;
end;
while R~=P
P=R;
[m,n]=size(P);
for i=2:m-1
for j=2:n-1
count2=0;count3=0;count4=0;count5=0;count6=0;count7=0;count8=0;count9=0;
p1=P(i,j);
p2=P(i-1,j);
p3=P(i-1,j+1);
p4=P(i,j+1);
p5=P(i+1,j+1);
p6=P(i+1,j);
p7=P(i+1,j-1);
p8=P(i,j-1);
p9=P(i-1,j-1);
if p1==255
if p2==255
count2=1;
else count2=0;
end;
if p3==255
count3=1;
else count3=0;
end;
if p4==255
count4=1;
else count4=0;
end;
if p5==255
count5=1;
else count5=0;
end;
if p6==255
count6=1;
else count6=0;
end;
if p7==255
count7=1;
else count7=0;
end;
if p8==255
count8=1;
else count8=0;
end;
if p9==255
count9=1;
else count9=0;
end;
else W(i,j)=P(i,j);
end;
count=count2+count3+count4+count5+count6+count7+count8+count9;
if count<2|count>6
W(i,j)=P(i,j);
else
Q(1:8)=[p2,p3,p4,p5,p6,p7,p8,p9];
a=0;
for k=1:7
if Q(k)<Q(k+1)
a=a+1;
end;
end;
if a~=1
W(i,j)=P(i,j);
else
if p2*p4*p6~=0|p4*p6*p8~=0
W(i,j)=P(i,j);
else W(i,j)=0;
end;
end;
end;
end;
end;
[m,n]=size(W);
for i=2:m-1
for j=2:n-1
p1=W(i,j);
p2=W(i-1,j);
p3=W(i-1,j+1);
p4=W(i,j+1);
p5=W(i+1,j+1);
p6=W(i+1,j);
p7=W(i+1,j-1);
p8=W(i,j-1);
p9=W(i-1,j-1);
if p1==255
if p2==255
count2=1;
else count2=0;
end;
if p3==255
count3=1;
else count3=0;
end;
if p4==255
count4=1;
else count4=0;
end;
if p5==255
count5=1;
else count5=0;
end;
if p6==255
count6=1;
else count6=0;
end;
if p7==255
count7=1;
else count7=0;
end;
if p8==255
count8=1;
else count8=0;
end;
if p9==255
count9=1;
else count9=0;
end;
else R(i,j)=W(i,j);
end;
count=count2+count3+count4+count5+count6+count7+count8+count9;
if count<2|count>6
R(i,j)=W(i,j);
else
Q(1:8)=[p2,p3,p4,p5,p6,p7,p8,p9];
a=0;
for k=1:7
if Q(k)<Q(k+1)
a=a+1;
end;
end;
if a~=1
R(i,j)=W(i,j);
else
if p2*p4*p8~=0|p2*p6*p8~=0
R(i,j)=W(i,j);
else R(i,j)=0;
end;
end;
end;
end;
end;
end;
figure,imshow(R);
我的那个循环为什么不执行啊 我估计问题出在 条件的表示上 可是弄不好 上面程序 是对P进行边缘细化 ,需要重复那个循环 直到不再有变化为止 关键给我看看怎么让那个循环执行 :'( 谢了 |