【车牌识别】基于matlab GUI模板匹配车牌库识别【含Matlab源码 416期】

一、简介

基于matlab GUI车牌库识别:
该设计主要研究基于MATLAB软件的汽车号牌设别系统设计,系统主要包括图像采集、图像预处理、车牌定位、字符分割、字符识别五大核心部分。系统的图像预处理模块是将图像经过图像灰度化、图像增强、边缘提取、二值化等操作,转换成便于车牌定位的二值化图像;利用车牌的边缘、形状等特征,再结合Roberts 算子边缘检测、数字图像、形态学等技术对车牌进行定位;字符的分割采用的方法是将二值化后的车牌部分进行寻找连续有文字的块,若长度大于设定的阈值则切割,从而完成字符的分割;字符识别运用模板匹配算法完成。以上每个功能模块用MATLAB软件实现,最后识别出车牌,在研究设计的同时对其中出现的问题进行具体分析、处理,并寻求更优的方法。

二、源代码

close all;
clc

[fn,pn,fi] = uigetfile('*.jpg','请选择所要识别的图片');    
I = imread([pn fn]); % 读取图像 参数为图像名称和图像路径
figure,imshow(I);                   
title('原始图像'); %显示原始图像 

figure(1),imshow(I);title('原图');%显示原始图像
I1=rgb2gray(I);%将彩图转换位灰度图I1
figure(2),subplot(1,2,1),imshow(I1);title('灰度图');%创建显示I1图像,做图1的坐标轴
figure(2),subplot(1,2,2),imhist(I1);title('灰度直方图');%创建显示I1的柱状图,做图2的坐标轴
I2=edge(I1,'roberts',0.16,'both');
figure(3),subplot(2,2,1),imshow(I2);title('roberts算子边缘检测')
se=[1;1;1];
I3=imerode(I2,se);
figure(3),subplot(2,2,2),imshow(I3);title('腐蚀后图像');
se=strel('rectangle',[25,25]);
I4=imclose(I3,se);
figure(3),subplot(2,2,3),imshow(I4);title('平滑图像的轮廓');
I5=bwareaopen(I4,2000);
figure(3),subplot(2,2,4),imshow(I5);title('从对象中移除小对象');


%------车牌定位,并得到车牌的上边界PY1、下边界PY2、左边界PX1、右边界 PX2
[PY2,PY1,PX2,PX1] = Dingwei_Chepai(I);                     % 1.定位车牌边界

%------车牌修正,将车牌轮廓稍微扩张                             % 2.修正边界
[PY2,PY1,PX2,PX1,threshold] = Xiuzheng_Chepai(PY2,PY1,PX2,PX1);

%------框选出车牌信息,得到车牌图像
bw = I(PY1:PY2,PX1:PX2,:);                                   % 3.裁剪车牌照
figure,subplot(121),imshow(bw); 
title('车牌图像'); 

%------车牌图像灰度处理                                        % 4. 灰度处理
bw = rgb2gray(bw); % 车牌灰度图像
subplot(122),imshow(bw); % 显示车牌灰度图像
title('灰度图像'); % 图像标题

% I = bw;
% [m n]=size(I); % m n分别为图像的行 列数
%--- 进行垂直灰度投影
% for y=1:n
%      V(y)=sum(I(1:m,y));
% end
% y=1:n;
% figure
% subplot(211), plot(y,V(y));
% title('垂直灰度投影');
% % 进行水平灰度投影
% for x=1:m
%     L(x)=sum(I(x,:));
% end
% x=1:m;
% subplot(212),plot(x,L(x));
% title('水平灰度投影'); 

%------对车牌倾斜位置矫正                                 
qingxiejiao = rando_bianhuan(bw);                       % 5. 获得图像倾斜角 
bw = imrotate(bw,qingxiejiao,'bilinear','crop');               % 6.位置矫正
%图像进行位置矫正:取值为负值向右旋转 并选区双线性插值 并输出同样尺寸的图像
figure,subplot(121),imshow(bw); % 显示修正后的图像
title('倾斜校正');

%------转化为二值化图像
bw = imbinarize(bw,graythresh(bw));                          % 7.二值化处理
subplot(122), imshow(bw);
title('二值图像');

%------形态学处理
bw = Xingtaixue_Chuli(bw,threshold);                         % 8.形态学操作

%------裁剪使得字体紧贴边界
bw = touying(bw);                                         % 9. 贴近字符裁剪
%对图像进一步裁剪,保证边框贴近字体
figure,subplot(121),imshow(bw);
title('上下边界裁剪');
bw=~bw; 
bw = bwareaopen(bw, threshold); 
% 移除小面积对象函数  删除二值图像BW中面积小于threshold的对象
bw=~bw;                                                         % 10. 擦除
subplot(122),imshow(bw);
title('擦除');

%------分割车牌字符
[y,x] = size(bw);         % 11. 单个字符边界分割,并将边界信息赋值与fenge数组中
fenge = shuzifenge(bw,qingxiejiao);

