MATLAB进行车牌识别——清晰易懂

MATLAB进行车牌识别

  1. 图像读取
  2. 图像增强
  3. 车牌位置识别

未进行文字提取,net训练的脑壳痛
原理简单,就不打了,过程按以上步骤

源码如下,已运行测试

clc
clear
I=imread('2.jpg');
figure(1);
imshow(I);
title('原图');

%---------------------------------------------找车牌——-----------------------------------------%
%灰度化
I1=rgb2gray(I);
figure(2);
subplot(1,2,1);
imshow(I1);
title('灰度图');
figure(2);
subplot(1,2,2);
imhist(I1);
title('灰度图直方图');

%采用I1作为它的输入,并返回一个与I1相同大小的二值化图像,进行模板减法运算,在函数检测到边缘的地方为1,其他地方为0
I2=edge(I1,'Prewitt',0.15,'both');
figure(3),imshow(I2);title(' 锐化,Prewitt算子边缘检测')

I2=bwareaopen(I2,10);
[B,L]=bwboundaries(I2,'noholes');

out_result=regionprops(I2,'Extent','Centroid','boundingbox'); %各连通区域像素点与最小边界像素点比值
centroids = cat(1, out_result.Centroid);   %各连通区域质心
draw_rect=cat(1,out_result.BoundingBox);   %各连通区域最小边界矩形

%秒绘出车牌框
hold on;
for j=1:length(B)
    boundary=B{j};
    plot(boundary(:,2),boundary(:,1),'r','LineWidth',2); 
end
hold off;

for i=1:size(out_result)
    rectangle('position',draw_rect(i,:),'EdgeColor','y','LineWidth',2);  %绘出各连通区域最小边界矩形
end

%进行开运算
se=[1;1;1];%腐蚀模板
I3=imerode(I2,se);%腐蚀
figure(4);
subplot(1,2,1);
imshow(I3);
title('腐蚀后图像');
se=strel('rectangle',[25,25]);%膨胀模板
I4=imclose(I3,se);%膨胀
subplot(1,2,2);
imshow(I4);
title('膨胀后图像');
figure(5);
imshow(I4);
title('开运算后的轮廓');

%删除二值图像中面积小于1500的对象
I5=bwareaopen(I4,1500);
figure(6);
imshow(I5);
title('从图象中移除小对象');
[y,x,z]=size(I5);

%确定车牌范围
 %----------Y方向---------%
myI=double(I5);
tic
 white_y=zeros(y,1);
 for i=1:y
    for j=1:x
             if(myI(i,j,1)==1) 
                white_y(i,1)= white_y(i,1)+1;
            end  
     end       
 end
 [temp,MaxY]=max(white_y);
 PY1=MaxY;
 while ((white_y(PY1,1)>=5)&&(PY1>1))
        PY1=PY1-1;
 end    
 PY2=MaxY;
 while ((white_y(PY2,1)>=5)&&(PY2<y))
        PY2=PY2+1;
 end
 IY=I(PY1:PY2,:,:);
 
 %----------X方向---------%
 %进一步确定x方向的车牌区域
 Blue_x=zeros(1,x);
 for j=1:x
     for i=PY1:PY2
            if(myI(i,j,1)==1)
                Blue_x(1,j)= Blue_x(1,j)+1;               
            end  
     end       
 end
 PX1=1;
 while ((Blue_x(1,PX1)<3)&&(PX1<x))
       PX1=PX1+1;
 end    
 PX2=x;
 while ((Blue_x(1,PX2)<3)&&(PX2>PX1))
        PX2=PX2-1;
 end
 
 %对车牌区域的校正
 PX1=PX1-1;
 PX2=PX2+1;
 dw=I(PY1:PY2-8,PX1:PX2,:);
 
%计时结束
t=toc; 

figure(7),subplot(1,2,1),imshow(IY),title('行方向合理区域');
figure(7),subplot(1,2,2),imshow(dw),title('定位剪切后的彩色车牌图像')
imwrite(dw,'dw.jpg');
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)/3); % T 为二值化的阈值   向最近的方向取整
%[m,n,k]=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);
%filter2(B,X),B为滤波器.X为要滤波的数据,这里将B放在X上,一个一个移动进行模板滤波. 
d=imbinarize(round(filter2(h,d)));
imwrite(d,'4.均值滤波后.jpg');
figure(8),subplot(3,2,4),imshow(d),title('4.均值滤波后')

%膨胀或腐蚀处理
se=eye(2);%产生2×2的单位矩阵
[m,n]=size(d);
if bwarea(d)/m/n>=0.365 %bwarea是计算二值图像中对象的总面积的函数
    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.膨胀或腐蚀处理后')

有时间会加上训练模型,以及文字提取的程序

  • 2
    点赞
  • 34
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值