一、运动目标检测简介

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)更新:
在这里插入图片描述

二、部分源代码

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)