I. Introduction to moving target detection

In recent years, the detection of moving objects, especially the detection of moving vehicles, has been widely developed and applied. Moving object detection is to detect moving objects from image sequence. At present, there are three main methods of moving target detection: background difference method, frame difference method and optical flow method. In this paper, the moving vehicles in the dynamic scene in the video are detected in real time to solve the problem of missing target contour and the background cannot be updated in real time.

1 frame difference method between 1.1 2 frame difference method between interframe difference method is based on the motion image sequence has a strong correlation between two adjacent frames in the proposed detection method, the test method is not sensitive to changes in light, very suitable for dynamic change of the environment, and the operation is simple, fast detection, vehicle positioning and accurate, and suitable for real-time demand higher.

The basic principle of the traditional inter-frame difference method, namely the two-frame difference method, is as follows:Where: Gk(x,y) — result image; Pk(x,y) — image of frame K; Pk-1(x,y) — frame K-1 image. Since the video images captured by the camera have the characteristics of continuity, if there is no moving object in the scene, the change between successive frames is very weak, if there is moving object, the change is more obvious. The algorithm of two-frame difference method is simple and less affected by light, but it still has the following shortcomings: first, it cannot detect stationary or slow moving objects, and for high-speed moving objects, the segmented area is much larger than the real target, and the segmented area is related to the target moving speed; Secondly, if the gray level inside the object is relatively uniform, the adjacent frame difference may form a cavity in the overlapping part of the target, and in serious cases, the segmentation result is not connected, which is not conducive to further object analysis and recognition. Therefore, this paper proposes an improved three-frame difference method to solve the problems existing in the traditional frame difference method.

1.2 Three-frame difference methodWhen the target object is moving too fast, the image obtained by the two-frame difference method will have problems such as double image and the target area is too large. Therefore, the three-frame difference method is proposed to improve the image processing effect. The basic principle is to use three adjacent frames to detect the target instead of two frames to obtain the result image. The flow chart is shown in Figure 1.Figure 1 Flowchart of three-frame difference method Firstly, the difference diagram of the KTH frame and k-1 frame and the difference diagram of the k+1 frame and K frame are obtained respectively, then the gray image is converted into binary image, and finally the binary difference diagram obtained is calculated by “and” to obtain the final result. The formula is as follows:Where: Gk(x,y) — result image; Pk(x,y) — image of frame K; Pk-1(x,y) — frame K-1 image; Pk+1(x,y) — frame K +1 image.

In inter-frame difference method, the choice of threshold T is very important. If the threshold value T is too small, the noise in the difference image cannot be suppressed. If the threshold value T is too large, part of the target information in the difference image may be covered up. Moreover, a fixed threshold T cannot adapt to the changing light in the scene.

In order to make the difference between the moving object and the background more obvious, the maximum interclass variance method (also called Otsu algorithm) is used to achieve this goal. The algorithm is derived from the gray histogram using the least square method and has the best segmentation in statistical sense. Its basic principle is to divide the gray value of the image into two parts with the best threshold value, so that the variance between the two parts is maximum, that is, it has the maximum separation.

2 background difference method Background difference method first selects the average of one or several images in the background as the background image, and then subtracts the current frame of the subsequent sequence image from the background image to carry out background elimination. If the pixel number obtained is greater than a certain threshold value, it is the moving target region, otherwise, it is the background region. It has better detection effect on moving objects in complex background and less computation. The disadvantage is the influence of external conditions such as light and weather is larger.

2.1 mixed gaussian background modeling gaussian mixture background model is based on statistics background pixel representation method, using the pixels in a long period, a large number of sample values of the probability density of statistical information (such as model number, mean and standard deviation for each of the patterns said background, then USES the statistical difference (such as 3 sigma principle) target pixel judgment, The complex dynamic background can be modeled and the computation is large.

In the mixed Gaussian background model, it is considered that the color information between pixels is not correlated, and the processing of each pixel is independent of each other. For each pixel in the video image, the change of its value in the sequence image can be regarded as a random process that continuously generates pixel value, which is used to describe the color presentation rule of each pixel with Gaussian distribution (single mode (single peak), multi-mode (multi-peak)).

For the multi-peak Gaussian distribution model, each pixel of the image is modeled by the superposition of multiple Gaussian distributions with different weights. Each Gaussian distribution corresponds to a state that may produce the color of the pixel. The weights and distribution parameters of each Gaussian distribution are updated with time. When processing color images, it is assumed that the R,G, and B color channels are independent of each other and have the same variance. The observed data set for random variable X {x1,x2… , xN}, xt=(rt,gt,bt) is the sample of pixels at time t, then the probability density function of mixed Gaussian distribution obeyed by a single sampling point xt is:Where: k — total number of distribution modes; η (xt, μ I,t, τ I,t) — the ith Gaussian distribution at time T; μ I,t — its mean; τ I,t — its covariance matrix; The delta I, t – variance; I — three-dimensional identity matrix; Wi,t — the weight of the ith Gaussian distribution at time t.

2.2 Parameter SettingEach new pixel value xt is compared with the current K models according to Formula (8) until the distribution model matching the new pixel value is found, that is, the deviation from the mean value of this model is within 2.5σ :If the matched pattern meets the background requirements, the pixel belongs to the background; otherwise, it belongs to the foreground. The weights of each mode are updated according to Formula (9), where α is the learning rate. For matched mode Mk,t=1, otherwise Mk,t=0, then the weights of each mode are normalized:If the match is not successful, the mean μ and standard deviation σ remain unchanged, and the matching mode is updated according to Equation (10) — Equation (12) :

Two, some source code

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 anew 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'},'Open video');
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;
Copy the code

3. Operation results

Matlab version and references

1 matlab version 2014A

2 Reference [1] CAI Limei. MATLAB Image Processing — Theory, Algorithm and Case Analysis [M]. Tsinghua University Press, 2020. [2] Yang Dan, ZHAO Haibin, LONG Zhe. Examples of MATLAB Image Processing In detail [M]. Tsinghua University Press, 2013. [3] Zhou Pin. MATLAB Image Processing and Graphical User Interface Design [M]. Tsinghua University Press, 2013. [4] LIU Chenglong. Proficient in MATLAB Image processing [M]. Tsinghua University Press, 2015. [5] Han Shuai. Vehicle Detection Based on Improved Inter-frame difference Method and Background Difference Method [J]. Agricultural Equipment and Vehicle Engineering. 201,59(06)