基于Matlab的车牌定位与分割 经典算法
I二imread('car.jpg');
%读取图像
figure。; subplot(3,2,1),imshow(l), title('原始图像');
I1=rgb2gray(l);%转化为灰度图像
subplot(3,2,2),imshow(I1),title('灰度图像');
I2=edge(I1,'robert',0.09,'both');%采用 robert 算子进行边缘检
测 subplot(3,2,3),imshow(I2),title('边缘检测后图像');
边绿检浪I启图像
se=[1;1;1]; %线型结构元素 I3=imerode(l2,se);
%腐蚀图像
subplot(3,2,4),imshow(l3),title('腐蚀后边缘图像');
se=strel('recta ngle',[25,25]);矩形结构元素 I4=imclose(l3,se);%图像聚类、填充图像 subplot(3,2,5),imshow(I4),title('填充后图像');
rx fYlHzeros(y3__6Hdoub_e(_5x o
rfony li-
5iM=zX eN(H_sM一W血、5Hbwa」「丿-Heaopen2 0(0一04
2subp_0f(32600S% 二卅
弗WH
if(l6(i,j,1)==1) Y 1(i,1)= Y 1(i,1)+1; end
end
end
[temp MaxY]=max(Y1); figure。;
subplot(3,2,1),plot(0:y-1,Y1),title('行方向像素点灰度值累计和'), xlabel('行值'),ylabel('像素');
行值
%求的车牌的行起始位置和终止位置 PY 仁Max Y;
while ((Y 1(PY1,1)>=50)&&(PY 1>1))
PY 1=P Y1-1;
end
PY 2=Max Y;
while ((Y 1(PY2,1)>=50)&&(PY2 end IY=I(P Y1:P Y2,:,:); X1= zeros(1,x); for j=1:x for i=PY1:PY2 if(l6(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('像数'); 歹U方向像素点衣度值黒计环CT O 1OC 0 2OD 400 歹UfS 600 800 %求的车牌的列起始位置和终止位置 while ((X1(1,PX1)<3)&&(PX1 end PX2=x; PX仁1; while ((X1(1,PX2)<3)&&(PX2>PX1)) PX2=PX2-1; end PX仁PX1-1; PX2=PX2+1; %分割出车牌图像% dw=l(P Y1:P Y2,PX1:PX2,:); subplot(3,2,3),imshow(dw),title('定位剪切后的彩色车牌图像') 定位剪切后 的耘 色车牌图像 4.2车牌字符分割 确定车牌位置后下一步的任务就是进行字符切分分离出车牌号 码的全部字符图像。 if isrgb(I) I1 = rgb2gray(I); %将RGB图像转化为灰度图像 else I1=I ; end g_max=double(max(max(l1))); g_mi n=double(mi n(min (11))); T=round(g_max-(g_max-g_min)/3); % T 为二值化的阈值 [m,n]=size(I1);% d:二值图像 %h=graythresh(I1); I1=im2bw(I1,T/256); subplot(3,2,4); imshow(l1),title('二值化车牌图像'); 二值化车牌图像 I2=bwareaope n( 11,20); subplot(3,2,5); imshow(I2),title('形态学滤波后的二值化图像'); 中值滤浪后的二恒化團像 [y1,x1,z1]=size(l2); I3=double(I2); TT=1; %%%%%%%去除图像顶端和底端的不感兴趣区域 %%%%% Y1=zeros(y1,1); for i=1:y1 for j=1:x1 if(I3(i,j,1)==1) Y 1(i,1)= Y 1(i,1)+1 end end Py 1=1; PyO=1; while ((Y 1(Py0,1)<20)&&(Py0 end Py1= Py0; while((Y1(Py1,1)>=20)&&(Py1 end l2=l2(PyO:Py1,:,:); subplot(3,2,6); imshow(l2),title('目标车牌区域'); 目标车牌区域 %分割字符按行积累量% X1= zeros(1,x1); for j=1:x1 for i=1:y1 if(l3(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; Px 1=1; %分割字符 for i=1:7 while ((X1(1,PxO)<3)&&(Px0 while (((X1(1,Px1)>=3)&&(Px1 Z=I2(:,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(3); subplot(1,7,i); imshow(Z); 匍M Ul n刀团斷 PxO=Px1; End fun cti on inpt = pretreatme nt(l) %YUCHULI Summary of this function goeshere % Detailed explanation goes here if isrgb(I) I1 = rgb2gray(l); else I1=I; end I仁imresize(l1,[50 20]);% 将图片统一划为I1=im2bw(I1,0.9); [m,n]=size(I1); in pt=zeros(1,m* n); %将图像按列转换成一个行向量 for j=1:n for i=1:m inpt(1,m*(j-1)+i)=I1(i,j); end end 50*20 大小 %神经网络进行识别。 close all; clear all; %%%%归一化训练样本 %%%%%% I0=pretreatment(imread('0.jpg')); I1=pretreatment(imread('1.jpg')); I2=pretreatment(imread('2.jpg')); I3=pretreatment(imread('3.jpg')); I4=pretreatment(imread('4.jpg')); I5=pretreatment(imread('5.jpg')); I6=pretreatment(imread('6.jpg')); I7=pretreatment(imread('7.jpg')); I8=pretreatment(imread('8.jpg')); I9=pretreatment(imread('9.jpg')); I10=pretreatment(imread('A.jpg')); I11=pretreatment(imread('C.jpg')); I12=pretreatment(imread('G.jpg')); I13=pretreatment(imread('L.jpg')); I14=pretreatment(imread('M.jpg')); I15=pretreatment(imread('R.jpg')); I16=pretreatment(imread('H.jpg')); I17=pretreatment(imread('N.jpg')); P=[I0',I1',I2',I3',I4',I5',I6',I7',I8',I9',I10',I11',I12',I13',I14',I15',I16',I1 7']; %输出样本 %%% T=eye(18,18); %%bp 神经网络参数设置 net=newff(minmax(P),[1000,32,18],{'logsig','logsig','logsig'},'trainrp '); net.inputWeights{1,1}.initFcn ='randnr'; net.layerWeights{2,1}.initFcn ='randnr'; net.trainparam.epochs=5000; net.trainparam.show=50; %net.trainparam.lr=0.003; net.trainparam.goal=0.0000000001; net=init(net); %%%训练样本 %%%% [net,tr]=train(net,P,T); %%%%%%%测试%%%%%%%%% %I=imread('DSC01323.jpg'); I=imread('DSC01344.jpg'); dw=location(I);% 车牌定位 [PIN0,PIN1,PIN2,PIN3,PIN4,PIN5,PIN6]=StringSplit(dw);% 处理 %%%%%%%%%%% 测试字符,得到识别数值 %%%% PIN0=pretreatment(PIN0); PIN1=pretreatment(PIN1); PIN2=pretreatment(PIN2); PIN3=pretreatment(PIN3); PIN4=pretreatment(PIN4); PIN5=pretreatment(PIN5); PIN6=pretreatment(PIN6); P0=[PIN0',PIN1',PIN2',PIN3',PIN4',PIN5',PIN6']; for i=2:7 T0= sim(net ,P0(:,i)); T1 = compet (T0) ; 字符分割及 d =find(T1 == 1) - 1 if (d==10) str='A'; elseif (d==11) str='C'; elseif (d==12) str='G'; elseif (d==13) str='L'; elseif (d==14) str='M'; elseif (d==15) str='R'; elseif (d==16) str='H'; elseif (d==17) str='N'; else str=num2str(d); end switch i case2 str1=str; case3 str2=str; case4 str3=str; case5 str4=str; case6 str5=str; otherwise str6=str; end end %显示定位后的分割出的车牌彩图, % $俞貝N7968 %%%%%%识别结果以标题形式显示在图上 s=strcat(渝',str1,str2,str3,str4,str5,str6); figure。; imshow(dw),title(s); %%% 非诚勿扰欢迎下载 因篇幅问题不能全部显示,请点此查看更多更全内容
Copyright © 2019- hzar.cn 版权所有 赣ICP备2024042791号-5
违法及侵权请联系:TEL:199 1889 7713 E-MAIL:2724546146@qq.com
本站由北京市万商天勤律师事务所王兴未律师提供法律服务