%----拆分过的车牌字符                         % 12. 利用fenge数组显示七个字符
[m,k]=size(fenge);
for s=1:2:k-1
    subplot(1,k/2,(s+1)/2);
    imshow(bw( 1:y,fenge(s):fenge(s+1)));
end

function varargout = GUI(varargin)

gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @GUI_OpeningFcn, ...
                   'gui_OutputFcn',  @GUI_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 GUI is made visible.
function GUI_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 GUI (see VARARGIN)

% Choose default command line output for GUI
handles.output = hObject;

global I
global Icp
global threshold
global qingxiejiao
global xiuzhenghanzi 
global xiuzhengzimu
global xiuzhengzm_sz_1
global xiuzhengzm_sz_2 
global xiuzhengshuzi_1
global xiuzhengshuzi_2
global xiuzhengshuzi_3 

% Update handles structure
guidata(hObject, handles);

% UIWAIT makes GUI wait for user response (see UIRESUME)
% uiwait(handles.figure1);


% --- Outputs from this function are returned to the command line.
function varargout = GUI_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 duqu.
function duqu_Callback(hObject, eventdata, handles)

global I
global Img_0
global Img_1

axes(handles.YuanTuXiang)
[fn,pn,~] = uigetfile('*.jpg','请选择所要识别的图片');    
I = imread([pn fn]); % 读取图像 参数为图像名称和图像路径               =====1
% =========================================================================
Img_0 = rgb2gray(I); %*****灰度处理                                  =====2
Img_1 = edge(Img_0,'roberts');    %********roberts算子边缘检测       =====3
% =========================================================================
imshow(I)
title('原图像');

function Huidu_Callback(hObject, eventdata, handles)
% hObject    handle to Huidu (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
global Img_0
axes(handles.Hui)
imshow(Img_0)
title('灰度处理');

function Zhifang_Callback(hObject, eventdata, handles)
% hObject    handle to Zhifang (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
global Img_0
axes(handles.Zhi)
imhist(Img_0)
title('直方图');

% --- Executes on button press in Bianyuanjiance.
function Bianyuanjiance_Callback(hObject, eventdata, handles)
% hObject    handle to Bianyuanjiance (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
global Img_1
axes(handles.Bian)
imshow(Img_1)
title('边缘检测');

% --- Executes on button press in chepaidingwei.
function chepaidingwei_Callback(hObject, eventdata, handles)

global I
global Icp
global threshold
global qingxiejiao
global bw
global Img_2
global Img_3
global bw_0
global bw0
global bw1
global bw2

% =========================================================================
[PY2,PY1,PX2,PX1] = Dingwei_Chepai(I);                     % 1.定位车牌边界

%------车牌修正,将车牌轮廓稍微扩张                             % 2.修正边界
[PY2,PY1,PX2,PX1,threshold] = Xiuzheng_Chepai(PY2,PY1,PX2,PX1);

%------框选出车牌信息,得到车牌图像
I = I(PY1:PY2,PX1:PX2,:); 
Img_2 = I;                                        %************预定位的车牌
% =========================================================================
bw = rgb2gray(I);  %车牌灰度处理

qingxiejiao = rando_bianhuan(bw);  
Icp = imrotate(I,qingxiejiao,'bilinear','crop'); 
Img_3 = Icp;                                          %********矫正后的车牌
% =========================================================================
% =========================================================================

%------转化为二值化图像
bw = im2bw(Icp,graythresh(Icp));                           %******二值图像
bw_0 = bw;
% =========================================================================
%------形态学处理:断开H连接,移除毛刺,先腐蚀后膨胀,擦除,取反
bw = Xingtaixue_Chuli(bw,threshold);
bw0 = bw;                                                 %******形态学处理
% =========================================================================
%------对图像进一步裁剪,保证边框贴近字体
bw = touying(bw);                                        %******定位剪切
bw1 = bw;
% =========================================================================
% -----移除小面积对象函数  删除二值图像BW中面积小于threshold的对象
bw=~bw; 
bw = bwareaopen(bw, threshold); 
bw=~bw;   
bw2 =bw;                                                %******移除噪声
% =========================================================================
% =========================================================================
axes(handles.Yu) 
imshow(Img_2);
title('车牌预定位','color','b');

% --- Executes on button press in pushbutton21.
function pushbutton21_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton21 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
% --- Executes on button press in ZifuFeng.
global Img_3
axes(handles.Jiao) 
imshow(Img_3);
title('矫正车牌','color','b');

% --- Executes on button press in pushbutton23.
function pushbutton23_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton23 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
global Icp

I = rgb2gray(Icp);
[m n]=size(I); % m n分别为图像的行 列数--- 进行垂直灰度投影
for y=1:n
     V(y)=sum(I(1:m,y));
end

三、运行结果

【车牌识别】基于matlab GUI模板匹配车牌库识别【含Matlab源码 416期】_第1张图片

四、备注

完整代码或者代写添加QQ 1564658423

你可能感兴趣的:(matlab,图像处理)