题目:MATLAB车牌识别系统
内容:介绍随着二十一世纪到来,经济快速发展和人们生活水平显著提高,汽车逐渐成为家庭的主要交通工具。汽车的产量快速增多,车辆流动也变得越来越频繁,因此给交通带来了严重问题,如交通堵塞、交通事故等,智能交通系统(Intelligent Transportation System)的产生就是为了从根本上解决交通问题。在智能交通系统中车牌识别技术占有重要位置,车牌识别技术的推广普及必将对加强道路管理、城市交通事故、违章停车、处理车辆被盗案件、保障社会稳定等方面产生重大而深远的影响。该设计主要研究基于MATLAB软件的汽车号牌设别系统设计,系统主要包括图像采集、图像预处理、车牌定位、字符分割、字符识别五大核心部分。系统的图像预处理模块是将图像经过图像灰度化、图像增强、边缘提取、二值化等操作,转换成便于车牌定位的二值化图像;利用车牌的边缘、形状等特征,再结合Roberts 算子边缘检测、数字图像、形态学等技术对车牌进行定位;字符的分割采用的方法是将二值化后的车牌部分进行寻找连续有文字的块,若长度大于设定的阈值则切割,从而完成字符的分割;字符识别运用模板匹配算法完成。以上每个功能模块用MATLAB软件实现,
- 总体设计
汽车号牌识别系统技术是从一幅车辆图像中准确定位出车牌区域,然后经过字符切割和字符识别来实现车辆牌照的自动识别。主要流程图如下:图1.1
- 总体功能模块
- 基于MATLAB车牌识别系统主要包括图像采集、图像预处理、车牌定位、字符分割、字符识别五个关键环节[11],其基本工作如下:(1) 图像采集:使用摄像头、照相机拍摄采集图像。(2) 图像预处理:把图像转换成便于定位的二值化图像,需要经过图像灰度化、图像增强、边缘提取、二值化操作。(3)车牌定位:利用车牌的边缘、形状等特征,再结合Roberts 算子边缘检测、数字图像、形态学等技术对车牌进行定位。(4)字符分割:以二值化后的车牌部分进行寻找连续有文字的块,若长度大于设定的阈值则切割,从而完成字符的分割。(5)字符识别:运用模板匹配算法将分割后的字符二值化,并将其尺寸大小缩放为模板库中字符的大小,然后与所有的模板进行匹配,准确地识别车牌。输出识别结果,并进行数据存储。由于纹理特征车牌灰度图像的边缘、图像水平方向上的方差、水平方向上的梯度等比较稳定而且易于提取,所以本系统车牌定位算法采用纹理特征作为车牌的主要特征。在汽车号牌字符识别中,由于汉字的复杂性所以本设计的模板库字符包含5个汉字,26个大写英文字母及10个阿拉伯数字。首字符为汉字,第2-6个字符为英文字母或数字。本系统采用的是边缘检测的方法实现车牌定位的,寻找连续有文字的块的方法实现字符分割,模板匹配法来确定最终的识别结果。
- 具体设计下图
- 源码
function varargout = chepai(varargin)
% CHEPAI M-file for chepai.fig
% CHEPAI, by itself, creates a new CHEPAI or raises the existing
% singleton*.
%
% H = CHEPAI returns the handle to a new CHEPAI or the handle to
% the existing singleton*.
%
% CHEPAI('CALLBACK',hObject,eventData,handles,...) calls the local
% function named CALLBACK in CHEPAI.M with the given input arguments.
%
% CHEPAI('Property','Value',...) creates a new CHEPAI or raises the
% existing singleton*. Starting from the left, property value pairs are
% applied to the GUI before chepai_OpeningFcn gets called. An
% unrecognized property name or invalid value makes property application
% stop. All inputs are passed to chepai_OpeningFcn via varargin.
%
% *See GUI Options on GUIDE's Tools menu. Choose "GUI allows only one
% instance to run (singleton)".
%
% See also: GUIDE, GUIDATA, GUIHANDLES
% Edit the above text to modify the response to help chepai
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @chepai_OpeningFcn, ...
'gui_OutputFcn', @chepai_OutputFcn, ...
'gui_LayoutFcn', [] , ...
'gui_Callback', []);
if nargin && ischar(varargin{1})
gui_State.gui_Callback = str2func(varargin{1});
end
if nargout
[varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
gui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT
% --- Executes just before chepai is made visible.
function chepai_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% varargin command line arguments to chepai (see VARARGIN)
% Choose default command line output for chepai
handles.output = hObject;
% Update handles structure
guidata(hObject, handles);
% UIWAIT makes chepai wait for user response (see UIRESUME)
% uiwait(handles.figure1);
% --- Outputs from this function are returned to the command line.
function varargout = chepai_OutputFcn(hObject, eventdata, handles)
% varargout cell array for returning output args (see VARARGOUT);
% hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Get default command line output from handles structure
varargout{1} = handles.output;
% --- Executes on button press in find.
function find_Callback(hObject, eventdata, handles)
% hObject handle to find (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
%获取图像 装入待处理彩色图像并显示原始图像
global Scolor
[fn,pn,fi]=uigetfile('*.bmp','选择图片');
Scolor=imread([pn fn]);
axes(handles.axes1);
imshow(Scolor);
title('车牌原始图像');
% --- Executes on button press in ok.
function ok_Callback(hObject, eventdata, handles)
% hObject handle to ok (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
global Scolor
%axes(handles.axes1);
%imshow(Scolor),title('车牌原始图像');
%将彩色图像转换为黑白并显示
Sgray = rgb2gray(Scolor);%rgb2gray转换成灰度图
%figure,imshow(Sgray),title('原始黑白图像');
%c=histeq(Sgray);
%figure,imshow(c);title('直方图均衡化图像');
s=strel('disk',13);%strei函数13
Bgray=imopen(Sgray,s);%打开sgray s图像
%figure,imshow(Bgray);title('背景图像');%输出背景图像
Egray=imsubtract(Sgray,Bgray);%两幅图相减
%figure,imshow(Egray);title('增强黑白图像');%输出黑白图像
grd=edge(Egray,'canny',0.09,'both');
%figure,imshow(grd);title('robert算子边缘检测');
se=[1;1;1]; %线型结构元素
I3=imerode(grd,se); %腐蚀图像
[y,x,]=size(bg2);
I6=double(bg2);
%绘制行曲线图
Y1=zeros(y,1);%y行1列的零矩阵
for i=1:y
for j=1:x
if(I6(i,j,1)==1)
Y1(i,1)= Y1(i,1)+1;
end
end
end
% figure();
% subplot(1,3,1);
% plot(0:y-1,Y1),title('行像素灰度值累计'),xlabel('行值'),ylabel('像素和');
[temp, MaxY]=max(Y1);
PY1=MaxY;
while ((Y1(PY1,1)>=50)&&(PY1>1))
PY1=PY1-7;
end
PY2=MaxY;
while ((Y1(PY2,1)>=50)&&(PY2<y))
PY2=PY2+7;
end
%绘制列曲线图
X1=zeros(1,x);
for j=1:x
for i=PY1:PY2
if(I6(i,j,1)==1)
X1(1,j)= X1(1,j)+1;
end
end
end
% subplot(1,3,2);
% plot(0:x-1,X1),title('列像素灰度值累计'),xlabel('列值'),ylabel('像数和');
PX1=1;
while ((X1(1,PX1)<3)&&(PX1<x))
PX1=PX1+1;
end
PX2=x;
while ((X1(1,PX2)<3)&&(PX2>PX1))
PX2=PX2-1;
end
DW=Scolor(PY1:PY2,PX1:PX2,:);
axes(handles.axes2);
imshow(DW),title('车牌定位图像');
%if isrgb(DW)
I1 = rgb2gray(DW); %将RGB图像转化为灰度图像
%else I1=DW;
%end
%figure,imshow(I1),title('车牌灰度图像');
g_max=double(max(max(I1)));
g_min=double(min(min(I1)));
T=round(g_max-(g_max-g_min)/3); % T 为二值化的阈值
[m,n]=size(I1);% d:二值图像
%h=graythresh(I1);
imane_bw=im2bw(I1,T/256);
%figure,imshow(imane_bw),title('二值化车牌图像');
% imane_bw=bwareaopen(I1,3);
% figure,imshow(imane_bw),title('形态学滤波后的二值化图像');
[y1,x1,z1]=size(imane_bw);
I3=double(imane_bw);
TT=1;
%%%%%%%去除图像顶端和底端的不感兴趣区域%%%%%
Y1=zeros(y1,1);
for i=1:y1
for j=1:x1
if(I3(i,j,1)==1)
Y1(i,1)= Y1(i,1)+1 ;
end
end
end
Py1=1;
Py0=1;
while ((Y1(Py0,1)<9)&&(Py0<y1))
Py0=Py0+1;
end
Py1=Py0;
while((Y1(Py1,1)>=9)&&(Py1<y1))
Py1=Py1+1;
end
I2=imane_bw(Py0:Py1,:,:);
axes(handles.axes3);
imshow(I2),title('目标车牌区域');
% 寻找连续有文字的块,若长度大于某阈值,则认为该块有两个字符组成,需要分割
d=qiege(I2);
[m,n]=size(d);
%figure,subplot(2,1,1),imshow(d),title(n)
k1=1;k2=1;s=sum(d);j=1;
liccode=char(['0':'9' 'A':'Z' '藏川甘赣贵桂黑沪吉济冀津晋京警兰辽领鲁蒙闽宁青琼陕使苏皖湘新学渝豫粤云浙']); %建立自动识别字符代码表
tt=1;
l=1;
for I=1:7
ii=int2str(I);
t=imread([ii,'.jpg']);
t=255-t;
level=graythresh(t);
t=im2bw(t,level);
SegBw2=imresize(t,[22 14],'nearest');
if tt==1 %第一位汉字识别
kmin=37;
kmax=72;
t=~t;
SegBw2=imresize(t,[22 14],'nearest');
elseif tt==2 %第二位 A~Z 字母识别
kmin=11;
kmax=36;
else %第三位以后是字母或数字识别
kmin=1;
kmax=36;
end
for k2=kmin:kmax
fname=strcat('code module\Sam',liccode(k2),'.jpg');
%SamBw2 = imread(fname);
SamBw22 = imread(fname);
SamBw22 = rgb2gray(SamBw22);
SamBw2=imresize(SamBw22,[22 14],'nearest');
level=graythresh(SamBw2);
SamBw2=im2bw(SamBw2,level);
for i=1:22
for j=1:14
SubBw2(i,j)=SegBw2(i,j)-SamBw2(i,j);
end
end
% 以上相当于两幅图相减得到第三幅图
Dmax=0;
for k1=1:22
for l1=1:14
if ( SubBw2(k1,l1) > 0 || SubBw2(k1,l1) <0 )
Dmax=Dmax+1;
end
end
end
Error(k2)=Dmax;
end
Error1=Error(kmin:kmax);
MinError=min(Error1);
findc=find(Error1==MinError);
if tt==1
findc=findc+36;
end
if tt==2
findc=findc+10;
end
tt=tt+1;
res=liccode(findc);
shibiejieguo(1,l)=res;
l=l+1;
end
set(handles.edit2,'string',shibiejieguo);
%==========================对话框显示显示=============================================
function edit2_Callback(hObject, eventdata, handles)
% hObject handle to edit2 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hints: get(hObject,'String') returns contents of edit2 as text
% str2double(get(hObject,'String')) returns contents of edit2 as a double
% --- Executes during object creation, after setting all properties.
function edit2_CreateFcn(hObject, eventdata, handles)
% hObject handle to edit2 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% Hint: edit controls usually have a white background on Windows.
% See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
% --- Executes on button press in stop.
function stop_Callback(hObject, eventdata, handles)
% hObject handle to stop (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
ax1=(handles.axes1);cla(ax1,'reset')
ax2=(handles.axes2);cla(ax2,'reset')
ax3=(handles.axes3);cla(ax3,'reset')
ax4=(handles.axes4);cla(ax4,'reset')
ax5=(handles.axes5);cla(ax5,'reset')
ax6=(handles.axes6);cla(ax6,'reset')
ax7=(handles.axes7);cla(ax7,'reset')
ax8=(handles.axes8);cla(ax8,'reset')
ax9=(handles.axes9');cla(ax9,'reset')
ax10=(handles.axes10);cla(ax10,'reset')
set(handles.edit2,'string',' ');