车牌识别程序及说明.docx
《车牌识别程序及说明.docx》由会员分享,可在线阅读,更多相关《车牌识别程序及说明.docx(15页珍藏版)》请在冰豆网上搜索。
车牌识别程序及说明
数字图像处理综合实验报告
车牌识别技术(LPR)
1.图像灰度化及滤波
I=imread('yuan.bmp');
figure;
subplot(2,2,1);
imshow(I);
title('原图');
I1=rgb2gray(I);%图象灰度化
subplot(2,2,2);
imshow(I1);
title('灰度化图');
m2=medfilt2(I1,[5,5]);
subplot(2,2,3);
imshow(m2);
title('中值滤波');
w2=wiener2(I1,[5,5]);%维纳滤波
subplot(2,2,4);
imshow(w2);
title('维纳滤波');
2.图像二值化
fmax1=double(max(max(I1)));
fmin1=double(min(min(I1)));
level=(fmax1-(fmax1-fmin1)/3)/255;
bw2=im2bw(I1,level);
bw22=double(bw2);
figure,imshow(bw22),title('图像二值化');
3.边缘检测及开运算闭运算
lubo=edge(bw22,'log');
figure;
subplot(2,2,1);
imshow(bw2);title('Log算子边缘检测');%图象边缘检测
bg1=imclose(lubo,strel('rectangle',[1,33]));
subplot(2,2,2),imshow(bg1);title('图像闭运算[1,33]');
bg3=imopen(bg1,strel('rectangle',[1,33]));
subplot(2,2,3),imshow(bg3);title('图像开运算[1,33]');
bg2=imopen(bg3,strel('rectangle',[12,1]));
subplot(2,2,4),imshow(bg2);title('图像开[15,1]');
4.图像标记
[L,num]=bwlabel(bg2,8);%标注二进制图像中已连接的部分
%Feastats=imfeature(L,'basic');%计算图像区域的特征尺寸
%Area=[Feastats.Area];%区域面积
%BoundingBox=[Feastats.BoundingBox];%[xywidthheight]车牌的框架大小
%RGB=label2rgb(L,'spring','k','shuffle');%标志图像向RGB图像转换
figure,imshow(L);title('图像标记');%输出框架的彩色图像
5.形态滤波
I5=bwareaopen(L,1000);%去除聚团灰度值小于2000的部分
figure,imshow(I5),title('形态滤波后图像');
6.形态滤波车牌定位及字符分割
[y,x,z]=size(I5);
I6=double(I5);
Y1=zeros(y,1);
fori=1:
y
forj=1:
x
if(I6(i,j,1)==1)
Y1(i,1)=Y1(i,1)+1;
end
end
end
[tempMaxY]=max(Y1);
figure();
subplot(3,2,1),plot(0:
y-1,Y1),title('行方向像素点灰度值累计和'),xlabel('行值'),ylabel('像素');
PY1=MaxY;
while((Y1(PY1,1)>=50)&&(PY1>1))
PY1=PY1-1;
end
PY2=MaxY;
while((Y1(PY2,1)>=50)&&(PY2PY2=PY2+1;
end
IY=I(PY1:
PY2,:
:
);
X1=zeros(1,x);
forj=1:
x
fori=PY1:
PY2
if(I6(i,j,1)==1)
X1(1,j)=X1(1,j)+1;
end
end
end
subplot(3,2,2),plot(0:
x-1,X1),title('列方向像素点灰度值累计和'),xlabel('列值'),ylabel('像数');
PX1=1;
while((X1(1,PX1)<3)&&(PX1PX1=PX1+1;
end
PX2=x;
while((X1(1,PX2)<3)&&(PX2>PX1))
PX2=PX2-1;
end
PX1=PX1-1;
PX2=PX2+1;
%分割出车牌图像%
dw=I(PY1:
PY2,PX1:
PX2,:
);
subplot(3,2,3),imshow(dw),title('定位剪切后的彩色车牌图像')
ifisrgb(dw)
II1=rgb2gray(dw);%将RGB图像转化为灰度图像
elseII1=dw;
end
g_max=double(max(max(II1)));
g_min=double(min(min(II1)));
T=round(g_max-(g_max-g_min)/3);%T为二值化的阈值
[m,n]=size(II1);%d:
二值图像
%h=graythresh(I1);
II=im2bw(II1,T/256);
subplot(3,2,4);
imshow(II),title('二值化车牌图像');
I2=bwareaopen(II,20);
subplot(3,2,5);
imshow(I2),title('形态学滤波后的二值化图像');
[y1,x1,z1]=size(I2);
I3=double(I2);
TT=1;
%%%%%%%去除图像顶端和底端的不感兴趣区域%%%%%
Y1=zeros(y1,1);
fori=1:
y1
forj=1:
x1
if(I3(i,j,1)==1)
Y1(i,1)=Y1(i,1)+1;
end
end
end
Py1=1;
Py0=1;
while((Y1(Py0,1)<20)&&(Py0Py0=Py0+1;
end
Py1=Py0;
while((Y1(Py1,1)>=20)&&(Py1Py1=Py1+1;
end
I2=I2(Py0:
Py1,:
:
);
subplot(3,2,6);
imshow(I2),title('目标车牌区域');
%%%%%%分割字符按行积累量%%%%%%%
X1=zeros(1,x1);
forj=1:
x1
fori=1:
y1
if(I3(i,j,1)==1)
X1(1,j)=X1(1,j)+1;
end
end
end
figure;
plot(0:
x1-1,X1),title('列方向像素点灰度值累计和'),xlabel('列值'),ylabel('累计像素量');
Px0=1;
Px1=1;
%%%%%%%%%%%%分割字符%%%%%%%%%%%%%%%%%%
fori=1:
7
while((X1(1,Px0)<3)&&(Px0Px0=Px0+1;
end
Px1=Px0;
while(((X1(1,Px1)>=4)&&(Px1Px1=Px1+1;
end
Z=I2(:
Px0:
Px1,:
);
switchstrcat('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(8);
subplot(1,7,i);
imshow(Z);
Px0=Px1;
end
Px0=1;
Px1=1;
%%%%%%%%%%%%分割字符%%%%%%%%%%%%%%%%%%
fori=1:
7
while((X1(1,Px0)<3)&&(Px0Px0=Px0+1;
end
Px1=Px0;
while(((X1(1,Px1)>=4)&&(Px1Px1=Px1+1;
end
Z=I2(:
Px0:
Px1,:
);
switchstrcat('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(8);
subplot(1,7,i);
imshow(Z);
Px0=Px1;
end
附录:
子函数
functioninpt=pretreatment(dw)
%YUCHULISummaryofthisfunctiongoeshere
%Detailedexplanationgoeshere
ifisrgb(dw)
II1=rgb2gray(dw);
else
II1=dw;
end
II1=imresize(II1,[5020]);%将图片统一划为50*20大小
II1=im2bw(II1,0.9);
[m,n]=size(II1);
inpt=zeros(1,m*n);
%%%%%%将图像按列转换成一个行向量
forj=1:
n
fori=1:
m
inpt(1,m*(j-1)+i)=II1(i,j);
end
end