【目標檢測】基於matlab GUI差分法運動目標檢測【含Matlab原始碼 1284期】

語言: CN / TW / HK

一、運動目標檢測簡介

0 引言 近些年來,運動目標的檢測,尤其是運動車輛的檢測的發展和應用非常廣泛。運動目標檢測就是從影象序列中檢測出運動目標。目前,運動目標的檢測主要方法有背景差分法、幀間差分法、光流法3種。本文對影片中動態場景的運動車輛進行實時檢測,解決目標輪廓缺失、背景無法實時更新的問題。

1 幀間差分法 1.1 二幀間差分法 幀間差分法是基於運動影象序列中相鄰兩幀影象間具有強相關性而提出的檢測方法,這種檢測方法對光照變化不敏感,非常適合動態變化的環境,而且運算簡單,檢測速度快,車輛定位準確,適用於實時性要求較高的場景。

傳統的幀間差分法,即二幀間差分法的基本原理如下: 在這裡插入圖片描述 式中:Gk(x,y)——結果影象;Pk(x,y)——第k幀影象;Pk-1(x,y)——第k-1幀影象。 由於攝像機採集的影片影象具有連續性的特點,如果場景內不存在運動的物體,則連續幀之間的變化很微弱,如果存在運動目標,則變化較明顯。二幀間差分法演算法簡單,受光線影響小,但它仍然存在以下幾個缺點:首先,它不能檢測出靜止或運動速度過慢的物體,對於高速運動的物體又會使得分割區域遠遠大於真實目標,其分割區域與目標運動速度相關;其次,如果物體內部的灰度比較均勻,相鄰幀差可能在目標重疊部分形成空洞,嚴重時造成分割結果不連通,不利於進一步的物體分析與識別。 因此,本文提出了改進的三幀間差分法來解決傳統幀間差分法中存在的問題。

1.2 三幀間差分法 當目標物體運動速度過快時,二幀間差分法所得影象便會出現重影、目標區域過大等問題,因此,提出了三幀間差分法來改善影象的處理效果。其基本原理是把原來利用兩幀獲取結果影象的方法,改進為利用相鄰三幀來實現目標的檢測。其流程圖如圖1所示。 在這裡插入圖片描述 圖1 三幀間差分法流程圖 首先,分別得到第k幀與k-1幀的差分圖和第k+1幀與k幀的差分圖,然後將灰度影象轉化為二值影象,最後將所得的二值差值圖進行“與”運算得到最終的結果。 其公式原理如下: 在這裡插入圖片描述 式中:Gk(x,y)——結果影象;Pk(x,y)——第k幀影象;Pk-1(x,y)——第k-1幀影象;Pk+1(x,y)——第k+1幀影象。

在幀間差分法中,閾值T的選擇非常重要。如果閾值T選取的值太小,則無法抑制差分影象中的噪聲;如果閾值T選取的值太大,又有可能掩蓋差分影象中目標的部分資訊;而且,固定的閾值T無法適應場景中光線變化等情況。

為了使運動目標與背景之間的差異更加明顯,使用最大類間方差法(也叫Otsu演算法)來實現這一目標。該演算法是在灰度直方圖的基礎上採用最小二乘法原理推匯出來的,具有統計意義上的最佳分割。它的基本原理是以最佳閾值將影象的灰度值分割成兩部分,使兩部分之間的方差最大,即具有最大的分離性。

2 背景差分法 背景差分法首先選取背景中的一幅或幾幅影象的平均作為背景影象,然後把以後的序列影象當前幀和背景影象相減,進行背景消去。若所得到的畫素數大於某一閾值,則為運動目標區域,反之則為背景區域。其對複雜背景下的運動物體檢測效果較好,計算量小。缺點是受光線、天氣等外界條件的影響較大。

2.1 混合高斯背景建模 混合高斯背景建模是基於畫素樣本統計資訊的背景表示方法,利用畫素在較長時間內大量樣本值的概率密度等統計資訊(如模式數量、每個模式的均值和標準差)表示背景,然後使用統計差分(如3σ原則)進行目標畫素判斷,可以對複雜動態背景進行建模,計算量較大。

在混合高斯背景模型中,認為畫素之間的顏色資訊互不相關,對各畫素點的處理都是相互獨立的。對於影片影象中的每一個畫素點,其值在序列影象中的變化可看作是不斷產生畫素值的隨機過程,即用高斯分佈來描述每個畫素點的顏色呈現規律(單模態(單峰),多模態(多峰))。

對於多峰高斯分佈模型,影象的每一個畫素點按不同權值的多個高斯分佈的疊加來建模,每種高斯分佈對應一個可能產生畫素點所呈現顏色的狀態,各個高斯分佈的權值和分佈引數隨時間更新。當處理彩色影象時,假定影象畫素點R,G,B三色通道相互獨立並具有相同的方差。對於隨機變數X的觀測資料集{x1,x2,…,xN},xt=(rt,gt,bt)為t時刻畫素的樣本,則單個取樣點xt其服從的混合高斯分佈概率密度函式: 在這裡插入圖片描述 式中:k——分佈模式總數;η(xt,μi,t,τi,t)——t時刻第i個高斯分佈;μi,t——其均值;τi,t——其協方差矩陣;δi,t——方差;I——三維單位矩陣;wi,t——t時刻第i個高斯分佈的權重。

2.2 引數設定 每個新畫素值xt同當前k個模型按式(8)進行比較,直到找到匹配新畫素值的分佈模型,即同該模型的均值偏差在2.5σ內: 在這裡插入圖片描述 若所匹配的模式符合背景要求,則該畫素屬於背景,否則屬於前景。各模式權值按式(9)更新,其中α是學習速率,對於匹配的模式Mk,t=1,否則Mk,t=0,然後各模式使得權重進行歸一化: 在這裡插入圖片描述 若未匹配成功,其均值μ和標準差σ均不變,匹配模式按式(10)—式(12)更新: 在這裡插入圖片描述

