【车牌识别】基于BP神经网络求解车牌识别问题matlab代码​
1 简介随着信息技术的不断成熟和网络应用的持续发展人们的日常生活与工作越来越趋于数字化和网络化。近年来由于我国工业化的不断发展和人们生活水平的不断提高汽车这一主要交通工具充斥了我们生活的方方面面所以利用相关信息技术采集汽车信息并进行管理具有至关重要的意义同时也是信息社会的标志。其中识别汽车牌照是当前智能交通管理的主要任务和前提也是当下最热门的研究方向之一。为了识别出汽车牌照我们需要完成以下任务在复杂背景下分析并处理图像然后将牌照定位分割再对字符进行分割最后系统就可以自动识别出相应汽车牌照上的字符。众所周知汽车牌照具有唯一性这是我们进行智能交通管理的重要依据基于此我们可以快速高效的对车辆识别、统计。充分利用计算机上的视觉系统以及神经网络和数字图像处理等相关技术我们就能够设计出识别汽车牌照的系统。汽车牌照识别作为最前沿、最基础的技术在实现当代交通的智能化管理中扮演着重要角色关乎智能交通管理的成败。利用牌照识别系统对收集到的图像自动提取汽车牌照信息然后定位和分割牌照最后正确的识别出字符这样就可以将交通管理的复杂度降低。计算机操作下的这种汽车牌照识别比人工汽车牌照识别操作起来更简单工作效率高。为正确识别出汽车牌照我们需要完成的主要任务是正确定位车牌的所在位置准确地分割出车牌以及分割出车牌字符将分割好的字符逐一识别出来。但是由于外界因素千差万别一点细微的变化就可能影响到牌照识别的精准度比如车辆在抖动时收集的图像会变的歪斜、有的车辆牌照因磨损和污迹难以识别、甚至在收集图像时不同光的照也会产生或多或少的影响。因此人们至今也没有想到用什么方法可以在复杂环境下精准无误的识别出车牌信息复杂的环境条件无疑将对汽车牌照识别技术产生深远的影响。​随着信息时代的到来现代智能交通系统能够很轻易的识别出汽车牌照这是智能交通管理的标志之一。智能交通管理系统的牌照识别集合了图像采集和预处理、车牌定位技术、字符分割和字符识别等相关技术。其中车牌定位、字符分割和字符识别是最关键的技术也是本次毕业设计的难点所在。正确利用好这三种关键技术将有助于牌照识别的实时性和准确性对于智能交通系统的实现有着决定性作用。在MATLAB软件开发环境下系统首先对图像进行预处理、然后将预处理后的图像进行定位分割最后识别出相应牌照上的字符这样就可以模拟设计出汽车牌照识别系统。本文的图像预处理模块是将图像灰度化和用Canny算子进行边缘检测汽车牌照定位依据是它的颜色特征使用MATLAB中的Radon函数和Imrotate函数来进行车牌矫正分割字符时需要先找到连续的文字块然后根据长度大小来确定是否分割假如所找到的连续文字块的长度大于阈值那么就表示可以对此文字块进行分割。并且为了能对车牌上的字符进行正确的识别本文将采用BP神经网络算法。最后设计GUI界面使界面更加简洁明了便于操作。根据实验得出的结论这种方式可以对蓝色的车牌进行高效、精确的识别同时也对光照、旋转和噪声表现出很好的鲁棒性定位精度和识别正确率甚至可以超过90%。​2 部分代码function varargout run(varargin) % RUN MATLAB code for run.fig % RUN, by itself, creates a new RUN or raises the existing % singleton*. % % H RUN returns the handle to a new RUN or the handle to % the existing singleton*. % % RUN(CALLBACK,hObject,eventData,handles,...) calls the local % function named CALLBACK in RUN.M with the given input arguments. % % RUN(Property,Value,...) creates a new RUN or raises the % existing singleton*. Starting from the left, property value pairs are % applied to the GUI before run_OpeningFcn gets called. An % unrecognized property name or invalid value makes property application % stop. All inputs are passed to run_OpeningFcn via varargin. % % *See GUI Options on GUIDEs 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 run % Last Modified by GUIDE v2.5 07-May-2016 15:41:22 % Begin initialization code - DO NOT EDIT gui_Singleton 1; gui_State struct(gui_Name, mfilename, ... gui_Singleton, gui_Singleton, ... gui_OpeningFcn, run_OpeningFcn, ... gui_OutputFcn, run_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 run is made visible. function run_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 run (see VARARGIN) % Choose default command line output for run handles.output hObject; handles.cd0 cd; handles.Color 0; handles.I []; axes(handles.axes1); set(gca,Xtick,[]); set(gca,Ytick,[]); box on; axes(handles.axes2); set(gca,Xtick,[]); set(gca,Ytick,[]); box on; axes(handles.axes3); set(gca,Xtick,[]); set(gca,Ytick,[]); box on; axes(handles.axes4); set(gca,Xtick,[]); set(gca,Ytick,[]); box on; axes(handles.axes5); set(gca,Xtick,[]); set(gca,Ytick,[]); box on; axes(handles.axes6); set(gca,Xtick,[]); set(gca,Ytick,[]); box on; axes(handles.axes8); set(gca,Xtick,[]); set(gca,Ytick,[]); box on; axes(handles.axes9); set(gca,Xtick,[]); set(gca,Ytick,[]); box on; axes(handles.axes12); set(gca,Xtick,[]); set(gca,Ytick,[]); box on; axes(handles.axes13); set(gca,Xtick,[]); set(gca,Ytick,[]); box on; axes(handles.axes14); set(gca,Xtick,[]); set(gca,Ytick,[]); box on; axes(handles.axes15); set(gca,Xtick,[]); set(gca,Ytick,[]); box on; axes(handles.axes16); set(gca,Xtick,[]); set(gca,Ytick,[]); box on; axes(handles.axes17); set(gca,Xtick,[]); set(gca,Ytick,[]); box on; % Update handles structure guidata(hObject, handles); % UIWAIT makes run wait for user response (see UIRESUME) % uiwait(handles.figure1); % --- Outputs from this function are returned to the command line. function varargout run_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 pushbutton1. function pushbutton1_Callback(hObject, eventdata, handles) % hObject handle to pushbutton1 (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) %% 读图 [filename, cd1] uigetfile( ... {*.tif;*.TIF;*.JPG;*.jpg;*.bmp;*.BMP;*.jpeg;*.JPEG;,Image file;... *.*, All file (*.*)},Pick an Image); axes(handles.axes1); cla; axes(handles.axes2); cla; axes(handles.axes3); cla; axes(handles.axes4); cla; if filename cd(cd1); d imread(filename); cd(handles.cd0); handles.I d; axes(handles.axes1); imshow(d); handles.filename filename; box on; end handles.Color 0; cd(handles.cd0); set(handles.text2,string,); guidata(hObject, handles); % --- Executes on button press in pushbutton2. function pushbutton2_Callback(hObject, eventdata, handles) % hObject handle to pushbutton2 (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) image handles.I; gray rgb2gray(image); % 图像灰度化 axes(handles.axes2); imshow(gray); % --- Executes on button press in pushbutton3. function pushbutton3_Callback(hObject, eventdata, handles) % hObject handle to pushbutton3 (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) image handles.I; gray rgb2gray(image); new_gray histeq(gray); % 直方图均衡 图像增强 axes(handles.axes3); imshow(new_gray); % --- Executes on button press in pushbutton4. function pushbutton4_Callback(hObject, eventdata, handles) % hObject handle to pushbutton4 (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) image handles.I; gray rgb2gray(image); new_gray histeq(gray); % 直方图均衡 图像增强 if size(new_gray,1)1000 new_gray_1 imresize(new_gray,0.1); else new_gray_1 new_gray; end bw edge(new_gray_1,canny); axes(handles.axes4); imshow(bw); % --- Executes on button press in pushbutton5. function pushbutton5_Callback(hObject, eventdata, handles) % hObject handle to pushbutton5 (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) %% chepai dingwei Image handles.I; DI Image(:,:,3); GI (Image(:,:,1)100 Image(:,:,2)150 Image(:,:,3)120 ... abs(double(Image(:,:,2))-double(Image(:,:,3)))30); axes(handles.axes5); imshow(GI); handles.GI GI; guidata(hObject, handles); % --- Executes on button press in pushbutton6. function pushbutton6_Callback(hObject, eventdata, handles) % hObject handle to pushbutton6 (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) %% fengecheoai d handles.GI; se ones(40); % 腐蚀膨胀模版 d imdilate(d,se);% 再做膨胀运算 d imerode(d,se); % 先做腐蚀运算 % 先膨胀后腐蚀的过程称为开运算。用来填充物体内细小空洞、连接邻近物体、平滑其边界的同时并不明显改变其面积。 d bwareaopen(d,100); % 移除小对象 小区域肯定是噪声 STATS regionprops(d);%统计被标记的区域的面积分布显示区域总数 area []; for i 1:size(STATS,1) area [area;STATS(i).Area]; end [value,index] max(area); Bound round(STATS(index).BoundingBox); xmin Bound(2); xmax Bound(2)Bound(4); ymin Bound(1); ymax Bound(1)Bound(3); II handles.I(xmin:xmax,ymin:ymax,:); axes(handles.axes6); imshow(II); handles.Divice II; guidata(hObject, handles); % --- Executes on button press in pushbutton7. function pushbutton7_Callback(hObject, eventdata, handles) % hObject handle to pushbutton7 (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) %% 车牌预处理 校正 去边框 I_old handles.Divice; if size(I_old,2) 900 I imresize(I_old,0.1); else I I_old; end I_grayrgb2gray(I); %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%水平校正 I_gray_bwedge(I_gray,sobel,horizontal); theta11:180; [R1,xp1]radon(I_gray_bw,theta1); [I1,J1]find(R1max(max(R1))); qxj_hor90-J1; I_HOR_JZimrotate(I,qxj_hor,bilinear,crop); %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%水平校正结束 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%水平去边框 I_hor_jz_grayrgb2gray(I_HOR_JZ); I_hor_jz_gray_bwedge(I_hor_jz_gray,canny); [m,n]size(I_hor_jz_gray_bw); count_horzeros(m,1); for i1:1:m tbd0; for j1:1:n-1 if I_hor_jz_gray_bw(i,j)~I_hor_jz_gray_bw(i,j1); tbdtbd1; end end count_hor(i,1)tbd/2; end for i11:1:m if count_hor(i1,1)9 TOPi1; break; end end for j1m:-1:1; if count_hor(j1,1)9 BOTTOMj1; break; end end I_HOR_JZ_SI_HOR_JZ(TOP:BOTTOM,:,:); %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%水平去边框结束 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%垂直校正 I_hor_jz_graymedfilt2(I_hor_jz_gray); I_hor_jz_gray_bw1edge(I_hor_jz_gray,canny); [height,width]size( I_hor_jz_gray_bw1); I_hor_jz_gray_bw1_1I_hor_jz_gray_bw1(:,width/2:width); I_hor_jz_gray_bw1_2I_hor_jz_gray_bw1(:,1:width/2); theta2-30:30; [R2,xp2]radon(I_hor_jz_gray_bw1_1,theta2); [R3,xp3]radon(I_hor_jz_gray_bw1_2,theta2); [I2,J2]find(R2max(max(R2))); [I3,J3]find(R3max(max(R3))); qxj_ver2J2-30; qxj_ver2_smean(qxj_ver2(:)); qxj_ver3J3-30; qxj_ver3_smean(qxj_ver3(:)); if abs(qxj_ver2_s-qxj_ver3_s)3 qxj_ver(qxj_ver2_sqxj_ver3_s)/2; else [mexp,nexp]size(I_HOR_JZ_S); count_blackzeros(61,nexp); count_whitezeros(61,nexp); for angle-30:30 Tmaketform(affine,[1 -tand(angle) 0;0 1 0;0 0 1]); I_VER_expimtransform(I_HOR_JZ_S,T,FillValues,255); I_VER_exprgb2gray(I_VER_exp); I_VER_exphisteq(I_VER_exp); I_VER_expmedfilt2(I_VER_exp); threshexpgraythresh(I_VER_exp); I_VER_exp_erzhiim2bw(I_VER_exp,threshexp); [mexp,nexp]size(I_VER_exp_erzhi); whitezeros(1,nexp); blackzeros(1,nexp); for zexp1:nexp for cexp1:mexp if I_VER_exp_erzhi(cexp,zexp)1 white(1,zexp) white(1,zexp)1; else black(1,zexp)black(1,zexp)1; end end if white(1,zexp)0 count_white(angle31,zexp)1; end if black(1,zexp)0 count_black(angle31,zexp)1; end end end resultcount_whitecount_black; lingzeros(61,1); for o1:61 for p1:nexp if result(o,p)0 ling(o,1)ling(o,1)1; end end end [hang2,lie2]find(lingmax(max(ling))); qxj_verhang2-31; qxj_vermean(qxj_ver(:)) end T1maketform(affine,[1 -tand(qxj_ver) 0;0 1 0;0 0 1]); I_VER_JZimtransform(I_HOR_JZ_S,T1,FillValues,255); %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%垂直校正结束 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%垂直去边框 I_VER_JZ_grayrgb2gray(I_VER_JZ); I_VER_JZ_grayhisteq(I_VER_JZ_gray); I_VER_JZ_graymedfilt2(I_VER_JZ_gray); threshgraythresh(I_VER_JZ_gray); I_VER_JZ_erzhiim2bw(I_VER_JZ_gray,thresh); % figure(6);imshow(I_VER_JZ_erzhi);title(二值图); [m1,n1]size(I_VER_JZ_erzhi); count_verzeros(n1,1); for z1:n1 tbd10; for c1:m1-1 if I_VER_JZ_erzhi(c,z)~I_VER_JZ_erzhi(c1,z) tbd1tbd11; end end count_ver(z,1)tbd1; end for z11:1:n1-1 if count_ver(z1,1)1count_ver(z11,1)1 LEFTz1; break; end end for c1n1:-1:2 if count_ver(c1,1)1count_ver(c1-1,1)1 RIGHTc1; break; end end I_VER_JZ_SI_VER_JZ(:,LEFT:RIGHT,:); axes(handles.axes8); imshow(I_VER_JZ_S); handles.chepaiyu I_VER_JZ_S; handles.chepaiyuT thresh; guidata(hObject, handles); % --- Executes on button press in pushbutton8. function pushbutton8_Callback(hObject, eventdata, handles) % hObject handle to pushbutton8 (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) %% 字符分割 I_VER_JZ_S handles.chepaiyu; thresh handles.chepaiyuT; I_1rgb2gray(I_VER_JZ_S); Kim2bw(I_1,thresh); [kuan,chang]size(K); % STATS regionprops(K); % % % for i 1:size(STATS,1) % STATS(i).Area % rectangle(Position,STATS(4).BoundingBox,EdgeColor,r); % [x,y]find(K1) a1length(x); [x,y]find(K0) b1length(y); if a1b1 K1~K; else K1K; end [a b] size(K1); sumK sum(K1); %寻找阈值 T2; myb find(sumKT); %所在的列为myb myf zeros(1,length(myb)); %大于阈值的列的数目 for ii 2:length(myb) if myb(ii)-myb(ii-1)1 myb(ii-1) 0; %myb不为0的点位跳变的前边缘_index else myf(ii) myb(ii); %myf不为0的点对应跳变的后边缘_index end end myd find(myb~0); %找到跳变的前边缘 MYE myb(myd); %对应列号 myh find(myf~0); %找到跳变的后边缘 myi myf(myh); MYB [1 myi]; for ii 1:length(MYE) partsumK(1,MYB(ii):MYE(ii)) sumPsum(part,2) end count 1; for ii 1:length(MYE) part1sumK(1,MYB(ii):MYE(ii)) sumP1sum(part1,2) if sumP1(1) (chang*kuan)*0.02 switch count case 1 axes(handles.axes9); case 2 axes(handles.axes12); case 3 axes(handles.axes13); case 4 axes(handles.axes14); case 5 axes(handles.axes15); case 6 axes(handles.axes16); otherwise axes(handles.axes17); end imshow(K1(:,MYB(ii):MYE(ii))); images_test1 imresize(K1(:,MYB(ii):MYE(ii)),[24 12]); images_test(:,count) double(reshape(images_test1,288,1)); count count1; end end handles.testnum images_test; guidata(hObject, handles); % --- Executes on button press in pushbutton9. function pushbutton9_Callback(hObject, eventdata, handles) % hObject handle to pushbutton9 (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) %% 字符识别 images_test_all handles.testnum; nnmain; for i 1:size(images_test_all,2) images_test double(images_test_all(:,i)); pred(i) predict(Theta1, Theta2, images_test); end chepai []; for i 1:size(pred,2) if pred(i)0 chepai [chepai,Name{pred(i)}]; end end % chepai(1) 苏; set(handles.text2,string,chepai); % --- Executes on button press in pushbutton10. function pushbutton10_Callback(hObject, eventdata, handles) % hObject handle to pushbutton10 (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) %% 这个按钮关闭软件 close all; clear; clc;3 仿真结果4 参考文献[1]王璐. 基于MATLAB的车牌识别系统研究[D]. 上海交通大学.博主简介擅长智能优化算法、神经网络预测、信号处理、元胞自动机、图像处理、路径规划、无人机等多种领域的Matlab仿真相关matlab代码问题可私信交流。部分理论引用网络文献若有侵权联系博主删除。