1. 程式人生 > 其它 >【車牌識別】基於matlab GUI閾值分割車牌定位識別【含Matlab原始碼 721期】

【車牌識別】基於matlab GUI閾值分割車牌定位識別【含Matlab原始碼 721期】

一、簡介

1 閾值分割原理:
一副影象包括目標、背景和噪聲,設定某一閾值T將影象分成兩部分:大於T的畫素群和小於T的畫素群。

在實際處理時候,為了顯示需要一般用255表示背景,用0表示物件物。
由於實際得到的影象目標和背景之間不一定單純地分佈在兩個灰度範圍內,此時就需要兩個或以上的閾值來提取目標。

影象閾值化分割是一種傳統的最常用的影象分割方法,因其實現簡單、計算量小、效能較穩定而成為影象分割中最基本和應用最廣泛的分割技術。它特別適用於目標和背景佔據不同灰度級範圍的影象。難點在於如何選擇一個合適的閾值實現較好的分割。

2 最大方差閾值

最大方差閾值的基本思想是:把直方圖在某一閾值處分割成兩組,當被分成的的兩組之間方差最大時,決定閾值。
直方圖是影象的一種統計表達,由一系列高度不等的縱向條紋表示資料分佈的情況。灰度直方圖是灰度級的函式,它表示圖象中具有每種灰度級的象素的個數,反映圖象中每種灰度出現的頻率( 即影象中0~255每個畫素點的 個數統計)。
灰度直方圖描述了影象中各種灰度(對於畫素深度為 8 位的影象,共為 0-255 共256 種取值)在整個影象中佔有的 比例。
如下圖所示,灰度直方圖的 橫座標是 灰度級, 縱座標是該灰度級出現的 頻率,是圖象的最基本的統計特徵。

3 雙峰法選擇閾值
雙峰法的原理認為影象由前景和背景或者兩族顏色組成,在灰度直方圖上,兩族顏
色畫素灰度值的分佈形成山峰狀態。在雙峰之間的最低谷處就是影象分割的閾值所
在,根據這一原理可以簡單算出閾值,進行影象分割。Show一下直方圖即可確定閾
值。但是這種方法容易丟失一些影象細節
即下圖中,以Zt為閾值進行二值化分割,可以將目標和背景分割開。

4 迭代法選取閾值

4.1 求出圖象的最大灰度值和最小灰度值,分別記為Pmax和Pmin,令初始閾值T0=(Pmax+Pmin)/2;
4.2 根據閾值T(k)(k=0,1,2...,k)將圖象分割為前景和背景,分別求出兩者的平均灰度值H1和H2;
4.3 求出新閾值T(k+1)=(H1+H2)/2;
4.4 若T(k)=T(k+1),則所得即為閾值;否則轉2,迭代計算

5 大津法選擇閾值

Otsu實現思路
5.1 計算0~255各灰階對應的畫素個數,儲存至一個數組中,該陣列下標是灰度值,儲存內容是當前灰度值對應畫素數
5.2 計算背景影象的平均灰度、背景影象畫素數所佔比例
5.3 計算前景影象的平均灰度、前景影象畫素數所佔比例
5.4 遍歷0~255各灰階,計算並尋找類間方差極大值

大津法是屬於最大類間方差法,它是自適應計算單閾值的簡單高效方法,或者叫(Otsu)大津法由大津於1979年提出,對影象Image,記t為前景與背景的分割閾值,前景點數佔影象比例為w0,平均灰度為u0;背景點數佔影象比例為w1,平均灰度為u1。影象的總平均灰度為:u=w0u0+w1u1。從最小灰度值到最大灰度值遍歷t,當t使得值g=w0(u0-u)2+w1(u1-u)2 最大時t即為分割的最佳閾值。對大津法可作如下理解:
該式實際上就是類間方差值,閾值t分割出的前景和背景兩部分構成了整幅影象,而前景取值u0,概率為 w0,背景取值u1,概率為w1,總均值為u,根據方差的定義即得該式。因方差是灰度分佈均勻性的一種度量,方差值越大,說明構成影象的兩部分差別越大, 當部分目標錯分為背景或部分背景錯分為目標都會導致兩部分差別變小,因此使類間方差最大的分割意味著錯分概率最小。直接應用大津法計算量較大,因此一般採用了等價的公式g=w0w1

(u0-u1)2。

6 由灰度拉伸選擇閾值
大津法是較通用的方法,但是它對兩群物體在灰度不明顯的情況下會丟失一些整體資訊。因此為了解決這種現象採用灰度拉伸的增強大津法。在大津法的思想上增加灰度的級數來增強前兩群物體的灰度差。對於原來的灰度級乘上同一個係數,從而擴大了影象灰度的級數。試驗結果表明不同的拉伸係數,分割效果差別比較大。

二、原始碼

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-Jun-2020 15:07:10

% 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'); 


% --------------------------------------------------------------------
% --- 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)
% 載入車牌影象
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);



% --- 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

三、執行結果

四、備註

版本:2014a
完整程式碼或代寫加1564658423