日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

【目标检测】基于帧差法+Vibe算法实现车辆行人检测matlab源码

發布時間:2023/12/9 编程问答 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 【目标检测】基于帧差法+Vibe算法实现车辆行人检测matlab源码 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

一、幀差分法

??? 攝像機采集的視頻序列具有連續性的特點。如果場景內沒有運動目標,則連續幀的變化很微弱,如果存在運動目標,則連續的幀和幀之間會有明顯地變化。

1.1? 兩幀差分法

??? 幀間差分法(Temporal Difference)就是借鑒了上述思想。由于場景中的目標在運動,目標的影像在不同圖像幀中的位置不同。該類算法對時間上連續的兩幀或三幀圖像進行差分運算,不同幀對應的像素點相減,判斷灰度差的絕對值,當絕對值超過一定閾值時,即可判斷為運動目標,從而實現目標的檢測功能。

??????????????????????

??? 兩幀差分法的運算過程如圖2-2所示。記視頻序列中第n幀和第n?1幀圖像為fn和fn?1,兩幀對應像素點的灰度值記為fn(x,y)和fn?1(x?,?y),按照式2.13將兩幀圖像對應像素點的灰度值進行相減,并取其絕對值,得到差分圖像Dn:

??????????????????????????????????????????????????????????????????????????????????

????設定閾值T,按照式2.14逐個對像素點進行二值化處理,得到二值化圖像Rn'。其中,灰度值為255的點即為前景(運動目標)點,灰度值為0的點即為背景點;對圖像Rn'進行連通性分析,最終可得到含有完整運動目標的圖像Rn。

?????????????????????????????????????? ??

1.2 三幀差分法

??? 兩幀差分法適用于目標運動較為緩慢的場景,當運動較快時,由于目標在相鄰幀圖像上的位置相差較大,兩幀圖像相減后并不能得到完整的運動目標,因此,人們在兩幀差分法的基礎上提出了三幀差分法。

????????????

???三幀差分法的運算過程如圖2-3所示。記視頻序列中第n+1幀、第n幀和第n?1幀的圖像分別為fn+1、fn和fn?1,三幀對應像素點的灰度值記為fn+1(x , y)?、fn(x , y)?和fn?1(x , y) ,?按照式2.13分別得到差分圖像Dn+1和Dn,對差分圖像Dn+1和Dn按照式2.15進行與操作,得到圖像Dn',然后再進行閾值處理、連通性分析,最終提取出運動目標。?

???????????????????

??? 在幀間差分法中,閾值?T?的選擇非常重要。如果閾值T選取的值太小,則無法抑制差分圖像中的噪聲;如果閾值T選取的值太大,又有可能掩蓋差分圖像中目標的部分信息;而且固定的閾值T無法適應場景中光線變化等情況。為此,有人提出了在判決條件中加入對整體光照敏感的添加項的方法,將判決條件修改為:

????????????????????????????? ?

??? 其中,?N?A為待檢測區域中像素的總數目,λ為光照的抑制系數,A可設為整幀圖像。添加項表達了整幀圖像中光照的變化情況。如果場景中的光照變化較小,則該項的值趨向于零;如果場景中的光照變化明顯,則該項的值明顯增大,導致式2.16右側判決條件自適應地增大,最終的判決結果為沒有運動目標,這樣就有效地抑制了光線變化對運動目標檢測結果的影響。

??? 圖?2-5?是采用幀間差分法對自拍序列?lab?序列進行運動目標檢測的實驗結果,(b)圖是采用兩幀差分法的檢測結果,(c)圖是采用三幀差分法的檢測結果。lab序列中的目標運動較快,在這種情況下,運動目標在不同圖像幀內的位置明顯不同,采用兩幀差分法檢測出的目標會出現“重影”的現象,采用三幀差分法,可以檢測出較為完整的運動目標。

?? ?????????????

??? 綜上所述,幀間差分法的原理簡單,計算量小,能夠快速檢測出場景中的運動目標。但由實驗結果可以看出,幀間差分法檢測的目標不完整,內部含有“空洞”,這是因為運動目標在相鄰幀之間的位置變化緩慢,目標內部在不同幀圖像中相重疊的部分很難檢測出來。幀間差分法通常不單獨用在目標檢測中,往往與其它的檢測算法結合使用。

二、混合高斯建模法

高斯模型有單高斯模型(SGM)和混合高斯模型(GMM)兩種。

(1)單高斯模型:

為簡單起見,閾值t的選取一般靠經驗值來設定。通常意義下,我們一般取t=0.7-0.75之間。

二維情況如下所示:

(2)混合高斯模型:

??? ? 對于(b)圖所示的情況,很明顯,單高斯模型是無法解決的。為了解決這個問題,人們提出了高斯混合模型(GMM),顧名思義,就是數據可以看作是從數個高斯分布中生成出來的。雖然我們可以用不同的分布來隨意地構造 XX Mixture Model ,但是 GMM是 最為流行。另外,Mixture Model 本身其實也是可以變得任意復雜的,通過增加 Model 的個數,我們可以任意地逼近任何連續的概率密分布。

??? 每個 GMM 由 K 個 Gaussian 分布組成,每個 Gaussian 稱為一個“Component”,這些 Component 線性加成在一起就組成了 GMM 的概率密度函數:

? ? ? ? ? ? ? ? (1)

