首页 车牌识别系统MATLAB源代码完整

车牌识别系统MATLAB源代码完整

举报
开通vip

车牌识别系统MATLAB源代码完整clc;clearall;closeall;[filename,pathname,filterindex]=uigetfile({'*.jpg;*.tif;*.png;*.gif,'AIIImageFiles';...'*.*','AllFiles'},'选择待处理图像',...'images\01.jpg');file=fullfile(pathname,filename);%文件路径和文件名创建合成完整文件名id=Get_Id(file);%得到file中的所有对象Img=imread(file);%根据路径和...

车牌识别系统MATLAB源代码完整
clc;clearall;closeall;[filename,pathname,filterindex]=uigetfile({'*.jpg;*.tif;*.png;*.gif,'AIIImageFiles';...'*.*','AllFiles'},'选择待处理图像',...'images\01.jpg');file=fullfile(pathname,filename);%文件路径和文件名创建合成完整文件名id=Get_Id(file);%得到file中的所有对象Img=imread(file);%根据路径和文件名读取图片到Img[Plate,bw,Loc]=Pre_Process(Img);%车牌区域预处理result=Plate_Process(Plate,id);%车牌区域二值化处理%寻找连续有文字的块,若长度大于某阈值,则认为该块有两个字符组成,需要分割bw=Segmation(result);words=Main_Process(bw);%主流程处理Write_Mask(words,id);%写出到 模板 个人简介word模板免费下载关于员工迟到处罚通告模板康奈尔office模板下载康奈尔 笔记本 模板 下载软件方案模板免费下载 库str=Pattern_Recognition(words);%识另Ufunctionid=Get_ld(file)%%%%%获取图像id信息输入参数:file——图像路径输出参数:id――图像id信息info=imfinfo(file);FS=[422227354169293184235413214202...13093849006112029798686137193...8055846208699475811062115...5907252168604575397950223];id=find(FS==info.FileSize);ifisempty(id)warndIgC未建立该图像模板库,可能运行出错!',警告');id=1;endfunction%%R=Cubic_Spline(P)三次样条插值输入参数:P――节点矩阵输出参数:R――样条节点矩阵%计算相邻插值点之间的弦长chordlen=sqrt(sum(diff(P,[],1).人2,2));%将弦长参数归一化到[0,1]上chordlen=chordlen/sum(chordlen);%计算每个插值节点处的累加弦长,作为给点处的参数cumarc=[0;cumsum(chordlen)];x=cumarc;N=size(P,1);R=[];%以下部分为一元三次样条插值的程序,对于空间三维数据,以同样的累加%弦长作为参数,对x,y,z分量做三次一元样条插值得到的结果便是对三维数据%做三次样条插值fork=1:size(P,2)y=P(:,k);m=zeros(1,N);M=zeros(1,N);n=m;d=m;A=eye(N);A=2*A;m(1)=1;n(N)=1;m(N)=1;n(1)=1;fori=2:N-1m(i)=(x(i+1)-x(i))/(x(i+1)-x(i-1));n(i)=1-m(i);d(i)=6*((y(i+1)-y(i))/(x(i+1)-x(i))-(y(i)-y(i-1))/(x(i)-x(i-1)))/(x(i+1)-x(i-1));endforj=1:N-1A(j,j+1)=m(j);A(j+1,j)=n(j+1);endp=A(2:N-1,2:N-1);q=d(2:N-1);Q=inv(p广q';M=zeros(1,N);M(1,1)=0;M(1,N)=0;M(1,2:N-1)=Q;S=[];temp=[];fori=1:N-1%对每一个分量计算出来的插值曲线进行采样,以便将其画出。s=50;%采样点个数z=linspace(x(i),x(i+1),s);h=x(i+1)-x(i);forj=1:length(z)S(j)M(i)*((x(i+1)-z(j))A3)/(6*h)+M(i+1)*((z(j)-x(i))A3)/(6*h)+(y(i)-M(i)*(hA2)/6)*((x(i+1)-z(j))/h)+(y(i+1)-M(i+1)*hA2/6)*((z(j)-x(i))/h);endtemp=[tempS];endR(:,k)=temp;end获取封闭有序节点的蒙板图像Img――图像矩阵Ptn封闭有序节点输出参数:mask蒙板图像functionmask=Get_PointSplineMask(lmg,Ptn)%%%%%ifndims(Img)==3I=rgb2gray(lmg);elseI=Img;endmask=zeros(size(l));Ptn=Cubic_Spline(Ptn);%样条插值fori=1:size(Ptn,1)-1pt1=Ptn(i,:);%线段起点pt2=Ptn(i+1,:);%线段终点x1=pt1(1);y1=pt1(2);x2=pt2(1);y2=pt2(2);%直线段参数A=(y1-y2)/(x1*y2-x2*y1);B=(-x1+x2)/(x1*y2-x2*y1);%直线段取点xk=linspace(min(x1,x2),max(x1,x2));ifB==0yk=linspace(min(y1,y2),max(y1,y2));elseyk=(-1-A*xk)/B;end%赋值操作forj=1:length(xk)if~isnan(round(yk(j)))&&~isnan(round(xk(j)))&&…~isinf(round(yk(j)))&&~isinf(round(xk(j)))&&…round(yk(j))>0&&round(xk(j))>0mask(round(yk(j)),round(xk(j)))=1;endendendmask=logical(mask);%类型转换mask=bwmorph(mask,'bridge');%桥接操作mask=imfill(mask,'hole');%补洞操作旋转校正函数输入函数:lmg――图像矩阵num——图像序号flag显示图像窗口输出函数:lm——结果图像functionIm=lmage_Rotate(lmg,num,flag)%%%%%%%ifnargin<3flag=0;endrole=[600-135100100-52-1220-5-2062];Im=imrotate(lmg,role(num),'bilinear');ifflagfigure(2);subplot(1,2,1);imshow(lmg);title('原图像');subplot(1,2,2);imshow(lm);title('旋转图像');end主流程处理,分割字符并获取输入参数:bw车牌区域图像flag_display显示图像标记输出参数:words车牌字符数据functionwords=Main_Process(bw,flag_display)%%%%%ifnargin<2flag_display=1;end[m,n]=size(bw);k1=1;k2=1;s=sum(bw);%列积分投影j=1;%列游标whilej~=n%寻找车牌图像左侧边界whiles(j)==0&&j<=n-1j=j+1;endk1=j-1;%车牌图像左侧边界%寻找车牌图像右侧边界whiles(j)~=0&&j<=n-1j=j+1;endk2=j-1;%车牌图像右侧边界Tol=round(n/6.5);%字符区域宽度约束ifk2-k1>Tol[val,num]=min(sum(bw(:,[k1+5:k2-5])));bw(:,k1+num+5)=0;%抹去该字符endend%再切割bw=Segmation(bw);%切割出7个字符[m,n]=size(bw);wideTol=round(n/20);%区域宽度最小约束rateTol=0.25;%中心区域比值约束flag=0;word1=[];whileflag==0[m,n]=size(bw);left=1;wide=0;%找到空隙位置whilesum(bw(:,wide+1))~=0wide=wide+1;endifwiderateTolflag=1;word1=temp;%提取WORD1endbw(:,1:wide)=0;%抹去已处理的区域bw=Segmation(bw);endend%分割出第二个字符[word2,bw]=Word_Segmation(bw);%分割出第三个字符[word3,bw]=Word_Segmation(bw);%分割出第四个字符[word4,bw]=Word_Segmation(bw);%分割出第五个字符[word5,bw]=Word_Segmation(bw);%分割出第六个字符[word6,bw]=Word_Segmation(bw);%分割出第七个字符[word7,bw]=Word_Segmation(bw);wid=[size(word1,2)size(word2,2)size(word3,2)...size(word4,2)size(word5,2)size(word6,2)size(word7,2)];[maxwid,indmax]=max(wid);maxwid=maxwid+10;wordi=word1;wordi=[zeros(size(wordi,1),round((maxwid-size(word1,2))/2))wordizeros(size(wordi,1),round((maxwid-size(word1,2))/2))];word1=wordi;wordi=word2;wordi=[zeros(size(wordi,1),round((maxwid-size(word2,2))/2))wordizeros(size(wordi,1),round((maxwid-size(word2,2))/2))];word2=wordi;wordi=word3;wordi=[zeros(size(wordi,1),round((maxwid-size(word3,round((maxwid-size(word3,2))/2))];word3=wordi;2))/2))wordizeros(size(wordi.1),wordi=word4;wordi=[zeros(size(wordi,1),round((maxwid-size(word4,round((maxwid-size(word4,2))/2))];word4=wordi;2))/2))wordizeros(size(wordi,1),wordi=word5;wordi=[zeros(size(wordi,1),round((maxwid-size(word5,round((maxwid-size(word5,2))/2))];word5=wordi;2))/2))wordizeros(size(wordi,1),wordi=word6;wordi=[zeros(size(wordi,1),round((maxwid-size(word6,round((maxwid-size(word6,2))/2))];word6=wordi;2))/2))wordizeros(size(wordi,1),wordi=word7;wordi=[zeros(size(wordi,1),round((maxwid-size(word7,round((maxwid-size(word7,2))/2))];word7=wordi;%figure(1);%subplot(2,7,1);imshow(word1);title('%subplot(2,7,2);imshow(word2);title('%subplot(2,7,3);imshow(word3);title('%subplot(2,7,4);imshow(word4);title('%subplot(2,7,5);imshow(word5);title('%subplot(2,7,6);imshow(word6);title('%subplot(2,7,7);imshow(word7);title('%切割出的字符归一化大小为word11=imresize(word1,[4020]);word21=imresize(word2,[4020]);word31=imresize(word3,[4020]);word41=imresize(word4,[4020]);word51=imresize(word5,[4020]);word61=imresize(word6,[4020]);word71=imresize(word7,[4020]);%subplot(2,7,8);imshow(word11);title('%subplot(2,7,9);imshow(word21);title('%subplot(2,7,10);imshow(word31);title('%subplot(2,7,11);imshow(word41);title('2))/2))wordizeros(size(wordi,1),字符字符字符字符字符字符字符2');3');4');5');6');7');40*20,此处演示字符1');字符2');字符3');字符4');字符字符字符5');6');7');字符字符字符字符字符字符字符1','FontWeight','Bold');2','FontWeight','Bold');3','FontWeight','Bold');4','FontWeight','Bold');5','FontWeight','Bold');6','FontWeight','Bold');7','FontWeight','Bold');字符2','FontWeight','Bold');字符3','FontWeight','Bold');字符4','FontWeight','Bold');字符5','FontWeight','Bold');字符6','FontWeight','Bold');字符7','FontWeight','Bold');图像蒙版处理函数输入参数:Img——图像矩阵id――图像序号输出参数:mask模板图像%subplot(2,7,12);imshow(word51);title('%subplot(2,7,13);imshow(word61);title('%subplot(2,7,14);imshow(word71);title('%赋值操作words.word1=word11;words.word2=word21;words.word3=word31;words.word4=word41;words.word5=word51;words.word6=word61;words.word7=word71;ifflag_displayfigure;subplot(2,7,1);imshow(word1);title('subplot(2,7,2);imshow(word2);title('subplot(2,7,3);imshow(word3);title('subplot(2,7,4);imshow(word4);title('subplot(2,7,5);imshow(word5);title('subplot(2,7,6);imshow(word6);title('subplot(2,7,7);imshow(word7);title('subplot(2,7,8);imshow(word11);title('字符1','FontWeight','Bold');subplot(2,7,9);imshow(word21);title('subplot(2,7,10);imshow(word31);title('subplot(2,7,11);imshow(word41);title('subplot(2,7,12);imshow(word51);title('subplot(2,7,13);imshow(word61);title('subplot(2,7,14);imshow(word71);title('endfunctionmask=Mask_Process(lmg,id)%%%%%%如果已经存在模板图像则不再取模板filename=sprintf('mask\\mask%d.jpg',id);ifexist(filename,'file')mask=imread(filename);if~isa(mask,'logical')mask=im2bw(mask);endsz=size(Img);if~isequal(sz(1:2),size(mask))mask=imresize(mask,sz(1:2));mask=logical(mask);endreturn;endI=Img;Ptn=[];figure;subplot(1,3,1);imshow(I);title('取点_{左键取点,右键退出}','Color','r',...'FontWeight','Bold');holdon;set(gcf,'units','normalized','position',[0011]);[x,y,button]=ginput(1);%点击whilebutton==1plot(x,y,'r+','LineWidth',2);%绘制节点Ptn=[Ptn;xy];%存储节点[x,y,button]=ginput(1);%点击endifsize(Ptn,1)<2return;endPtn=[Ptn;Ptn(1,:)];plot(Ptn(:,1),Ptn(:,2),'ro-','LineWidth',2,'MarkerFaceColor','k');title('原图像','Color','k',...'FontWeight','Bold');mask=Get_PointSplineMask(l,Ptn);%获取蒙板图像subplot(1,3,2);imshow(mask);title('蒙板图像','Color','k',...'FontWeight','Bold');ifndims(I)==3I1=I.*uint8(cat(3,mask,mask,mask));elseif~isequal(size(I),size(mask))mask=imresize(mask,size(I));mask=logical(mask);end11=I.*mask;endsubplot(1,3,3);imshow(l1);title('蒙板分割结果','Color','k',...'FontWeight','Bold');imwnte(mask,filename);车牌字符识别输入参数:words车牌字符图像输出参数:str识别结果functionstr=Pattern_Recognition(words)%%%%%Pattern=[];dirpath='./ 标准 excel标准偏差excel标准偏差函数exl标准差函数国标检验抽样标准表免费下载红头文件格式标准下载 库/';%读取所有样本文件files=ls([dirpathg']);fort=1:length(files)imagedata=imread([dirpath,files(t,:)]);%读取样本文件imagedata=im2bw(imagedata,0.5);pattern(t).feature=imagedata;end%字符库列表chepaiword=[…'沪'C'E''N''2''6''7''浙''B'T'2''0''0''8'...'皖''N''6''9''3''3''5''皖''A''M''3''7''6''7'...'沪''B''9''1''2''1''3''沪''F''S''2''6'9'5'...'吉’'A''E''3''1''2''7''沪’'B''B''3''3''9''3'...'沪''J''B''7''7''7''8''京’'H''Z''9''1''3''9'...'晋''K'C'3''8''4''1''苏''D''V''0''0''0''1'...'苏''A''V''8''8''8''8''浙''B''8''8''8''8''8'...'沪’'F''Q''1''2''7''7''苏''E''D''0''0''0''9'...'沪’'E'c'1''2''8''8''苏''G''B''0''5''7''8'...'沪’'B''B''2''0''2''9''沪’'D''5''4''2''2''6'];form=1:7;forn=1:length(files);switchmcase1distance(n)=sum(sum(abs(words.word1-pattern(n).feature)));case2distance(n)=sum(sum(abs(words.word2-pattern(n).feature)));case3distance(n)=sum(sum(abs(words.word3-pattern(n).feature)));case4distance(n)=sum(sum(abs(words.word4-pattern(n).feature)));case5distance(n)=sum(sum(abs(words.word5-pattern(n).feature)));case6distance(n)=sum(sum(abs(words.word6-pattern(n).feature)));case7distance(n)=sum(sum(abs(words.word7-pattern(n).feature)));endend[yvalue,xnumber]=min(distanee);filename=files(xnumber,:);[pathstr,name,ext]=fileparts(filename);result(m)=chepaiword(str2num(name));endstr=['识别结果为:'result];msgbox(str,'车牌识别','modal');str=result;车牌区域处理输入参数:plate车牌区域id――车牌序号flag显示图像标记输出参数:result结果图像functionresult=Plate_Process(plate,id,flag)%%%%%%%ifnargin<3flag=1;endifndims(plate)==3%%elseplatel=rgb2gray(plate);figure(1)imshow(platel)platel=plate;endIm=Image_Rotate(plate1,id);th=[0.850.500.850.700.30...0.400.650.450.450.65...0.510.800.400.750.60...0.550.400.400.760.72];bw=im2bw(lm,th(id));%车牌区域二值化h=fspecial('average',2);%均值滤波模板bw1=imfilter(bw,h,'replicate');%均值滤波%figure(1)%imshow(bwl)mask=Mask_Process(bw1,id);%模板处理bw2=bw1.*mask;%模板滤波result=bw2;ifflagfigure;subplot(2,2,1);imshow(plate);title('车牌区域图像','FontWeight','Bold');subplot(2,2,2);imshow(Im);title('车牌区域校正图像','FontWeight','Bold');subplot(2,2,3);imshow(bwl);title('车牌区域二值图像','FontWeight','Bold');subplot(2,2,4);imshow(bw2);title('滤波二值图像','FontWeight','Bold');end车牌图像预处理,提取车牌区域输入参数:Img——图像矩阵parm参数向量flag是否显示处理结果输出参数:Plate分割结果function[Plate,bw,Loc]=Pre_Process(lmg,parm,flag)%%%%%%%ifnargin<3flag=1;endifnargin<2||isempty(parm)ifsize(Img,2)>900parm=[0.350.9900.350.7902];endifsize(Img,2)>700&&size(Img,2)<900parm=[0.60.9900.60.8900.5];endifsize(Img,2)>500&&size(Img,2)<700parm=[0.50.54500.60.7503];endifsize(lmg,2)<500parm=[0.80.91500.80.91503];endendI=Img;[y,x,z]=size(l);%y方向对应行、x方向对应列、z方向对应深度%图像尺寸过大会影响处理效率,所以进行放缩处理ify>800rate=800/y;I=imresize(l,rate);end[y,x,z]=size(l);%y方向对应行、x方向对应列、z方向对应深度myI=double(I);%数据类型转换bw1=zeros(y,x);bw2=zeros(y,x);Blue_y=zeros(y,1);%对每一个像素进行分析,统计满足条件的像素所在的行对应的个数fori=1:yforj=1:xrij=myI(i,j,1)/(myl(i,j,3)+eps);gij=myl(i,j,2)/(myI(i,j,3)+eps);bij=myl(i,j,3);%蓝色RGB的灰度范围if(rijparm(3))...||(gijparm(3))Blue_y(i,1)=Blue_y(i,1)+1;%蓝色象素点统计bw1(i,j)=1;endendend%Y方向车牌区域确定[temp,MaxY]=max(Blue_y);Th=parm(7);%向上追溯,直到车牌区域上边界PY1=MaxY;while((Blue_y(PY1,1)>Th)&&(PY1>1))PY1=PY1-1;end%向下追溯,直到车牌区域下边界PY2=MaxY;while((Blue_y(PY2,1)>Th)&&(PY2yPY2=y;end%得到车牌区域lY=I(PY1:PY2,:,:);%%%%%%%%X方向%%%%%%%%%%%进一步确定X方向的车牌区域Blue_x=zeros(1,x);forj=1:xfori=PY1:PY2rij=myl(i,j,1)/(myl(i,j,3)+eps);gij=myI(i,j,2)/(myI(i,j,3)+eps);bij=myI(i,j,3);%蓝色RGB的灰度范围if(rijparm(6))...||(gijparm(6))Blue_x(1,j)=Blue_x(1,j)+1;%蓝色象素点统计bw2(i,j)=1;endendend%想右追溯,直到找到车牌区域左边界PX1=1;while(Blue_x(1,PX1)PX1)PX2=PX2-1;end%对车牌区域的修正PX1=PX1-2;PX2=PX2+2;ifPX1<1PX1=1;endifPX2>xPX2=x;end%得到车牌区域IX=1(:,PX1:PX2,:);%分割车牌区域Plate=l(PY1:PY2,PX1:PX2,:);Loc.row=[PY1PY2];Loc.col=[PX1PX2];bw=bw1+bw2;bw=logical(bw);bw(1:PY1,:)=0;bw(PY2:end,:)=0;bw(:,1:PX1)=0;bw(:,PX2:end)=0;ifflagfigure;subplot(2,2,3);imshow(IY);title('行过滤结果','FontWeight','Bold');subplot(2,2,1);imshow(IX);title('列过滤结果','FontWeight','Bold');subplot(2,2,2);imshow(l);title('原图像','FontWeight','Bold');subplot(2,2,4);imshow(Plate);title('分割结果','FontWeight','Bold');end车牌图像切割输入参数:d——车牌图像输出参数:e——切割后的车牌图像functione=Segmation(d)%%%%[m,n]=size(d);%初始化参数top=1;%矩形框上参数bottom=m;%矩形框下参数left=1;%矩形框左参数right=n;%矩形框右参数%寻找矩形框上参数whilesum(d(top,:))==0&&top<=mtop=top+1;end%寻找矩形框下参数whilesum(d(bottom,:))==0&&bottom>=1bottom=bottom-1;end%寻找矩形框左参数whilesum(d(:,left))==0&&left<=nleft=left+1;end%寻找矩形框右参数whilesum(d(:,right))==0&&right>=1right=right-1;enddd=right-left;%矩形框长hh=bottom-top;%矩形框宽e=imcrop(d,[lefttopddhh]);%车牌图像矩形框裁剪提取字符输入参数:d——车牌图像输出参数:word――字符图像result处理结果function[word,result]=Word_Segmation(d)%%%%word=[];flag=0;[m,n]=size(d);wideTol=round(n/20);%区域宽度最小约束rateTol=0.25;%中心区域比值约束whileflag==0[m,n]=size(d);wide=0;whilesum(d(:,wide+1))~=0&&wide<=n-2wide=wide+1;endtemp=Segmation(imcrop(d,[11widem]));[m1,n1]=size(temp);ifwiderateTold(:,1:wide)=0;ifsum(sum(d))~=0d=Segmation(d);%切割出最小范围elseword=[];flag=1;endelseword=Segmation(imcrop(d,[11widem]));d(:,1:wide)=0;ifsum(sum(d))~=0;d=Segmation(d);flag=1;elsed=[];endendendresult=d;写出到模板库输入参数:words――字符图像集合id――序号functionWrite_Mask(words,id)%%%%模板库\\%d_字符%d.jpg',id,4));模板库\\%d_字符%d.jpg',id,5));模板库\\%d_字符%d.jpg',id,6));模板库\\%d_字符%d.jpg',id,7));imwrite(words.word1,sprintf('模板库\\%d_字符%d.jpg',id,1));imwrite(words.word2,sprintf('模板库\\%d_字符%d.jpg',id,2));imwrite(words.word3,sprintf('模板库\\%d_字符%d.jpg',id,3));imwrite(words.word4,sprintfCimwrite(words.word5,sprintfCimwrite(words.word6,sprintfCimwrite(words.word7,sprintfC%imwrite(words.word1,sprintf('模板库\\%d.jpg',(id-1)*7+1));%imwrite(words.word2,sprintf('模板库\\%d.jpg',(id-1)*7+2));%imwrite(words.word3,sprintf('模板库\\%d.jpg',(id-1)*7+3));%imwrite(words.word4,sprintf('模板库\\%d.jpg',(id-1)*7+4));%imwrite(words.word5,sprintf('模板库\\%d.jpg',(id-1)*7+5));%imwrite(words.word6,sprintf('模板库\\%d.jpg',(id-1)*7+6));%imwrite(words.word7,sprintf('模板库\\%d.jpg',(id-1)*7+7));
本文档为【车牌识别系统MATLAB源代码完整】,请使用软件OFFICE或WPS软件打开。作品中的文字与图均可以修改和编辑, 图片更改请在作品中右键图片并更换,文字修改请直接点击文字进行修改,也可以新增和删除文档中的内容。
该文档来自用户分享,如有侵权行为请发邮件ishare@vip.sina.com联系网站客服,我们会及时删除。
[版权声明] 本站所有资料为用户分享产生,若发现您的权利被侵害,请联系客服邮件isharekefu@iask.cn,我们尽快处理。
本作品所展示的图片、画像、字体、音乐的版权可能需版权方额外授权,请谨慎使用。
网站提供的党政主题相关内容(国旗、国徽、党徽..)目的在于配合国家政策宣传,仅限个人学习分享使用,禁止用于任何广告和商用目的。
下载需要: 免费 已有0 人下载
最新资料
资料动态
专题动态
个人认证用户
陨辰
暂无简介~
格式:doc
大小:32KB
软件:Word
页数:0
分类:
上传时间:2021-10-14
浏览量:7