当前位置:文档之家› 车牌识别matlab程序[1]

车牌识别matlab程序[1]

[, pathname] = uigetfile({'*.jpg', 'JPEG文件(*.jpg)';'*.bmp','BMP文件(*.bmp)';});
if( == 0), return, end
global %声明全局变量
= [pathname ];
I=imread();
imshow(I); %显示图像I
I1=rgb2gray(I);%RGB图转化为灰度图
figure,imshow(I1);
w1=medfilt2(I1);
figure,imshow(w1);
s1=histeq(w1,256);
figure,imshow(s1);
t1=imadjust(s1);
figure,imshow(t1);
I2=edge(t1,'robert',0.15,'both'); %用ROBERT算子提取图像边缘
figure,imshow(I2);
se=[1;1;1];
I3=imerode(I2,se); %弱化二进制图像I2的边缘
figure,imshow(I3);%为定位车牌,将白色区域膨胀,腐蚀去无关的小物件,包括车牌字符(下面两句)
se=strel('rectangle',[25,20]); %用来腐蚀的形状为矩形,面积20*25
I4=imclose(I3,se);
figure,imshow(I4);
I5=bwareaopen(I4,2000); %去除图像中面积过小的,可以肯定不是车牌的区域。

figure,imshow(I5);
[y,x,z]=size(I5); %z=1。

y,x分别为I5图像的高和宽
myI=double(I5)
%myI=I5;
tic
%begin横向扫描
white_y=zeros(y,1); %white_y为y行1列的零矩阵
for i=1:y
for j=1:x
if(myI(i,j,1)==1)
%如果myI(i,j,1)即myI图像中坐标为(i,j)的点为白色
%则white_y的相应行的元素white_y(i,1)值加1
white_y(i,1)= white_y(i,1)+1;
end
end
end
[temp MaxY]=max(white_y); %temp为向量white_y的元素中的最大值,MaxY为该值的索引(在向量中的位置)
PY1=MaxY;
while ((white_y(PY1,1)>=80)&&(PY1>1))
PY1=PY1-1;
end
PY2=MaxY;
while ((white_y(PY2,1)>=80)&&(PY2<y))
PY2=PY2+1;
end
IY=I(PY1:PY2,:,:); %IY为原始图像I中截取的纵坐标在PY1:PY2之间的部分%end横向扫描
%begin纵向扫描
white_x=zeros(1,x);
for j=1:x
for i=PY1:PY2
if(myI(i,j,1)==1)
white_x(1,j)= white_x(1,j)+1;
end
end
end
PX1=1;
while ((white_x(1,PX1)<3)&&(PX1<x))
PX1=PX1+1;
end
PX2=x;
while ((white_x(1,PX2)<3)&&(PX2>PX1))
PX2=PX2-1;
end
%end纵向扫描
PX1=PX1-2;
PX2=PX2+2;
Plate=I(PY1:PY2,:);
t=toc
figure,plot(white_y);grid
figure,plot(white_x);grid
figure,imshow(IY);
figure,imshow(Plate);
% if isrgd(Plate);
Plate3=rgb2gray(Plate);
%else
% Plate2=Plate;
%end
plate4=medfilt2(Plate3);
plate5=histeq(plate4,256);
Plate2=imadjust(plate5);
g_max=double(max(max(Plate2))); g_min=double(min(min(Plate2)));
t=round(g_max-(g_max-g_min)/2); [m,n]=size(Plate2);
Plate2=im2bw(Plate2,t/256); figure,imshow(Plate2);
plate=bwareaopen(Plate2,20); figure,imshow(plate);
[y1,x1,z1]=size(plate);
plate1=double(plate);
% tt=1;
Y1=zeros(y1,1);
for i=1:y1
for j=1:x1
if(plate1(i,j,1)==1)
Y1(i,1)=Y1(i,1)+1;
end
end
end
py1=1;
py0=1;
while((Y1(py0,1)<20)&&(py0<y1)) py0=py0+1;
end
py1=py0;
while((Y1(py1,1)>=20)&&(py1<y1)) py1=py1+1;
end
plate=plate(py0:py1,:,:);
figure,imshow(plate);
X1=zeros(1,x1);
for j=1:x1
for i=1:y1
if(plate1(i,j,1)==1)
X1(1,j)=X1(1,j)+1;
end
end
end
figure
plot(0:x1-1,X1)
px0=1;
px1=1;
for i=1:7
while((X1(1,px0)<3)&&(px0<x1))
px0=px0+1;
end
px1=px0;
while(((X1(1,px1)>=3)&&(px1<x1))||((px1-px0)<10)) px1=px1+1;
end
Z=plate(:,px0:px1,:);
switch strcat('Z',num2str(i))
case 'Z1'
PIN0=Z;
case 'Z2'
PIN1=Z;
case 'Z3'
PIN2=Z;
case 'Z4'
PIN3=Z;
case 'Z5'
PIN4=Z;
case 'Z6'
PIN5=Z;
otherwise
PIN6=Z;
end
figure
% subplot(1,7,i);
imshow(Z);
px0=px1;
end。

相关主题