高速公路车牌识别系统设计及MATLAB仿真 附录
imwrite(dw,'dw.jpg');
[filename,filepath]=uigetfile('dw.jpg','输入一个定位裁剪后的车牌图像'); jpg=strcat(filepath,filename); a=imread('dw.jpg'); b=rgb2gray(a);
imwrite(b,'1.车牌灰度图像.jpg');
figure(8);subplot(3,2,1),imshow(b),title('1.车牌灰度图像') g_max=double(max(max(b))); g_min=double(min(min(b)));
T=round(g_max-(g_max-g_min)/2); % T为二值化的阈值 [m,n]=size(b);
d=(double(b)>=T); % d:二值图像 imwrite(d,'2.车牌二值图像.jpg');
figure(8);subplot(3,2,2),imshow(d),title('2.车牌二值图像') figure(8),subplot(3,2,3),imshow(d),title('3.均值滤波前') % 滤波
h=fspecial('average',3); d=im2bw(round(filter2(h,d))); imwrite(d,'4.均值滤波后.jpg');
figure(8),subplot(3,2,4),imshow(d),title('4.均值滤波后')
% 某些图像进行操作 % 膨胀或腐蚀
% se=strel('square',3); % 使用一个3X3的正方形结果元素对象对创建的图像进行膨胀
% 'line'/'diamond'/'ball'...
se=eye(2); % eye(n) returns the n-by-n identity matrix 单位矩阵 [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,'5.膨胀或腐蚀处理后.jpg');
figure(8),subplot(3,2,5),imshow(d),title('5.膨胀或腐蚀处理后')
25
高速公路车牌识别系统设计及MATLAB仿真 附录
% 寻找连续有文字的块,若长度大于某阈值,则认为该块有两个字符组成,需要分割 d=qiege(d); [m,n]=size(d);
figure,subplot(2,1,1),imshow(d),title(n) 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);
% 切割出 7 个字符
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 temp=qiege(imcrop(d,[1 1 wide m])); 26 高速公路车牌识别系统设计及MATLAB仿真 附录 [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; % WORD 1 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); subplot(5,7,1),imshow(word1),title('1'); subplot(5,7,2),imshow(word2),title('2'); subplot(5,7,3),imshow(word3),title('3'); subplot(5,7,4),imshow(word4),title('4'); subplot(5,7,5),imshow(word5),title('5'); subplot(5,7,6),imshow(word6),title('6'); subplot(5,7,7),imshow(word7),title('7'); [m,n]=size(word1); % 商用系统程序中归一化大小为 40*20,此处演示 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]); 27 高速公路车牌识别系统设计及MATLAB仿真 附录 word6=imresize(word6,[40 20]); word7=imresize(word7,[40 20]); subplot(5,7,15),imshow(word1),title('1'); subplot(5,7,16),imshow(word2),title('2'); subplot(5,7,17),imshow(word3),title('3'); subplot(5,7,18),imshow(word4),title('4'); subplot(5,7,19),imshow(word5),title('5'); subplot(5,7,20),imshow(word6),title('6'); subplot(5,7,21),imshow(word7),title('7'); imwrite(word1,'1.jpg'); imwrite(word2,'2.jpg'); imwrite(word3,'3.jpg'); imwrite(word4,'4.jpg'); imwrite(word5,'5.jpg'); imwrite(word6,'6.jpg'); imwrite(word7,'7.jpg'); liccode=char(['0':'9' 'A':'Z' '苏豫陕鲁']); %建立自动标识符元代码表 SubBw2=zeros(40,20); l=1; for I=1:7 ii=int2str(I); t=imread([ii,'.jpg']); SegBw2=imresize(t,[40 20],'nearest'); if l==1 %第一位汉字识别 kmin=37; kmax=40; elseif l==2 %第二位 A~Z 字母识别 kmin=11; kmax=36; else l>=3 %第三位以后是字母或数字识别 kmin=1; kmax=36; end 28