1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
| function varargout = LoadVideo(varargin)
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @LoadVideo_OpeningFcn, ...
'gui_OutputFcn', @LoadVideo_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
end
% --- Executes just before LoadVideo is made visible.
function LoadVideo_OpeningFcn(hObject, ~, handles, varargin)
% Choose default command line output for LoadVideo
handles.output = hObject;
% Update handles structure
guidata(hObject, handles);
axis(findobj('Tag','VideoPanel'),'off');
OutI = zeros([640 480 3],'uint8');
imshow(OutI);
global playback;
playback = struct('paused',true,'motion',true,'loop',false)
% uiwait(handles.figure1);
end
% --- Outputs from this function are returned to the command line.
function varargout = LoadVideo_OutputFcn(~, ~, handles)
varargout{1} = handles.output;
end
% --- Executes on button press in Load.
function Load_Callback(~, ~, ~)
global playback;
global paused;
paused = true;
global video;
set(findobj('Tag','Play'),'Enable','off');
set(findobj('Tag','Stop'),'Enable','off');
set(findobj('Tag','Pause'),'Enable','off');
[fname,path,fi] = uigetfile({'*.avi;*.mpg','Video file'},'Choose a video');
if ~fi,return,end
video = mmreader([path fname]);
nFrames = video.NumberOfFrames;
vidHeight = video.Height;
vidWidth = video.Width;
global mov
mov = struct('cdata',{},'colormap', []);
mov(1:nFrames) = ...
struct('cdata', zeros(vidHeight, vidWidth, 1, 'uint8'),...
'colormap', []);
h = waitbar(0,'Please wait. Loading selected video...');
for k = 1 : nFrames
mov(k).cdata = (rgb2gray(read(video, k)));
waitbar(k/nFrames, h);
end
ProcessVideo(h);
delete (h);
global frcnt;
frcnt = nFrames;
global frame;
frame = 1;
set(findobj('Tag','Play'),'Enable','on');
axis(findobj('Tag','VideoPanel'));
set(findobj('Tag','filenametext'),'String',[path fname]);
set(findobj('Tag','frameratetext'),'String',sprintf('%.2f FPS',video.FrameRate));
set(findobj('Tag','durationtext'),'String',sprintf('%.2f s',nFrames/video.FrameRate));
colormap gray;
playframe();
end
function ProcessVideo(h)
global mov;
global video;
global mov2;
global bgr;
nFrames = video.NumberOfFrames;
vidHeight = video.Height;
vidWidth = video.Width;
h = waitbar(0,h,'Building motion map...');
bgr = zeros(vidHeight, vidWidth, 1, 'uint8');
mov2 = struct('cdata',{},'colormap', []);
mov2(1:nFrames) = ...
struct('cdata', zeros(vidHeight, vidWidth, 1, 'uint8'),...
'colormap', []);
mov2(1).cdata = mov(1).cdata;
for i=2:nFrames
mov2(i).cdata = mov(i).cdata - mov(i-1).cdata;
end
h = waitbar(0,h,'Removing motion noise...');
for i=1:nFrames
h = waitbar(i/nFrames,h,'Removing motion noise...');
idx = find(imclose ((medfilt2(mov2(i).cdata,[5 5]) > 30) ,strel('disk',10 ) ) );
mov2(i) = ... fill
struct('cdata', zeros(vidHeight, vidWidth, 1, 'uint8'),...
'colormap', []);
mov2(i).cdata(idx) = mov(i).cdata(idx);
end
global Bounds;
Bounds = struct('rect',{},'area',{});
for i=1:nFrames
end
for i=1:nFrames
Elements = regionprops ( mov2(i).cdata > 0 ,'all');
Bounds(i).rect = zeros([size(Elements,1) 4],'uint8');
if ~isempty(Elements)
for j=1:size(Elements)
Bounds(i).rect(j,1:4) = Elements(j).BoundingBox;
Bounds(i).area(j) = Elements(j).Area;
end
end
end
end
function playframe()
global mocheck;
global mov;
global mov2;
global frame;
global Bounds;
if ~mocheck
imshow( (mov(frame).cdata ) );
else
imshow( (mov2(frame).cdata) );
end
% hold on;
% if ~isempty(Bounds(frame));
% for i=1:size(Bounds(frame).rect,1);
% rct = Bounds(frame).rect(i);
% plot([rct(1),rct(2),rct(1)+rct(3),rct(2)+rct(4)],'-.r')
% end
% end
% hold off;
end
% --- Executes on button press in Play.
function Play_Callback(~, ~, ~)
global paused;
paused = false;
global frame;
global frcnt
global video;
global loop;
axis(findobj('Tag','VideoPanel'));
set(findobj('Tag','Play'),'Enable','off');
set(findobj('Tag','Stop'),'Enable','on');
set(findobj('Tag','Pause'),'Enable','on');
while ~paused
playframe();
set(findobj('Tag','framenumtext'),'String',frame);
frame = frame + 1;
if frame == frcnt + 1
if loop
frame = 1;
else
paused = true;
frame = 1;
set(findobj('Tag','Play'),'Enable','on');
set(findobj('Tag','Stop'),'Enable','off');
set(findobj('Tag','Pause'),'Enable','off');
axis(findobj('Tag','VideoPanel'));
playframe();
end
end
pause ( 1 / video.FrameRate);
end
end
function Stop_Callback(~, ~, ~)
global paused;
global frame;
paused = true;
frame = 1;
set(findobj('Tag','Play'),'Enable','on');
set(findobj('Tag','Stop'),'Enable','off');
set(findobj('Tag','Pause'),'Enable','off');
axis(findobj('Tag','VideoPanel'));
playframe();
end
% --- Executes on button press in Pause.
function Pause_Callback(~, ~, ~)
global paused;
paused = true;
set(findobj('Tag','Play'),'Enable','on');
set(findobj('Tag','Stop'),'Enable','on');
set(findobj('Tag','Pause'),'Enable','off');
end
% --- Executes during object creation, after setting all properties.
function Load_CreateFcn(~, ~, ~)
end
% --- If Enable == 'on', executes on mouse press in 5 pixel border.
% --- Otherwise, executes on mouse press in 5 pixel border or over Load.
function Load_ButtonDownFcn(~, ~, ~)
end
% --- Executes when user attempts to close figure1.
function figure1_CloseRequestFcn(hObject, ~, ~)
% Hint: delete(hObject) closes the figure
delete(hObject);
end
% --- Executes during object deletion, before destroying properties.
function figure1_DeleteFcn(~, ~, ~)
global paused;
paused = true;
end
% --- Executes on button press in loopcheck.
function loopcheck_Callback(hObject, ~, ~)
% hObject handle to loopcheck (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
global loop;
loop = get(hObject,'Value');
% Hint: get(hObject,'Value') returns toggle state of loopcheck
end
% --- Executes on button press in nextframe.
function nextframe_Callback(~, ~, ~)
global frame;
global frcnt;
global paused;
global loop;
frame=frame+1;
if frame == frcnt + 1
if loop
frame = 1;
else
paused = true;
frame = 1;
set(findobj('Tag','Play'),'Enable','on');
set(findobj('Tag','Stop'),'Enable','off');
set(findobj('Tag','Pause'),'Enable','off');
axis(findobj('Tag','VideoPanel'));
playframe();
end
end
playframe();
set(findobj('Tag','framenumtext'),'String',frame);
% hObject handle to nextframe (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
end
% --- Executes on button press in prevframe.
function prevframe_Callback(~, ~, ~)
global frame;
global frcnt;
global paused;
global loop
frame=frame - 1;
if frame == 0
if loop
frame = 1;
else
paused = true;
frame = frcnt;
set(findobj('Tag','Play'),'Enable','on');
set(findobj('Tag','Stop'),'Enable','off');
set(findobj('Tag','Pause'),'Enable','off');
axis(findobj('Tag','VideoPanel'));
playframe();
end
end
playframe();
set(findobj('Tag','framenumtext'),'String',frame);
% hObject handle to prevframe (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
end
% --- Executes on button press in mocheck.
function mocheck_Callback(hObject, ~, ~)
% hObject handle to mocheck (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
global mocheck;
mocheck = get(hObject,'Value');
playframe();
% Hint: get(hObject,'Value') returns toggle state of mocheck
end
% --- Executes on button press in subtract.
function subtract_Callback(hObject, eventdata, handles)
% hObject handle to subtract (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
global subtract;
subtract = get(hObject,'Value');
playframe();
% Hint: get(hObject,'Value') returns toggle state of subtract
end |