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});
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 defin