第 05 章 基于阈值分割的车牌定位识别-全套系统MATLAB智能驾驶深度学习
MATLAB智能驾驶深度学习
在汽车智能技术、汽车新能源技术、汽车电子这个群雄逐鹿的赛道。智能驾驶技术的要求也在不断的提升。
智能车辆教学平台、智能网联教学平台、汽车电子教学设备、在仿真应用开发部分,目前主流的系统有很多,常用的基础部分和算法开发,智能驾驶模块,较好的还是matlab平台。
本文从]图像的阈值分割技术进行图像识别和匹配展开讨论。
以终为始,先看效果。
车牌汉字库—参考
车牌字符库–参考
参考案例图像
GUI界面展示
成功实现
主函数GUI
function varargout = Gui_Main(varargin)
% GUI_MAIN MATLAB code for Gui_Main.fig
% GUI_MAIN, by itself, creates a new GUI_MAIN or raises the existing
% singleton*.
%
% H = GUI_MAIN returns the handle to a new GUI_MAIN or the handle to
% the existing singleton*.
%
% GUI_MAIN('CALLBACK',hObject,eventData,handles,...) calls the local
% function named CALLBACK in GUI_MAIN.M with the given input arguments.
%
% GUI_MAIN('Property','Value',...) creates a new GUI_MAIN or raises the
% existing singleton*. Starting from the left, property value pairs are
% applied to the GUI before Gui_Main_OpeningFcn gets called. An
% unrecognized property name or invalid value makes property application
% stop. All inputs are passed to Gui_Main_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 Gui_Main
% Last Modified by GUIDE v2.5 20-Apr-2011 14:40:49
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @Gui_Main_OpeningFcn, ...
'gui_OutputFcn', @Gui_Main_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_Main is made visible.
function Gui_Main_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_Main (see VARARGIN)
% Choose default command line output for Gui_Main
clc;
axes(handles.axes1); cla reset; box on; set(gca, 'XTickLabel', [], 'YTickLabel', []);
axes(handles.axes2); cla reset; box on; set(gca, 'XTickLabel', [], 'YTickLabel', []);
axes(handles.axes3); cla reset; box on; set(gca, 'XTickLabel', [], 'YTickLabel', []);
axes(handles.axes4); cla reset; box on; set(gca, 'XTickLabel', [], 'YTickLabel', []);
set(handles.text1, 'string', '');
handles.output = hObject;
handles.file = [];
handles.Plate = [];
handles.bw = [];
handles.words = [];
% Update handles structure
guidata(hObject, handles);
% UIWAIT makes Gui_Main wait for user response (see UIRESUME)
% uiwait(handles.figure1);
% --- Outputs from this function are returned to the command line.
function varargout = Gui_Main_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;
% --------------------------------------------------------------------
function uipushtool1_ClickedCallback(hObject, eventdata, handles)
% hObject handle to uipushtool1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
[filename, pathname] = uiputfile({'*.jpg;*.tif;*.png;*.gif','All Image Files';...
'*.*','All Files' }, '保存结果', ...
'Result\result.jpg');
if isempty(filename)
return;
end
file = fullfile(pathname, filename);
f = getframe(gcf);
f = frame2im(f);
imwrite(f, file);
msgbox('保存结果图像成功!', '提示信息', 'modal');
% --------------------------------------------------------------------
function uipushtool2_ClickedCallback(hObject, eventdata, handles)
% hObject handle to uipushtool2 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% 载入车牌图像
set(handles.text_result, 'string', '');
axes(handles.axes1); cla reset; box on; set(gca, 'XTickLabel', [], 'YTickLabel', []);
axes(handles.axes2); cla reset; box on; set(gca, 'XTickLabel', [], 'YTickLabel', []);
axes(handles.axes3); cla reset; box on; set(gca, 'XTickLabel', [], 'YTickLabel', []);
axes(handles.axes4); cla reset; box on; set(gca, 'XTickLabel', [], 'YTickLabel', []);
set(handles.text1, 'string', '');
handles.file = [];
handles.Plate = [];
handles.bw = [];
handles.words = [];
[filename, pathname, filterindex] = uigetfile({'*.jpg;*.tif;*.png;*.gif','All Image Files';...
'*.*','All Files' }, '选择待处理图像', ...
'images\car.jpg');
if filename == 0
return;
end
file = fullfile(pathname, filename);
Img = imread(file);
[y, ~, ~] = size(Img);
if y > 800
rate = 800/y;
Img1 = imresize(Img, rate);
else
Img1 = Img;
end
axes(handles.axes1);
imshow(Img1); title('原图像', 'FontWeight', 'Bold');
handles.Img = Img;
handles.file = file;
guidata(hObject, handles);
% --- 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)
if isempty(handles.file)
msgbox('请载入待检测图像', '提示信息', 'modal');
return;
end
[Plate, ~, Loc] = Pre_Process(handles.Img, [], 0);
axes(handles.axes1); hold on;
row = Loc.row;
col = Loc.col;
plot([col(1) col(2)], [row(1) row(1)], 'g-', 'LineWidth', 3);
plot([col(1) col(2)], [row(2) row(2)], 'g-', 'LineWidth', 3);
plot([col(1) col(1)], [row(1) row(2)], 'g-', 'LineWidth', 3);
plot([col(2) col(2)], [row(1) row(2)], 'g-', 'LineWidth', 3);
hold off;
axes(handles.axes2); cla reset; box on; set(gca, 'XTickLabel', [], 'YTickLabel', []);
imshow(Plate, []); title('车牌区域图像', 'FontWeight', 'Bold');
handles.Plate = Plate;
guidata(hObject, handles);
% --- If Enable == 'on', executes on mouse press in 5 pixel border.
% --- Otherwise, executes on mouse press in 5 pixel border or over pushbutton1.
function pushbutton1_ButtonDownFcn(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)
% --- Executes on mouse press over axes background.
function axes1_ButtonDownFcn(hObject, eventdata, handles)
% hObject handle to axes1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% --- Executes on mouse press over axes background.
function axes2_ButtonDownFcn(hObject, eventdata, handles)
% hObject handle to axes2 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% --- Executes on mouse press over axes background.
function axes3_ButtonDownFcn(hObject, eventdata, handles)
% hObject handle to axes3 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% --- Executes on mouse press over axes background.
function axes4_ButtonDownFcn(hObject, eventdata, handles)
% hObject handle to axes4 (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 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)
if isempty(handles.Plate)
msgbox('请先进行车牌区域标定操作!', '提示信息', 'modal');
return;
end
bw = Plate_Process(handles.Plate, 0);
axes(handles.axes3); cla reset; box on; set(gca, 'XTickLabel', [], 'YTickLabel', []);
imshow(bw, []); title('车牌区域二值图像', 'FontWeight', 'Bold');
handles.bw = bw;
guidata(hObject, handles);
% --- 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)
if isempty(handles.bw)
msgbox('请先进行车牌区域二值化操作!', '提示信息', 'modal');
return;
end
bw = Segmation(handles.bw);
words = Main_Process(bw, 0);
axes(handles.axes4); cla reset; box on; set(gca, 'XTickLabel', [], 'YTickLabel', []);
words_display = [words.word1 words.word2 words.word3 ...
words.word4 words.word5 words.word6 words.word7];
imshow(words_display, []); title('车牌字符图像', 'FontWeight', 'Bold');
handles.words = words;
guidata(hObject, handles);
% --- 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)
if isempty(handles.words)
msgbox('请先进行车牌区域字符分割操作!', '提示信息', 'modal');
return;
end
str = Pattern_Recognition(handles.words);
set(handles.text_result, 'string', str);
局部函数区域
Pattern_Recognition.m
function str = Pattern_Recognition(words)
pattern = [];
dirpath = fullfile(pwd, '标准库/*.bmp');
files = ls(dirpath);
for t = 1 : length(files)
filenamet = fullfile(pwd, '标准库', files(t,:));
[~, name, ~] = fileparts(filenamet);
imagedata = imread(filenamet);
imagedata = im2bw(imagedata, 0.5);
pattern(t).feature = imagedata;
pattern(t).name = name;
end
distance = [];
for m = 1 : 7;
for n = 1 : length(files);
switch m
case 1
distance(n)=sum(sum(abs(words.word1-pattern(n).feature)));
case 2
distance(n)=sum(sum(abs(words.word2-pattern(n).feature)));
case 3
distance(n)=sum(sum(abs(words.word3-pattern(n).feature)));
case 4
distance(n)=sum(sum(abs(words.word4-pattern(n).feature)));
case 5
distance(n)=sum(sum(abs(words.word5-pattern(n).feature)));
case 6
distance(n)=sum(sum(abs(words.word6-pattern(n).feature)));
case 7
distance(n)=sum(sum(abs(words.word7-pattern(n).feature)));
end
end
[yvalue,xnumber]=min(distance);
filename = files(xnumber, :);
[pathstr, name, ext] = fileparts(filename);
result(m) = name;
end
str = ['识别结果为:' result];
msgbox(str, '车牌识别', 'modal');
str = result;
图像分割函数
Segmation.m
function e = Segmation(d)
[m, n] = size(d);
top = 1;
bottom = m;
left = 1;
right = n;
while sum(d(top,:))==0 && top<=m
top = top + 1;
end
while sum(d(bottom,:))==0 && bottom>=1
bottom = bottom - 1;
end
while sum(d(:,left))==0 && left<=n
left = left + 1;
end
while sum(d(:,right))==0 && right>=1
right = right - 1;
end
dd = right - left;
hh = bottom - top;
e = imcrop(d, [left top dd hh]);
全套文件压缩包整理–>传送门
对于参考的样例,可能有几个识别不出来,需要进行自定义修改,看情况考虑,。