二、部分原始碼

```c function varargout = object_detect(varargin) % OBJECT_DETECT MATLAB code for object_detect.fig % OBJECT_DETECT, by itself, creates a new OBJECT_DETECT or raises the existing % singleton. % % H = OBJECT_DETECT returns the handle to a new OBJECT_DETECT or the handle to % the existing singleton. % % OBJECT_DETECT('CALLBACK',hObject,eventData,handles,...) calls the local % function named CALLBACK in OBJECT_DETECT.M with the given input arguments. % % OBJECT_DETECT('Property','Value',...) creates a new OBJECT_DETECT or raises the % existing singleton. Starting from the left, property value pairs are % applied to the GUI before object_detect_OpeningFcn gets called. An % unrecognized property name or invalid value makes property application % stop. All inputs are passed to object_detect_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 object_detect

% Last Modified by GUIDE v2.5 16-May-2021 08:41:00

% Begin initialization code - DO NOT EDIT gui_Singleton = 1; gui_State = struct('gui_Name', mfilename, ... 'gui_Singleton', gui_Singleton, ... 'gui_OpeningFcn', @object_detect_OpeningFcn, ... 'gui_OutputFcn', @object_detect_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 object_detect is made visible. function object_detect_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 object_detect (see VARARGIN)

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

% Update handles structure guidata(hObject, handles);

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

% --- Outputs from this function are returned to the command line. function varargout = object_detect_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, pathname] = uigetfile({'.avi'; '.mp4'},'開啟影片'); str = [pathname filename]; set(handles.edit1, 'String', str);

function edit1_Callback(hObject, eventdata, handles) % hObject handle to edit1 (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 edit1 as text % str2double(get(hObject,'String')) returns contents of edit1 as a double

% --- Executes during object creation, after setting all properties. function edit1_CreateFcn(hObject, eventdata, handles) % hObject handle to edit1 (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 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) videoName = get(handles.edit1, 'String'); videoSource = vision.VideoFileReader(videoName,... 'ImageColorSpace', 'RGB', 'VideoOutputDataType', 'uint8'); videoInfo = info(videoSource); videoRate = videoInfo.VideoFrameRate; waitTime = 1.0/videoRate; frame_last = rgb2gray(step(videoSource)); count = 1 global exit_flag; global pause_flag; exit_flag = false; pause_flag = false;

while ~isDone(videoSource) && ~exit_flag if pause_flag uiwait(handles.figure1); end frame = step(videoSource); frame_now = rgb2gray(frame); frame_now = medfilt2(frame_now); frame_diff = abs(frame_now - frame_last); fgMask = imbinarize(frame_diff); fgMask = imopen(fgMask, strel('rectangle', [3, 3])); fgMask = imfill(fgMask, 'holes'); frame_last = frame_now; axes(handles.axes1); imshow(frame); axes(handles.axes2); imshow(fgMask); pause(waitTime - 0.02); count = count + 1 end release(videoSource);

% --- 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) global exit_flag; exit_flag = true;

% --- 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) global pause_flag; pause_flag = true;

% --- 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) global pause_flag; pause_flag = false; uiresume(handles.figure1);

% --- 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) videoName = get(handles.edit1, 'String'); videoSource = vision.VideoFileReader(videoName,... 'ImageColorSpace', 'RGB', 'VideoOutputDataType', 'uint8'); videoInfo = info(videoSource); videoRate = videoInfo.VideoFrameRate; waitTime = 1.0/videoRate; frame_first = rgb2gray(step(videoSource)); frame = step(videoSource); global exit_flag; global pause_flag; exit_flag = false; pause_flag = false; while ~isDone(videoSource) && ~exit_flag if pause_flag uiwait(handles.figure1); end % 顯示該幀影象 axes(handles.axes1); imshow(frame); frame_second = rgb2gray(frame); frame = step(videoSource); frame_third = rgb2gray(frame); frame_diff1 = abs(frame_second - frame_first); frame_diff2 = abs(frame_third - frame_second); fgMask = imbinarize(min(frame_diff1,frame_diff2)); fgMask = imopen(fgMask, strel('rectangle', [3, 3])); fgMask = imfill(fgMask, 'hole'); frame_first = frame_second; axes(handles.axes2); imshow(fgMask); pause(waitTime - 0.02); end release(videoSource);

% --- 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) close(gcf);

% --- 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) videoName = get(handles.edit1, 'String'); videoSource = vision.VideoFileReader(videoName,... 'ImageColorSpace', 'RGB', 'VideoOutputDataType', 'uint8'); videoInfo = info(videoSource); videoRate = videoInfo.VideoFrameRate; waitTime = 1.0/videoRate; global exit_flag; global pause_flag; exit_flag = false; pause_flag = false; ```

三、執行結果

在這裡插入圖片描述 在這裡插入圖片描述

四、matlab版本及參考文獻

1 matlab版本 2014a

2 參考文獻 [1] 蔡利梅.MATLAB影象處理——理論、演算法與例項分析[M].清華大學出版社,2020. [2]楊丹,趙海濱,龍哲.MATLAB影象處理例項詳解[M].清華大學出版社,2013. [3]周品.MATLAB影象處理與圖形使用者介面設計[M].清華大學出版社,2013. [4]劉成龍.精通MATLAB影象處理[M].清華大學出版社,2015. [5]韓帥.基於改進幀間差分法與背景差分法車輛檢測[J].農業裝備與車輛工程. 2021,59(06)

「其他文章」