程序出错:关于轮廓提取(爬虫法)
?? Subscript indices must either be real positive integers or logicals.Error in ==> C:\MATLAB6p5\work\elea\Untitled3.m
On line 33==> if (BW_new(i,j+1)==1)&(BW_new(i,j+2)==1)&(BW_new(i,j-1)==1)&(BW_new(i,j-2)==1)&(BW_new(i,j+3)==1)&(BW_new(i,j-3)==1)&(BW_new(i,j+4)==1)&(BW_new(i,j-4)==1)
大侠相救,谢谢
s=[];
s=size(BW_f);
row=s(1);
line=s(2);
BW_new=ones(row,line); %创建新图
for i=2:(row-1)
for j=1:(line-1)
if (BW_l(i,j)==0)&(BW_new(i,j)~=0)
p=0
if (line-1)>j>=1
if (BW_new(i,j+1)==1)&(BW_new(i,j+2)==1)&(BW_new(i,j-1)==1)&(BW_new(i,j-2)==1)&(BW_new(i,j+3)==1)&(BW_new(i,j-3)==1)&(BW_new(i,j+4)==1)&(BW_new(i,j-4)==1)
p=1
end
elseif j==(line-1)
if (BW_new(i,line)==1)&(BW_new(i,j-1)==1)
p=1
end
end %p==1表明找到起始点
if p==1
BW_new(i,j)=0
stai=i;
staj=j;
A=;
D=[];
B=[];
C=zeros(1,4);
e=0;
while (1<i<row)&(1<j<line)&(e==0)
if A==[-1,0]
D(1)=BW_f(i-1,j-1);
B(1,1)=i-1;
B(1,2)=j-1;
u1=1;
d1=-1;
l1=1;
r1=-1;
D(2)=BW_f(i-1,j);
B(2,1)=i-1;
B(2,2)=j;
u2=1;
d2=-1;
l2=0;
r2=0;
D(3)=BW_f(i-1,j+1);
B(3,1)=i-1;
B(3,2)=j+1;
u3=1;
d3=-1;
l3=-1;
r3=1
elseif A==[-1, 1]
D(1)=BW_f(i-1,j);
B(1,1)=i-1;
B(1,2)=j;
u1=1;
d1=-1;
l1=0;
r1=0;
D(2)=BW_f(i-1,j+1);
B(2,1)=i-1;
B(2,2)=j+1;
u2=1;
d2=-1;
l2=-1;
r2=1;
D(3)=BW_f(i,j+1);
B(3,1)=i;
B(3,2)=j+1;
u3=0;
d3=0;
r3=1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;
r3=-1;
l3=1;
else
D(1)=BW_f(i,j-1);
B(1,1)=i;
B(1,2)=j-1;
u1=0;
d1=0;
l1=1;
r1=-1;
D(2)=BW_f(i-1,j-1);
B(2,1)=i-1;
B(2,2)=j-1;
u2=1;
d2=-1;
r2=-1;
l2=1;
D(3)=BW_f(i-1,j);
B(3,1)=i-1;
B(3,2)=j;
u3=1;
d3=-1;
l3=0;
r3=0;
end
d=0;
F=ones(1,3);
for v=1:3
if (D(v)==0)&(BW_new(B(v,1),B(v,2))==1)
F(v)=0
end
end
d=F(1)+F(2)+F(3);
if d==2
v1=1
while F(v1)==1
v1=v1+1
end
if ((C(1)<3)&(C(2)<3)&(C(3)<3)&(C(4)<3))
switch v1
case 1
C(1)=C(1)+u1;
C(2)=C(2)+d1;
C(3)=C(3)+r1;
C(4)=C(4)+l1;
case 2
C(1)=C(1)+u2;
C(2)=C(2)+d2;
C(3)=C(3)+r2;
C(4)=C(4)+l2;
otherwise
C(1)=C(1)+u3;
C(2)=C(2)+d3;
C(3)=C(3)+r3;
C(4)=C(4)+l3;
end
end
BW_new(B(v1,1),B(v1,2))=0;
k=i;
l=j;
i=B(v1,1);
j=B(v1,2);
elseif d==1
if F(1)==0
v1=1
if F(2)==0
v2=2
else
v2=3
end
else
v1=2
v2=3
end
if (p_l(B(v1,1),B(v1,2))==0)&(p_l(B(v2,1),B(v2,2))==1)
if ((C(1)<3)&(C(2)<3)&(C(3)<3)&(C(4)<3))
switch v1
case 1
C(1)=C(1)+u1;
C(2)=C(2)+d1;
C(3)=C(3)+r1;
C(4)=C(4)+l1;
case 2
C(1)=C(1)+u2;
C(2)=C(2)+d2;
C(3)=C(3)+r2;
C(4)=C(4)+l2;
otherwise
C(1)=C(1)+u3;
C(2)=C(2)+d3;
C(3)=C(3)+r3;
C(4)=C(4)+l3;
end
end
BW_new(B(v1,1),B(v1,2))=0;
k=i;
l=j;
i=B(v1,1);
j=B(v1,2);
elseif (BW_l(B(v1,1),B(v1,2))==1)&(BW_l(B(v2,1),B(v2,2))==0)
if ((C(1)<3)&(C(2)<3)&(C(3)<3)&(C(4)<3))
switch v2
case 1
C(1)=C(1)+u1;
C(2)=C(2)+d1;
C(3)=C(3)+r1;
C(4)=C(4)+l1;
case 2
C(1)=C(1)+u2;
C(2)=C(2)+d2;
C(3)=C(3)+r2;
C(4)=C(4)+l2;
otherwise
C(1)=C(1)+u3;
C(2)=C(2)+d3;
C(3)=C(3)+r3;
C(4)=C(4)+l3;
end
end
BW_new(B(v2,1),B(v2,2))=0;
k=i;
l=j;
i=B(v2,1);
j=B(v2,2);
else
if (v1==2)|(v2==2)
if v1==2
if((C(1)<3)&(C(2)<3)&(C(3)<3)&(C(4)<3))
switch v1
case 1
C(1)=C(1)+u1;
C(2)=C(2)+d1;
C(3)=C(3)+r1;
C(4)=C(4)+l1;
case 2
C(1)=C(1)+u2;
C(2)=C(2)+d2;
C(3)=C(3)+r2;
C(4)=C(4)+l2;
otherwise
C(1)=C(1)+u3;
C(2)=C(2)+d3;
C(3)=C(3)+r3;
C(4)=C(4)+l3;
end
end
BW_new(B(v1,1),B(v1,2))=0;
k=i;
l=j;
i=B(v1,1);
j=B(v1,2);
else
if ((C(1)<3)&(C(2)<3)&(C(3)<3)&(C(4)<3))
switch v2
case 1
C(1)=C(1)+u1;
C(2)=C(2)+d1;
C(3)=C(3)+r1;
C(4)=C(4)+l1;
case 2
C(1)=C(1)+u2;
C(2)=C(2)+d2;
C(3)=C(3)+r2;
C(4)=C(4)+l2;
otherwise
C(1)=C(1)+u3;
C(2)=C(2)+d3;
C(3)=C(3)+r3;
C(4)=C(4)+l3;
end
end
BW_new(B(v2,1),B(v2,2))=0;
k=i;
l=j;
i=B(v2,1);
j=B(v2,2);
end
else
if ((C(1)<3)&(C(2)<3)&(C(3)<3)&(C(4)<3))
C(1)=C(1)+u3;
C(2)=C(2)+d3;
C(3)=C(3)+r3;
C(4)=C(4)+l3;
end
BW_new(B(3,1),B(3,2))=0;
k=i;
l=j;
i=B(3,1);
j=B(3,2);
end
end
elseif d==0
if BW_l(B(1,1),B(1,2))+BW_l(B(2,1),B(2,2))+BW_l(B(3,1),B(3,2))==2
v1=1;
while BW_l(B(v1,1),B(v1,2))==1
v1=v1+1
end
if ((C(1)<3)&(C(2)<3)&(C(3)<3)&(C(4)<3))
switch v1
case 1
C(1)=C(1)+u1;
C(2)=C(2)+d1;
C(3)=C(3)+r1;
C(4)=C(4)+l1;
case 2
C(1)=C(1)+u2;
C(2)=C(2)+d2;
C(3)=C(3)+r2;
C(4)=C(4)+l2;
otherwise
C(1)=C(1)+u3;
C(2)=C(2)+d3;
C(3)=C(3)+r3;
C(4)=C(4)+l3;
end
end
BW_new(B(v1,1),B(v1,2))=0;
k=i;
l=j;
i=B(v1,1);
j=B(v1,2);
elseif BW_l(B(1,1),B(1,2))+BW_l(B(2,1),B(2,2))+BW_l(B(3,1),B(3,2))==1
if ((C(1)<3)&(C(2)<3)&(C(3)<3)&(C(4)<3))
C(1)=C(1)+u3;
C(2)=C(2)+d3;
C(3)=C(3)+r3;
C(4)=C(4)+l3;
end
BW_new(B(3,1),B(3,2))=0;
k=i;
l=j;
i=B(3,1);
j=B(3,2);
elseif BW_l(B(1,1),B(1,2))+BW_l(B(2,1),B(2,2))+BW_l(B(3,1),B(3,2))==0
BW_new(B(2,1),B(2,2))=0;
k=i;
l=j;
i=B(2,1);
j=B(2,2);
else
BW_new(B(2,1),B(2,2))=0;
k=i;
l=j;
i=B(2,1);
j=B(2,2);
end
else
break
end
A(1)=i-k;
A(2)=j-l;
if (i==1)|(i==row)|(j==1)|(j==line)
break
end
if ((C(1)>=3)|(C(2)>=3)|(C(3)>=3)|(C(4)>=3))
if (abs(stai-i)<=1)&(abs(staj-j)<=1)
c=1
elseif (abs(stai-i)==2)&(abs(staj-j)==2)
BW_new(i+((stai-i)/2),j+((staj-j)/2))=0
elseif stai==i
BW_new(i,j+((staj-j)/2))=0
elseif staj==j
BW_new(i+((stai-i)/2),j)=0
elseif ((staj-j)==2)&(abs(stai-i)==1)
BW_new(i,j+1)=0
elseif ((staj-i)==-2)&(abs(staj-j)==1)
BW_new(i-1,j)=0
elseif ((staj-j)==-2)&(abs(stai-i)==1)
BW_new(i,j-1)=0
elseif ((staj-i)==2)&(abs(staj-j)==1)
BW_new(i+1,j)=0
end
end
end
end
end
end
end
I1=im2double(rgb2gray(RGB));
=imhist(I1);
m=max(counts);
A=[];
j=1;
for i=6:251
if((counts(i+5)-counts(i))>0)&((counts(i)-counts(i-5))<0)&(counts(i)<m)
A(j)=i
j=j+1
end
end
[ 本帖最后由 eight 于 2007-6-4 23:48 编辑 ] 这是MATLAB程序? 感觉象FORTRAN的。
你的问题是矩阵的下标不对,应该从1开始。建议将程序做些优化。 原帖由 berylyl 于 2007-5-31 17:14 发表 http://www.chinavib.com/forum/images/common/back.gif
?? Subscript indices must either be real positive integers or logicals.
Error in ==> C:\MATLAB6p5\work\elea\Untitled3.m
On line 33==> if (BW_new(i,j+1)==1)&(BW_new(i,j+2)== ...
这类错误建议自己调试一下,保证下标引用是正整数
改过了,现在遇到新问题
s=[];s=size(BW_f);
row=s(1);
line=s(2);
BW_new=ones(row,line); %创建新图
for i=2:(row-1)
for j=5:(line-1)
if (BW_l(i,j)==0)&(BW_new(i,j)~=0)
p=0
if (line-1)>j>=1
if (BW_new(i,j+1)==1)&(BW_new(i,j+2)==1)&(BW_new(i,j-1)==1)&(BW_new(i,j-2)==1)&(BW_new(i,j+3)==1)&(BW_new(i,j-3)==1)&(BW_new(i,j+4)==1)&(BW_new(i,j-4)==1)
p=1
end
elseif j==(line-1)
if (BW_new(i,line)==1)&(BW_new(i,j-1)==1)
p=1
end
end %p==1表明找到起始点
if p==1
BW_new(i,j)=0
stai=i;
staj=j;
A=;
D=[];
B=[];
C=zeros(1,4);
e=0;
while (1<i<row)&(1<j<line)&(e==0)
if A==[-1,0]
D(1)=BW_f(i-1,j-1);
B(1,1)=i-1;
B(1,2)=j-1;
u1=1;
d1=-1;
l1=1;
r1=-1;
D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r1=0;
D(2)=BW_f(i+1,j-1);
B(2,1)=i+1;
B(2,2)=j-1;
u2=-1;
d2=1;
l2=1;
r2=-1;
D(3)=BW_f(i,j-1);
B(3,1)=i;
B(3,2)=j-1;
u3=0;
d3=0;
l3=1;
r3=-1;
elseif A==
D(1)=BW_f(i+1,j-1);
B(1,1)=i+1;
B(1,2)=j-1;
u1=-1;
d1=1;
l1=1;
r1=-1;
D(2)=BW_f(i,j-1);
B(2,1)=i;
B(2,2)=j-1;
u2=0;
d2=0;
l2=1;
r2=-1;
D(3)=BW_f(i-1,j-1);
B(3,1)=i-1;
B(3,2)=j-1;
u3=1;
d3=-1;
r3=-1;
l3=1;
else
D(1)=BW_f(i,j-1);
B(1,1)=i;
B(1,2)=j-1;
u1=0;
d1=0;
l1=1;
r1=-1;
D(2)=BW_f(i-1,j-1);
B(2,1)=i-1;
B(2,2)=j-1;
u2=1;
d2=-1;
r2=-1;
l2=1;
D(3)=BW_f(i-1,j);
B(3,1)=i-1;
B(3,2)=j;
u3=1;
d3=-1;
l3=0;
r3=0;
end
d=0;
F=ones(1,3);
for v=1:3
if (D(v)==0)&(BW_new(B(v,1),B(v,2))==1)
F(v)=0
end
end
d=F(1)+F(2)+F(3);
if d==2
v1=1
while F(v1)==1
v1=v1+1
end
if ((C(1)<3)&(C(2)<3)&(C(3)<3)&(C(4)<3))
switch v1
case 1
C(1)=C(1)+u1;
C(2)=C(2)+d1;
C(3)=C(3)+r1;
C(4)=C(4)+l1;
case 2
C(1)=C(1)+u2;
C(2)=C(2)+d2;
C(3)=C(3)+r2;
C(4)=C(4)+l2;
otherwise
C(1)=C(1)+u3;
C(2)=C(2)+d3;
C(3)=C(3)+r3;
C(4)=C(4)+l3;
end
end
BW_new(B(v1,1),B(v1,2))=0;
k=i;
l=j;
i=B(v1,1);
j=B(v1,2);
elseif d==1
if F(1)==0
v1=1
if F(2)==0
v2=2
else
v2=3
end
else
v1=2
v2=3
end
if (p_l(B(v1,1),B(v1,2))==0)&(p_l(B(v2,1),B(v2,2))==1)
if ((C(1)<3)&(C(2)<3)&(C(3)<3)&(C(4)<3))
switch v1
case 1
C(1)=C(1)+u1;
C(2)=C(2)+d1;
C(3)=C(3)+r1;
C(4)=C(4)+l1;
case 2
C(1)=C(1)+u2;
C(2)=C(2)+d2;
C(3)=C(3)+r2;
C(4)=C(4)+l2;
otherwise
C(1)=C(1)+u3;
C(2)=C(2)+d3;
C(3)=C(3)+r3;
C(4)=C(4)+l3;
end
end
BW_new(B(v1,1),B(v1,2))=0;
k=i;
l=j;
i=B(v1,1);
j=B(v1,2);
elseif (BW_l(B(v1,1),B(v1,2))==1)&(BW_l(B(v2,1),B(v2,2))==0)
if ((C(1)<3)&(C(2)<3)&(C(3)<3)&(C(4)<3))
switch v2
case 1
C(1)=C(1)+u1;
C(2)=C(2)+d1;
C(3)=C(3)+r1;
C(4)=C(4)+l1;
case 2
C(1)=C(1)+u2;
C(2)=C(2)+d2;
C(3)=C(3)+r2;
C(4)=C(4)+l2;
otherwise
C(1)=C(1)+u3;
C(2)=C(2)+d3;
C(3)=C(3)+r3;
C(4)=C(4)+l3;
end
end
BW_new(B(v2,1),B(v2,2))=0;
k=i;
l=j;
i=B(v2,1);
j=B(v2,2);
else
if (v1==2)|(v2==2)
if v1==2
if((C(1)<3)&(C(2)<3)&(C(3)<3)&(C(4)<3))
switch v1
case 1
C(1)=C(1)+u1;
C(2)=C(2)+d1;
C(3)=C(3)+r1;
C(4)=C(4)+l1;
case 2
C(1)=C(1)+u2;
C(2)=C(2)+d2;
C(3)=C(3)+r2;
C(4)=C(4)+l2;
otherwise
C(1)=C(1)+u3;
C(2)=C(2)+d3;
C(3)=C(3)+r3;
C(4)=C(4)+l3;
end
end
BW_new(B(v1,1),B(v1,2))=0;
k=i;
l=j;
i=B(v1,1);
j=B(v1,2);
else
if ((C(1)<3)&(C(2)<3)&(C(3)<3)&(C(4)<3))
switch v2
case 1
C(1)=C(1)+u1;
C(2)=C(2)+d1;
C(3)=C(3)+r1;
C(4)=C(4)+l1;
case 2
C(1)=C(1)+u2;
C(2)=C(2)+d2;
C(3)=C(3)+r2;
C(4)=C(4)+l2;
otherwise
C(1)=C(1)+u3;
C(2)=C(2)+d3;
C(3)=C(3)+r3;
C(4)=C(4)+l3;
end
end
BW_new(B(v2,1),B(v2,2))=0;
k=i;
l=j;
i=B(v2,1);
j=B(v2,2);
end
else
if ((C(1)<3)&(C(2)<3)&(C(3)<3)&(C(4)<3))
C(1)=C(1)+u3;
C(2)=C(2)+d3;
C(3)=C(3)+r3;
C(4)=C(4)+l3;
end
BW_new(B(3,1),B(3,2))=0;
k=i;
l=j;
i=B(3,1);
j=B(3,2);
end
end
elseif d==0
if BW_l(B(1,1),B(1,2))+BW_l(B(2,1),B(2,2))+BW_l(B(3,1),B(3,2))==2
v1=1;
while BW_l(B(v1,1),B(v1,2))==1
v1=v1+1
end
if ((C(1)<3)&(C(2)<3)&(C(3)<3)&(C(4)<3))
switch v1
case 1
C(1)=C(1)+u1;
C(2)=C(2)+d1;
C(3)=C(3)+r1;
C(4)=C(4)+l1;
case 2
C(1)=C(1)+u2;
C(2)=C(2)+d2;
C(3)=C(3)+r2;
C(4)=C(4)+l2;
otherwise
C(1)=C(1)+u3;
C(2)=C(2)+d3;
C(3)=C(3)+r3;
C(4)=C(4)+l3;
end
end
BW_new(B(v1,1),B(v1,2))=0;
k=i;
l=j;
i=B(v1,1);
j=B(v1,2);
elseif BW_l(B(1,1),B(1,2))+BW_l(B(2,1),B(2,2))+BW_l(B(3,1),B(3,2))==1
if ((C(1)<3)&(C(2)<3)&(C(3)<3)&(C(4)<3))
C(1)=C(1)+u3;
C(2)=C(2)+d3;
C(3)=C(3)+r3;
C(4)=C(4)+l3;
end
BW_new(B(3,1),B(3,2))=0;
k=i;
l=j;
i=B(3,1);
j=B(3,2);
elseif BW_l(B(1,1),B(1,2))+BW_l(B(2,1),B(2,2))+BW_l(B(3,1),B(3,2))==0
BW_new(B(2,1),B(2,2))=0;
k=i;
l=j;
i=B(2,1);
j=B(2,2);
else
BW_new(B(2,1),B(2,2))=0;
k=i;
l=j;
i=B(2,1);
j=B(2,2);
end
else
break
end
A(1)=i-k;
A(2)=j-l;
if (i==1)|(i==row)|(j==1)|(j==line)
break
end
if ((C(1)>=3)|(C(2)>=3)|(C(3)>=3)|(C(4)>=3))
if (abs(stai-i)<=1)&(abs(staj-j)<=1)
c=1
elseif (abs(stai-i)==2)&(abs(staj-j)==2)
BW_new(i+((stai-i)/2),j+((staj-j)/2))=0
elseif stai==i
BW_new(i,j+((staj-j)/2))=0
elseif staj==j
BW_new(i+((stai-i)/2),j)=0
elseif ((staj-j)==2)&(abs(stai-i)==1)
BW_new(i,j+1)=0
elseif ((staj-i)==-2)&(abs(staj-j)==1)
BW_new(i-1,j)=0
elseif ((staj-j)==-2)&(abs(stai-i)==1)
BW_new(i,j-1)=0
elseif ((staj-i)==2)&(abs(staj-j)==1)
BW_new(i+1,j)=0
end
end
end
end
end
end
end
有偿帮忙
如果你认为你可以的话,电邮我beryl.yinlu@163.comRGB=imread('C:\Documents and Settings\Admin\beryl\DSCN04660.jpg');
I1=im2double(rgb2gray(RGB));
=imhist(I1);
m=max(counts);
A=[];
j=1;
for i=9:248
if((counts(i+8)-counts(i))>0)&((counts(i)-counts(i-8))<0)&(counts(i)<m)
A(j)=i
j=j+1
end
end
k=max(A);
level1=x(k+8);
level2=x(k-8);
BW_h=im2double(im2bw(I1,level1));%BW_h为高阈值对应的二值图像
BW_l=im2double(im2bw(I1,level2));%BW_l为低阈值对应的二值图像
SE=ones(3,3);
BW_f=erode(BW_l,SE); %BW_f为对BW_h腐蚀后的结果
s=[];
s=size(BW_f);
row=s(1);
line=s(2);
BW_new=ones(row,line); %创建新图
for i=2:(row-1)
for j=5:(line-1)
if (BW_f(i,j)==0)&(BW_new(i,j)~=0)
p=0
if (line-1)>j>=1
if (BW_new(i,j+1)==1)&(BW_new(i,j+2)==1)&(BW_new(i,j-1)==1)&(BW_new(i,j-2)==1)&(BW_new(i,j+3)==1)&(BW_new(i,j-3)==1)&(BW_new(i,j+4)==1)&(BW_new(i,j-4)==1)
p=1
end
elseif j==(line-1)
if (BW_new(i,line)==1)&(BW_new(i,j-1)==1)
p=1
end
end %p==1表明找到起始点
if p==1
BW_new(i,j)=0
stai=i;
staj=j;
A=;
D=[];
B=[];
C=zeros(1,4);
e=0;
while (3<i<row)&(3<j<line)&(e==0)
if A==[-1,0]
D(1)=BW_f(i-1,j-1);
B(1,1)=i-1;
B(1,2)=j-1;
u1=1;
d1=-1;
l1=1;
r1=-1;
D(2)=BW_f(i-1,j);
B(2,1)=i-1;
B(2,2)=j;
u2=1;
d2=-1;
l2=0;
r2=0;
D(3)=BW_f(i-1,j+1);
B(3,1)=i-1;
B(3,2)=j+1;
u3=1;
d3=-1;
l3=-1;
r3=1
elseif A==[-1, 1]
D(1)=BW_f(i-1,j);
B(1,1)=i-1;
B(1,2)=j;
u1=1;
d1=-1;
l1=0;
r1=0;
D(2)=BW_f(i-1,j+1);
B(2,1)=i-1;
B(2,2)=j+1;
u2=1;
d2=-1;
l2=-1;
r2=1;
D(3)=BW_f(i,j+1);
B(3,1)=i;
B(3,2)=j+1;
u3=0;
d3=0;
r3=1;
l3=-1;
elseif A==
D(1)=BW_f(i-1,j+1);
B(1,1)=i-1;
B(1,2)=j+1;
u1=1;
d1=-1;
l1=-1;
r1=1;
D(2)=BW_f(i,j+1);
B(2,1)=i;
B(2,2)=j+1;
u2=0;
d2=0;
r2=1;
l2=-1;
D(3)=BW_f(i+1,j+1);
B(3,1)=i+1;
B(3,2)=j+1;
u3=-1;
d3=1;
r3=1;
l3=-1;
elseif A==
D(1)=BW_f(i,j+1);
B(1,1)=i;
B(1,2)=j+1;
u1=0;
d1=0;
r1=1;
l1=-1;
D(2)=BW_f(i+1,j+1);
B(2,1)=i+1;
B(2,2)=j+1;
u2=-1;
d2=1;
r2=1;
l2=-1;
D(3)=BW_f(i+1,j);
B(3,1)=i+1;
B(3,2)=j;
u3=-1;
d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(3)=C(3)+r1;
C(4)=C(4)+l1;
case 2
C(1)=C(1)+u2;
C(2)=C(2)+d2;
C(3)=C(3)+r2;
C(4)=C(4)+l2;
otherwise
C(1)=C(1)+u3;
C(2)=C(2)+d3;
C(3)=C(3)+r3;
C(4)=C(4)+l3;
end
end
BW_new(B(v1,1),B(v1,2))=0;
k=i;
l=j;
i=B(v1,1);
j=B(v1,2);
elseif d==1
if F(1)==0
v1=1
if F(2)==0
v2=2
else
v2=3
end
else
v1=2
v2=3
end
if (BW_l(B(v1,1),B(v1,2))==0)&(BW_l(B(v2,1),B(v2,2))==1)
if ((C(1)<3)&(C(2)<3)&(C(3)<3)&(C(4)<3))
switch v1
case 1
C(1)=C(1)+u1;
C(2)=C(2)+d1;
C(3)=C(3)+r1;
C(4)=C(4)+l1;
case 2
C(1)=C(1)+u2;
C(2)=C(2)+d2;
C(3)=C(3)+r2;
C(4)=C(4)+l2;
otherwise
C(1)=C(1)+u3;
C(2)=C(2)+d3;
C(3)=C(3)+r3;
C(4)=C(4)+l3;
end
end
BW_new(B(v1,1),B(v1,2))=0;
k=i;
l=j;
i=B(v1,1);
j=B(v1,2);
elseif (BW_l(B(v1,1),B(v1,2))==1)&(BW_l(B(v2,1),B(v2,2))==0)
if ((C(1)<3)&(C(2)<3)&(C(3)<3)&(C(4)<3))
switch v2
case 1
C(1)=C(1)+u1;
C(2)=C(2)+d1;
C(3)=C(3)+r1;
C(4)=C(4)+l1;
case 2
C(1)=C(1)+u2;
C(2)=C(2)+d2;
C(3)=C(3)+r2;
C(4)=C(4)+l2;
otherwise
C(1)=C(1)+u3;
C(2)=C(2)+d3;
C(3)=C(3)+r3;
C(4)=C(4)+l3;
end
end
BW_new(B(v2,1),B(v2,2))=0;
k=i;
l=j;
i=B(v2,1);
j=B(v2,2);
else
if (v1==2)|(v2==2)
if v1==2
if((C(1)<3)&(C(2)<3)&(C(3)<3)&(C(4)<3))
switch v1
case 1
C(1)=C(1)+u1;
C(2)=C(2)+d1;
C(3)=C(3)+r1;
C(4)=C(4)+l1;
case 2
C(1)=C(1)+u2;
C(2)=C(2)+d2;
C(3)=C(3)+r2;
C(4)=C(4)+l2;
otherwise
C(1)=C(1)+u3;
C(2)=C(2)+d3;
C(3)=C(3)+r3;
C(4)=C(4)+l3;
end
end
BW_new(B(v1,1),B(v1,2))=0;
k=i;
l=j;
i=B(v1,1);
j=B(v1,2);
else
if ((C(1)<3)&(C(2)<3)&(C(3)<3)&(C(4)<3))
switch v2
case 1
C(1)=C(1)+u1;
C(2)=C(2)+d1;
C(3)=C(3)+r1;
C(4)=C(4)+l1;
case 2
C(1)=C(1)+u2;
C(2)=C(2)+d2;
C(3)=C(3)+r2;
C(4)=C(4)+l2;
otherwise
C(1)=C(1)+u3;
C(2)=C(2)+d3;
C(3)=C(3)+r3;
C(4)=C(4)+l3;
end
end
BW_new(B(v2,1),B(v2,2))=0;
k=i;
l=j;
i=B(v2,1);
j=B(v2,2);
end
else
if ((C(1)<3)&(C(2)<3)&(C(3)<3)&(C(4)<3))
C(1)=C(1)+u3;
C(2)=C(2)+d3;
C(3)=C(3)+r3;
C(4)=C(4)+l3;
end
BW_new(B(3,1),B(3,2))=0;
k=i;
l=j;
i=B(3,1);
j=B(3,2);
end
end
elseif d==0
if BW_l(B(1,1),B(1,2))+BW_l(B(2,1),B(2,2))+BW_l(B(3,1),B(3,2))==2
v1=1;
while BW_l(B(v1,1),B(v1,2))==1
v1=v1+1
end
if ((C(1)<3)&(C(2)<3)&(C(3)<3)&(C(4)<3))
switch v1
case 1
C(1)=C(1)+u1;
C(2)=C(2)+d1;
C(3)=C(3)+r1;
C(4)=C(4)+l1;
case 2
C(1)=C(1)+u2;
C(2)=C(2)+d2;
C(3)=C(3)+r2;
C(4)=C(4)+l2;
otherwise
C(1)=C(1)+u3;
C(2)=C(2)+d3;
C(3)=C(3)+r3;
C(4)=C(4)+l3;
end
end
BW_new(B(v1,1),B(v1,2))=0;
k=i;
l=j;
i=B(v1,1);
j=B(v1,2);
elseif BW_f(B(1,1),B(1,2))+BW_f(B(2,1),B(2,2))+BW_f(B(3,1),B(3,2))==1
if ((C(1)<3)&(C(2)<3)&(C(3)<3)&(C(4)<3))
C(1)=C(1)+u3;
C(2)=C(2)+d3;
C(3)=C(3)+r3;
C(4)=C(4)+l3;
end
BW_new(B(3,1),B(3,2))=0;
k=i;
l=j;
i=B(3,1);
j=B(3,2);
elseif BW_f(B(1,1),B(1,2))+BW_f(B(2,1),B(2,2))+BW_f(B(3,1),B(3,2))==0
BW_new(B(2,1),B(2,2))=0;
k=i;
l=j;
i=B(2,1);
j=B(2,2);
else
BW_new(B(2,1),B(2,2))=0;
k=i;
l=j;
i=B(2,1);
j=B(2,2);
end
else
break
end
A(1)=i-k;
A(2)=j-l;
if (i==1)|(i==row)|(j==1)|(j==line)
break
end
if ((C(1)>=3)|(C(2)>=3)|(C(3)>=3)|(C(4)>=3))
if (abs(stai-i)<=1)&(abs(staj-j)<=1)
c=1
elseif (abs(stai-i)==2)&(abs(staj-j)==2)
BW_new(i+floor((stai-i)/2),j+floor((staj-j)/2))=0
elseif stai==i
BW_new(i,j+floor((staj-j)/2))=0
elseif staj==j
BW_new(i+floor((stai-i)/2),j)=0
elseif ((staj-j)==2)&(abs(stai-i)==1)
BW_new(i,j+1)=0
elseif ((staj-i)==-2)&(abs(staj-j)==1)
BW_new(i-1,j)=0
elseif ((staj-j)==-2)&(abs(stai-i)==1)
BW_new(i,j-1)=0
elseif ((staj-i)==2)&(abs(staj-j)==1)
BW_new(i+1,j)=0
end
end
end
end
end
end
end
[ 本帖最后由 berylyl 于 2007-6-5 01:19 编辑 ]
kk
如果您可以做的话,联系我,价钱详谈.对程序的评价
这个程序写得有一点点点的 Lan:@(事实就是这样!看了就头大
沉了吧,解决了,特此感谢一个好朋友呢
沉了沉了
页:
[1]