matlab 车牌识别 程序

%车牌识别
%[fn,pn,fi]=uigetfile('benchi.jpg','选择图片');%读入图片
%I=imread([pn fn]);
close all;
clc;
I=imread('benchi.jpg');
figure(1),imshow(I);
title('原始车牌图像');


IGray=rgb2gray(I);
figure(2),
subplot(1,2,1),
imshow(IGray),
title('灰度图像');
subplot(1,2,2),
imhist(IGray),
title('灰度直方图');


Itiao=imadjust(IGray,[0.19,0.78],[0,1]);%调整图片
figure(3),
subplot(1,2,1),
imshow(Itiao),
title('增强灰度图像');
subplot(1,2,2),
imhist(Itiao),
title('增强灰度直方图');


Iedge=edge(IGray,'sobel',0.15,'both');
figure(4),imshow(Iedge);
title('sobel算子实现边缘检测');


se=[1;1;1];
Ierode=imerode(Iedge,se);%图像腐蚀
figure(5),imshow(Ierode);
title('腐蚀效果图');
se=strel('rectangle',[25,25]);%创建有指定形状的结构元素


Iclose=imclose(Ierode,se);
figure(6),imshow(Iclose);
title('平滑图像');


Iopen=bwareaopen(Iclose,2000);%删除小面积图形
figure(7),imshow(Iopen);
title('移除小对象');


[y,x,z]=size(Iopen);
Im6=double(Iopen);
Blue_y=zeros(y,1);
for i=1:y
    for j=1:x
        if(Im6(i,j,1)==1)
            Blue_y(i,1)=Blue_y(i,1)+1;
        end
    end
end
[temp MaxY]=max(Blue_y);%垂直方向车牌区域确定
PY1=MaxY;
while((Blue_y(PY1,1)>=5)&&(PY1>1))
    PY1=PY1-1;
end
PY2=MaxY;
while((Blue_y(PY2,1)>=5)&&(PY2<y))
    PY2=PY2+1;
end
IY=I(PY1:PY2,:,:);
Blue_x=zeros(1,x);
for j=1:x
    for i=PY1:PY2
        if(Im6(i,j,1)==1)
            Blue_x(1,j)=Blue_x(1,j)+1;
        end
    end
end
PX1=1;
while((Blue_x(1,PX1)<3)&&(PX1<x))
    PX1=PX1+1;
end
PX2=x;
while((Blue_x(1,PX2)<3)&&(PX2>PX1))
    PX2=PX2-1;
end
PX1=PX1-1;
PX2=PX2+1;
dw=I(PY1:PY2-6,PX1:PX2,:);
figure(8),
subplot(1,2,1),
imshow(IY),
title('垂直方向合理区域');
subplot(1,2,2),
imshow(dw),
title('定位剪切后的图像车牌');


imwrite(dw,'dw.jpg');
a=imread('dw.jpg');
b=rgb2gray(a);
imwrite(b,'车牌灰度图像.jpg');
figure(9),
subplot(3,2,1),
imshow(b),
title('车牌灰度图像');
g_max=double(max(max(b)));
g_min=double(min(min(b)));
T=round(g_max-(g_max-g_min)/3);%T为二值化阈值
[m,n]=size(b);
d=(double(b)>=T);%d为二值化图像
imwrite(d,'车牌二值化图像.jpg');
figure(9),
subplot(3,2,2),
imshow(d),
title('车牌二值化图像');
figure(9),
subplot(3,2,3),
imshow(d),
title('均值滤波前图像');
h=fspecial('average',3);
d=im2bw(round(filter2(h,d)));
imwrite(d,'均值滤波后图像.jpg');
figure(9),
subplot(3,2,4),
imshow(d),
title('均值滤波后图像');
se=eye(2);
[m,n]=size(d);
if bwarea(d)/m/n>=0.365
    d=imerode(d,se);
elseif bwarea(d)/m/n<=0.235
    d=imdilate(d,se);
end
imwrite(d,'膨胀或俯视后图像.jpg');
figure(9),
subplot(3,2,5),
imshow(d),
title('膨胀或俯视后图像');


d=qiege(d);
[m,n]=size(d);
k1=1;k2=1;s=sum(d);
j=1;
while j~=n
    while s(j)==0
        j=j+1;
    end
    k1=j;
    while s(j)~=0&&j<=n-1
        j=j+1;
    end
    k2=j-1;
    if k2-k1>=round(n/6.5)
        [val,num]=min(sum(d(:,[k1+5:k2-5])));
        d(:,k1+num+5)=0;
    end
end


