本文共 5159 字,大约阅读时间需要 17 分钟。
%车牌识别 %[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]);转载地址:http://xklpi.baihongyu.com/