1、准备工作
1)准备材料
车牌字符模板和测试的实验车牌
2)车牌字符模板
数字、字母和省份缩写
3)测试车牌
四张测试车牌
2、车牌识别实现(已将其嵌入matlab)
1)打开APP
找到APP
找到我的APP双击点开
2)界面介绍
包括:按钮、视图界面和文本框等功能组件
按钮
包含打开图片、清楚图片、退出APP、关于APP、图片预处理、车牌定位、车牌定位处理、文字分割和识别结果
视图界面
初始车牌、预处理车牌、车牌定位和识别结果等
3、导入车牌、清除车牌、退出和关于APP按钮功能效果
导入车牌
代码实现
[filename,pathname]=uigetfile({'*.*';'*.png';'*.jpg';'*.bmp'});%获取图片路径
if isequal(filename,0)||isequal(pathname,0)%打开失败提示
errordlg('图片为空','错误');
app.Lamp_1.Color=('1.00,0.00,0.00');
else
img1=strcat(pathname,filename);%打开并显示图片
img1=imread(img1);
app.P1=img1;
imshow(img1,'Parent',app.UIAxes_1);
app.Lamp_1.Color=('0.00,1.00,0.00');
app.Lamp_2.Color=('0.00,1.00,0.00');
app.Lamp_3.Color=('0.00,1.00,0.00');
app.Lamp_4.Color=('0.00,1.00,0.00');
app.Lamp_5.Color=('0.00,1.00,0.00');
app.Lamp_6.Color=('0.00,1.00,0.00');
end
app.EditField_2.Value=('等待预处理');
app.EditField_3.Value=('等待定位');
工程效果
清除车牌
代码实现
im=imread("纯白.png");%清除显示区
imshow(im,'Parent',app.UIAxes_1);
imshow(im,'Parent',app.UIAxes_2);
imshow(im,'Parent',app.UIAxes_3);
imshow(im,'Parent',app.UIAxes_4);
imshow(im,'Parent',app.UIAxes_5);
imshow(im,'Parent',app.UIAxes_6);
imshow(im,'Parent',app.UIAxes_7);
imshow(im,'Parent',app.UIAxes_8);
imshow(im,'Parent',app.UIAxes_9);
imshow(im,'Parent',app.UIAxes_10);
imshow(im,'Parent',app.UIAxes_11);
app.Lamp_1.Color=('1.00,0.00,0.00');
app.Lamp_2.Color=('0.00,1.00,0.00');
app.Lamp_3.Color=('0.00,1.00,0.00');
app.Lamp_4.Color=('0.00,1.00,0.00');
app.Lamp_5.Color=('0.00,1.00,0.00');
app.Lamp_6.Color=('0.00,1.00,0.00');
app.EditField_2.Value='waiting';
app.EditField_3.Value='waiting';
app.EditField_1.Value='waiting';
工程效果
退出按钮
代码实现
choice=questdlg('确定关闭吗?','关闭','YES','NO','NO');%是否关闭程序
switch choice
case 'YES'
delete('1.jpg','2.jpg','3.jpg','4.jpg','5.jpg','6.jpg','7.jpg');
delete(app.UIFigure);
return;
case 'NO'
return;
end
工程效果
关于APP
代码实现
msgbox("这是一个车牌自动识别App","对话框",'help');%软件介绍对话框
工程效果
4、车牌识别过程
图片预处理
代码实现
img2=rgb2gray(app.P1);%灰度处理
app.P2=img2;
imshow(img2,'Parent',app.UIAxes_2);
img3= edge(app.P2, 'roberts', 0.15,'both');%边缘处理
app.P3=img3;
imshow(img3,'Parent',app.UIAxes_2);
app.EditField_2.Value=('完成预处理');
app.Lamp_2.Color=('1.00,0.00,0.00');
工程效果
车牌定位
代码实现
se=[1,1,1]';%腐蚀处理
img4 = imerode(app.P3,se);
app.P4=img4;
imshow(img4,'Parent',app.UIAxes_3);
se1=strel('rectangle',[30,30]);%图像平滑
img5=imclose(app.P4, se1);
app.P5=img5;
imshow(img5,'Parent',app.UIAxes_3);
img6= bwareaopen(app.P5, 2200);%移除对象
app.P6=img6;
imshow(img6,'Parent',app.UIAxes_3);
app.EditField_3.Value='完成车牌定位';
[img_Y1, img_Y2, img_X1, img_X2]=cj(app.P6);
img8 =app.P1(img_Y1:img_Y2,img_X1:img_X2,:);%原图裁剪
app.P7=img8;
imshow(img8,'Parent',app.UIAxes_3);
app.EditField_3.Value='完成裁剪处理';
app.Lamp_3.Color=('1.00,0.00,0.00');
工程效果
定位图处理
代码实现
img9=rgb2gray(app.P7);%灰度
app.P8=img9;
imshow(img9,'Parent',app.UIAxes_4);
img10=histeq(app.P8);%直方图均衡化
app.P9=img10;
imshow(img10,'Parent',app.UIAxes_4);
img11=im2bw(app.P9,0.76);%二值化
app.P10=img11;
imshow(img11,'Parent',app.UIAxes_4);
img12=medfilt2(app.P10);%中值滤波1
img12=medfilt2(img12);%中值滤波2
app.P11=img12;
imshow(img12,'Parent',app.UIAxes_4);
img12=fgt(img12);%裁剪
app.P12=img12;
imshow(img12,'Parent',app.UIAxes_4);
app.Lamp_4.Color=('1.00,0.00,0.00');
工程效果
文字分割
代码实现
img12=app.P12;
[m,n] = size(img12);
s = sum(img12);
j = 1;
k1 = 1;
k2 = 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(img12(:, [k1+5:k2-5])));
img12(:, k1+num+5) = 0;
end
end
[word1,img12]=fgzf(img12);%获取字符
[word2,img12]=fgzf(img12);
[word3,img12]=fgzf(img12);
[word4,img12]=fgzf(img12);
[word5,img12]=fgzf(img12);
[word6,img12]=fgzf(img12);
[word7,img12]=fgzf(img12);
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]);
imshow(word1,'Parent',app.UIAxes_5);
imshow(word2,'Parent',app.UIAxes_6);
imshow(word3,'Parent',app.UIAxes_7);
imshow(word4,'Parent',app.UIAxes_8);
imshow(word5,'Parent',app.UIAxes_9);
imshow(word6,'Parent',app.UIAxes_10);
imshow(word7,'Parent',app.UIAxes_11);
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');
app.Lamp_5.Color=('1.00,0.00,0.00');
工程效果
识别结果
代码实现
liccode=char(['0':'9' 'A':'Z' '京辽鲁陕苏豫浙贵闽粤沪川藏鄂赣甘桂黑冀津吉蒙宁青琼晋皖湘新渝']);
subBw2 = zeros(40, 20);
num = 1; % 车牌位数
for i = 1:7
ii = int2str(i); % 将整型数据转换为字符串型数据
word = imread([ii,'.jpg']); % 读取之前分割出的字符的图片
segBw2 = imresize(word, [40,20], 'nearest'); % 调整图片的大小
segBw2 = im2bw(segBw2, 0.5); % 图像二值化
if i == 1 % 字符第一位为汉字,定位汉字所在字段
kMin = 37;
kMax = 44;
elseif i == 2 % 第二位为英文字母,定位字母所在字段
kMin = 11;
kMax = 36;
elseif i >= 3 % 第三位开始就是数字了,定位数字所在字段
kMin = 1;
kMax = 36;
end
l = 1;
for k = kMin : kMax
fname = strcat("ZFMB\",liccode(k),'.jpg'); % 根据字符库找到图片模板
samBw2 = imread(fname); % 读取模板库中的图片
samBw2 = im2bw(samBw2, 0.5);% 图像二值化
% 将待识别图片与模板图片做差
for i1 = 1:40
for j1 = 1:20
subBw2(i1, j1) = segBw2(i1, j1) - samBw2(i1 ,j1);
end
end
Dmax = 0;
for i2 = 1:40
for j2 = 1:20
if subBw2(i2, j2) ~= 0
Dmax = Dmax + 1;
end
end
end
error(l) = Dmax;
l = l + 1;
end
errorMin = min(error);
findc = find(error == errorMin);
Code(num*2 - 1) = liccode(findc(1) + kMin - 1);
Code(num*2) = ' ';
num = num + 1;
end
delete('1.jpg','2.jpg','3.jpg','4.jpg','5.jpg','6.jpg','7.jpg');
app.EditField_1.Value=(Code);
app.Lamp_6.Color=('1.00,0.00,0.00');
工程效果