d=qiege(d);
y1=10;y2=0.25;flag=0;word1=[];
while flag==0
    [m,n]=size(d);
    left=1;wide=0;
    while sum(d(:,wide+1))~=0
        wide=wide+1;
    end
    if wide<y1
        d(:,[1:wide])=0;
        d=qiege(d);
    else
        temp=qiege(imcrop(d,[1 1 wide m]));
        [m,n]=size(temp);
        all=sum(sum(temp));
        two_thirds=sum(sum(temp([round(m/3):2*round(m/3)],:)));
        if two_thirds/all>y2
            flag=1;word1=temp;
        end
        d(:,[1:wide])=0;d=qiege(d);
    end
end
[word2,d]=getword(d);
[word3,d]=getword(d);
[word4,d]=getword(d);
[word5,d]=getword(d);
[word6,d]=getword(d);
[word7,d]=getword(d);
word1=imresize(word1,[40 20]);
word2=imresize(word2,[40 20]);
word3=imresize(word3,[40 20]);
word4=imresize(word4,[40 20]);
word5=imresize(word5,[40 20]);
word6=imresize(word6,[40 20]);
word7=imresize(word7,[40 20]);
figure,subplot(2,7,1),
imshow(word1),
title('1');
subplot(2,7,2),
imshow(word2),
title('2');
subplot(2,7,3),
imshow(word3),
title('3');
subplot(2,7,4),
imshow(word4),
title('4');
subplot(2,7,5),
imshow(word5),
title('5');
subplot(2,7,6),
imshow(word6),
title('6');
subplot(2,7,7),
imshow(word7),
title('7');
imwrite(word1,'1.bmp');
imwrite(word2,'2.bmp');
imwrite(word3,'3.bmp');
imwrite(word4,'4.bmp');
imwrite(word5,'5.bmp');
imwrite(word6,'6.bmp');
imwrite(word7,'7.bmp');


liccode=char(['0':'9' 'A':'Z' '京辽苏鲁浙陕川藏黑吉豫甘冀湘新渝粤云宁闽蒙青琼皖贵赣桂沪晋鄂津']);
L=1;
for LL=1:7
    ii=int2str(LL);
    t=imread([ii,'.bmp']);
    SegBw2=imresize(t,[40 20],'nearest');
    if L==1
        kmin=37;
        kmax=67;
    elseif L>=2&&L<=3
        kmin=11;
        kmax=36;
    elseif L>=4&&L<=7
        kmin=1;
        kmax=10;
    end
    for k2=kmin:kmax
        fname=strcat('字符模板\',liccode(k2),'.bmp');
        SamBW2=imread(fname);
        Dm=0;
        for k1=1:40
            for pp=1:20
                if SegBw2(k1,pp)==SamBW2(k1,pp)
                    Dm=Dm+1;
                end
            end
        end
        Error(k2)=Dm;
    end
    Error1=Error(kmin:kmax);
    MinError=max(Error1);
    findc=find(Error1==MinError);
    Resault(L*2-1)=liccode(findc(1)+kmin-1);
    %Resault(L*2)='';
    L=L+1;
end
%t=toc
Resault
msgbox(Resault,'识别结果')
fid=fopen('Data.xls','a+');
fprint(fid,'%s\r\n',Resault,datestr(now));
flose(fid);

        


function [word,result]=getword(d)  
word=[];flag=0;y1=8;y2=0.5;  
    while flag==0  
        [m,n]=size(d);  
        wide=0;  
        while sum(d(:,wide+1))~=0 && wide<=n-2  
            wide=wide+1;  
        end  
        temp=qiege(imcrop(d,[1 1 wide m]));  
        [m1,n1]=size(temp);  
        if wide<y1 && n1/m1>y2  
            d(:,[1:wide])=0;  
            if sum(sum(d))~=0  
                d=qiege(d);  % 切割出最小范围  
            else word=[];flag=1;  
            end  
        else  
            word=qiege(imcrop(d,[1 1 wide m]));  
            d(:,[1:wide])=0;  
            if sum(sum(d))~=0;  
                d=qiege(d);flag=1;  
            else d=[];  
            end  
        end  
    end  
result=d;  


function e=qiege(d)
[m,n]=size(d);
top=1;bottom=m;left=1;right=n;   % init
while sum(d(top,:))==0 && top<=m
    top=top+1;
end
while sum(d(bottom,:))==0 && bottom>=1
    bottom=bottom-1;
end
while sum(d(:,left))==0 && left<=n
    left=left+1;
end
while sum(d(:,right))==0 && right>=1
    right=right-1;
end
dd=right-left;
hh=bottom-top;
e=imcrop(d,[left top dd hh]);

        







  • 7
    点赞
  • 77
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值