马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有账号?我要加入
x
?? 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=[0,1];
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(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2=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
I1=im2double(rgb2gray(RGB));
[counts,x]=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 编辑 ] |