I=imread('E:\image\Image_9.BMP');
I=im2bw(I,graythresh(I));
se=strel('line',2,0.5*pi);
I=imerode(I,se);
[y x]=size(I);
%=== 顶端轮廓检测===%
Top=zeros(1,x);
for i=1:x
j=1;
while ((I(j,i)==1)&&(j<y))
j=j+1;
end
Top(i)=y-j;
end
%=== 底部轮廓检测===%
Bottom=zeros(1,x);
for i=1:x
j=y;
while ((I(j,i)==1)&&(j>1))
j=j-1;
end
Bottom(i)=y-j;
end
Height=Top-Bottom;
WordHeight=max(Height);
%=== 轮廓线的凹检测 ===%
TopD=zeros(1,x);
Concave=2;
Deep=0;
DeepH=0;
DeepT=5;
Sign=0;
for i=1:x-1
TopD(i)=Top(i+1)-Top(i);
end
for i=1:x-2
if (TopD(i)<0)
Sign=1;
DeepH=0;
Deep=Deep+TopD(i);
tempX=i+1;
end
if ((Sign==1)&&(TopD(i)>0))
if (abs(Deep)>=DeepT)
DeepH=DeepH+TopD(i);
if (abs(DeepH)>=DeepT)
Concave=[Concave tempX];
Sign=0;
DeepH=0;
end
else
Sign=0;
Deep=0;
end
end
end
%=== 轮廓线的凸检测 ===%
BottomD=zeros(1,x-1);
Convex=1;
Asend=0;
Desend=0;
ConvexT=3;
Sign=0;
for i=1:x-1
BottomD(i)=Bottom(i+1)-Bottom(i);
end
for i=1:x-2
if (BottomD(i)>0)
Sign=1;
Desend=0;
Asend=Asend+BottomD(i);
tempX=i+1 ;
end
if((Sign==1)&&(BottomD(i)<0))
if (abs(Asend)>=ConvexT)
Desend=Desend+BottomD(i);
if (abs(Desend)>=ConvexT)
Convex=[Convex tempX];
Sign=0;
Desend=0;
end
else
Sign=0;
Asend=0;
end
end
end
%=== 切分 ===%
[mytemp n]=size(Concave);
StrokeT=5;
GapT=8;
W=zeros(1,n);
for i=1:n-1
W(i)=Concave(i+1)-Concave(i);
end
W(n)=x-Concave(n);
Width=median(W);
PXR1=1;
PXR2=1;
Mark=0;
CrossSign=0;
Black=zeros(1,x);
BP=zeros(1,x);
SegSoke=zeros(3,x);
RH=zeros(1,x);
RW=zeros(1,x);
Score=zeros(1,x);
XGood=1;
SegY=1;
for k=2:n
WordH=max(Height(Concave(k-1):Concave(k)));
WordW=Concave(k)-Concave(k-1);
if ((WordW>=0.5*Width)&&(WordW<=1.5*Width))
%=== 选定切分的区域 ===%
PX1=Concave(k);
PX2=PX1;
while ((TopD(PX2)==0)&&(PX2<x))
PX2=PX2+2;
end
i=fix((PX1+PX2)/2);
if (Top(i)==1)
PXR1=[PXR1 i];
PXR2=[PXR2 i];
else
j=y+1-Top(i);
Mark=0;
while((j<y)&&(Mark<2))
if (I(j,i)==0)
Black(i)=Black(i)+1;
Si=i;
while ((Si>1)&&(I(j,Si)==0))
Si=Si-1;
SegSoke(1,i)=SegSoke(1,i)+1;
end
Si=i;
while ((Si<x)&&(I(j,Si)==0))
Si=Si+1;
SegSoke(2,i)=SegSoke(2,i)+1;
end
end
Mark=Mark+abs(I(j+1,i)-I(j,i));
j=j+1;
end
SegY=[SegY j-1];
if (j==38)
PXR1=[PXR1 i];
PXR2=[PXR2 i];
else
SLi=i;
while ((SLi>1)&&(I(j-1,SLi)==1))
SLi=SLi-1;
end
SRi=i;
while ((SRi<x)&&(I(j-1,SRi)==1))
SRi=SRi+1;
end
[Mytemp PX2]=max(Bottom(SLi:SRi));
PXR2=[PXR2 PX2+SLi-1];
PXR1=[PXR1 i];
end
end
else if (WordW>1.5*Width)
PX=fix((Concave(k)+Concave(k-1))/2);
k=k-1;
end
end
end;
%=== 分割===%
I=I(10:55,:);
if I(1:46,27)==zeros(1:46,1)
m=1;
else m=0;
end
if I(1:24,164)==zeros(1:24,1)
n=1;
else n=0;
end
if (m==1)||(n==1)
cnt=20;
for i=1:4
WI=I(:,cnt*i+2:cnt*(i+1));
WI=medfilt2(WI,[1 3]);
if (m==1)&&(i<4)
l=i;
figure(l); imshow(WI);
a=sum(WI);
b(l)=sum(a);
elseif (n==1)&&(i>1)&&(m==0)
l=i-1;
figure(l);
imshow(WI);
a=sum(WI);
b(l)=sum(a);
end
end
elseif m==0
cnt=18;
for i=1:4
WI=I(:,cnt*i-1:cnt*(i+1)-2);
if i>1
l=i-1;
figure(l);
imshow(WI);
a=sum(WI);
b(l)=sum(a);
end
end
end
%=== 识别===%
for l=1:3
c=b(l);
if (c==191)|(c==68)|(c==207)|(c==82)|(c==12)|(c==614)|(c==672)|(c==345)|(c==549)|(c==723)
d(l)='0';
elseif (c==718)|(c==719)|(c==570)|(c==787)
d(l)='2';
elseif (c==710)|(c==736)
d(l)='6';
elseif (c==660)| (c==793)
d(l)='1';
elseif (c==751)|(c==832)|(c==694)
d(l)='9';
elseif (c==729)|(c==532)|(c==727)
d(l)='8';
elseif (c==730)|(c==803)
d(l)='5';
elseif (c==700)|(c==773)|(c==800)
d(l)='4';
else
d(l)=' ';
end
end
disp(d);
- 1
- 2
- 3
- 4
- 5
- 6
前往页