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); @*20的零矩阵 l=1; for I=1:7
ii=int2str(I); %int2str整型转换字符串 t=imread([ii,'.jpg']);
SegBw2=imresize(t,[40 20],'nearest'); %改变图片的大小 if l==1 %第一位汉字识别 kmin=37;
kmax=41; %模板中汉字所在的位置 elseif l==2 %第二位 A~Z 字母识别 kmin=11;
kmax=36; %模板中数字所在的位置
else l>=3 %第三位以后是字母或数字识别 kmin=1;
kmax=36; %模板中字母所在的位置
end
for k2=kmin:kmax
fname=strcat('字符模板\\',liccode(k2),'.jpg'); SamBw2=imread(fname);
Error(k2)=sum(sum(abs(SegBw2-SamBw2)));
%将现有字符逐个与模板字符相减,认为相减误差最小的现有字符与该模板字符匹配 end
Error1=Error(kmin:kmax); MinError=min(Error1);
findc=find(Error1==MinError);%寻找最匹配的字符
Code(l*2-1)=liccode(findc(1)+kmin-1);%将上述找到的字符位置与liccode中的字符链接上
Code(l*2)=' ';%在字符之间加空格 l=l+1; end
figure(10),imshow(a),title (['车牌号码:', Code],'Color','b');
函数getword
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
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 %end
result=d;
qiege函数
function e=qiege(d) [m,n]=size(d); top=1; %下端 bottom=m; %下端 left=1; %左端 right=n; % 右端
while sum(d(top,:))==0 && top<=m %如果矩阵d的第top行的和为0的话 top=top+1; end
while sum(d(bottom,:))==0 && bottom>=1 %如果矩阵d的第bottom行的和为0的话 bottom=bottom-1;