本文共 6094 字,大约阅读时间需要 20 分钟。
一、简介
一、原理
摄像机采集的视频序列具有连续性的特点。如果场景内没有运动目标,则连续帧的变化很微弱,如果存在运动目标,则连续的帧和帧之间会有明显地变化。
帧间差分法(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。
二、三帧差分法
两帧差分法适用于目标运动较为缓慢的场景,当运动较快时,由于目标在相邻帧图像上的位置相差较大,两帧图像相减后并不能得到完整的运动目标,因此,人们在两帧差分法的基础上提出了三帧差分法。
三帧差分法的运算过程如图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序列中的目标运动较快,在这种情况下,运动目标在不同图像帧内的位置明显不同,采用两帧差分法检测出的目标会出现“重影”的现象,采用三帧差分法,可以检测出较为完整的运动目标。
综上所述,帧间差分法的原理简单,计算量小,能够快速检测出场景中的运动目标。但由实验结果可以看出,帧间差分法检测的目标不完整,内部含有“空洞”,这是因为运动目标在相邻帧之间的位置变化缓慢,目标内部在不同帧图像中相重叠的部分很难检测出来。帧间差分法通常不单独用在目标检测中,往往与其它的检测算法结合使用。
二、源代码
unction varargout = facedetecion(varargin)% FACEDETECION MATLAB code for facedetecion.fig% FACEDETECION, by itself, creates a new FACEDETECION or raises the existing% singleton*.%% H = FACEDETECION returns the handle to a new FACEDETECION or the handle to% the existing singleton*.%% FACEDETECION('CALLBACK',hObject,eventData,handles,...) calls the local% function named CALLBACK in FACEDETECION.M with the given input arguments.%% FACEDETECION('Property','Value',...) creates a new FACEDETECION or raises the% existing singleton*. Starting from the left, property value pairs are% applied to the GUI before facedetecion_OpeningFcn gets called. An% unrecognized property name or invalid value makes property application% stop. All inputs are passed to facedetecion_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 facedetecion% Last Modified by GUIDE v2.5 01-May-2017 19:18:42% Begin initialization code - DO NOT EDITgui_Singleton = 1;gui_State = struct('gui_Name', mfilename, ... 'gui_Singleton', gui_Singleton, ... 'gui_OpeningFcn', @facedetecion_OpeningFcn, ... 'gui_OutputFcn', @facedetecion_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{:});else gui_mainfcn(gui_State, varargin{:});end% End initialization code - DO NOT EDIT% --- Executes just before facedetecion is made visible.function facedetecion_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 facedetecion (see VARARGIN)% Choose default command line output for facedetecionhandles.output = hObject;% Update handles structureguidata(hObject, handles);% UIWAIT makes facedetecion wait for user response (see UIRESUME)% uiwait(handles.figure1);% --- Outputs from this function are returned to the command line.function varargout = facedetecion_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 structurevarargout{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)global myvideo myvideo1;[fileName,pathName] = uigetfile('*.*','Please select an video');%文件筐,选择文件 if(fileName) fileName = strcat(pathName,fileName); fileName = lower(fileName);%一致的小写字母形式 else % J = 0;%记录区域生长所分割得到的区域 msgbox('Please select an video'); return; %退出程序 end % boxlnserter = vision.ShapeInserter('BorderColor','Custom','CustomBorderColor',[255 0 0]);% videoOut = step(boxlnserter,videoFrame,bbox);myvideo = VideoReader(fileName);nFrames = myvideo.NumberOfFramesvidHeight = myvideo.HeightvidWidth = myvideo.Widthmov(1:nFrames) = struct('cdata',zeros(vidHeight,vidWidth,3,'uint8'),'colormap',[]); B_K = read(myvideo,1); axes(handles.axes1); imshow(B_K); % myvideo = VideoReader(fileName);% nFrames = myvideo.NumberOfFrames% vidHeight = myvideo.Height% vidWidth = myvideo.Width% mov(1:nFrames) = struct('cdata',zeros(vidHeight,vidWidth,3,'uint8'),'colormap',[]);% B_K = read(myvideo,1);% axes(handles.axes1);% imshow(B_K);% --- 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)global myvideo myvideo1;nFrames = myvideo.NumberOfFramesvidHeight = myvideo.HeightvidWidth = myvideo.Widthmov(1:nFrames) = struct('cdata',zeros(vidHeight,vidWidth,3,'uint8'),'colormap',[]); faceDetector = vision.CascadeObjectDetector();% videoFileReader = vision.VideoFileReader(fileName);% videoFrame = step(videoFileReader);
三、运行结果
四、备注
完整代码或者代写添加QQ 1575304183
转载地址:https://blog.csdn.net/qq_34763204/article/details/116378991 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!