其中,πk表示選中這個component部分的概率,我們也稱其為加權系數。

根據上面的式子,如果我們要從 GMM 的分布中隨機地取一個點的話,實際上可以分為兩步:

(1)首先隨機地在這?K?個 Component 之中選一個,每個 Component 被選中的概率實際上就是它的系數?πk,選中了 Component 之后,再單獨地考慮從這個 Component 的分布中選取一個點就可以了──這里已經回到了普通的 Gaussian 分布,轉化為了已知的問題。假設現在有?N?個數據點,我們認為這些數據點由某個GMM模型產生,現在我們要需要確定?πk,μk,σk?這些參數。很自然的,我們想到利用最大似然估計來確定這些參數,GMM的似然函數如下:

? ? ? ? (2)

在最大似然估計里面,由于我們的目的是把乘積的形式分解為求和的形式,即在等式的左右兩邊加上一個log函數,但是由上文博客里的(2)式可以看出,轉化為log后,還有log(a+b)的形式,因此,要進一步求解。

三、仿真代碼

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-2018 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}); endif nargout[varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:}); elsegui_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_flagif pause_flaguiwait(handles.figure1);endframe = 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_flagif pause_flaguiwait(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;% 已經匹配,則不需要再進行優先級排序判定if match_groundfgMask(i, j) = 0;continue;end% 優先級排序rank = W(i, j, :) ./ sd(i, j, :);[~, rank_index] = sort(rank, 'descend');fgMask(i, j) = 0;k = 1;temp_T = 0;while (match == 0) && (temp_T < T)index = rank_index(k);if abs(u_dist(i, j, index) <= D * sd(i, j, index))fgMask(i, j) = 0;break;elsetemp_T = temp_T + W(i, j, index);fgMask(i, j) = 255;endk = k + 1;endendend% 使用混合高斯背景建模fgMask = logical(fgMask);fgMask = imdilate(imerode(fgMask, disk1), disk2);axes(handles.axes1);imshow(frame);axes(handles.axes2);imshow(fgMask);drawnow;frame_count = frame_count + 1 end release(videoSource);% videoName = get(handles.edit1, 'String'); % videoSource = vision.VideoFileReader(videoName,... % 'ImageColorSpace', 'RGB', 'VideoOutputDataType', 'uint8'); % detector = vision.ForegroundDetector(... % 'NumTrainingFrames', 5, ... % 'InitialVariance', 30 * 30); % videoInfo = info(videoSource); % videoRate = videoInfo.VideoFrameRate; % waitTime = 1.0/videoRate; % 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); % fgMask = step(detector, frame_now); % fgMask = imopen(fgMask, strel('rectangle', [3, 3])); % fgMask = imfill(fgMask, 'holes'); % axes(handles.axes1); % imshow(frame); % axes(handles.axes2); % imshow(fgMask); % pause(waitTime - 0.02); % end % release(detector); % release(videoSource);% --- 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) videoName = get(handles.edit1, 'String'); videoSource = vision.VideoFileReader(videoName,...'ImageColorSpace', 'RGB', 'VideoOutputDataType', 'uint8'); videoInfo = info(videoSource);% 參數設置 sample_num = 10; % 樣本庫 match_thres = 20; % 匹配閾值 match_num = 2; % 最小匹配數 update_factor = 1; % 一開始50幀內采用該更新因 next_update_factor = 5; % 50幀以后的更新因子 cols = videoInfo.VideoSize(1); % 圖像的寬度 rows = videoInfo.VideoSize(2); % 圖像的高度 fore_thres = 20; % 前景閾值 frame_count = 0; % 處理幀數 neighbor = [1, 0, -1, -1, 1, 0, 0, -1, 1]; % 鄰域選擇 % 外部控制標記 global exit_flag; global pause_flag; exit_flag = false; pause_flag = false; % 判斷是否為第一幀 first_flag = true; while ~isDone(videoSource) && ~exit_flagif pause_flaguiwait(handles.figure1);endframe = step(videoSource);frame_gray = double(rgb2gray(frame));if first_flagfirst_flag = false;%% 開始初始化samples = cell(1, sample_num);% 前兩個樣本設置初始像素samples{1} = frame_gray;samples{2} = frame_gray;% 剩下的樣本初始化for i = 3:sample_numsamples{i} = frame_gray + double(floor(rand(rows, cols) * 20) - 10);endfore_count = uint8(zeros(rows, cols));frame_count = frame_count + 1% 初始化結束continueend%% 前景分割fgMask = uint8(ones(rows, cols) * match_num);for i = 1:sample_numdistance = uint8(abs(samples{i} - frame_gray) <= match_thres);fgMask = fgMask - distance;endfgMask = logical(fgMask * 255);% 更新前景計數for r = 1 : rowsfor c = 1:colsif fgMask(r, c) == 1fore_count(r, c) = fore_count(r, c) + 1;if fore_count(r, c) >= fore_thresfore_count(r,c) = 0;fgMask(r, c) = 0;endelsefore_count(r, c) = 0;endendendupdateMask = fgMask;updateMask = imfill(updateMask, 'hole');;imshow(fgMask);drawnow; end release(videoSource);

四、仿真結果

?

?

?

總結

以上是生活随笔為你收集整理的【目标检测】基于帧差法+Vibe算法实现车辆行人检测matlab源码的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。