clc;
close all;
clear all;
global mov;
movie_file = 'D:\MATLAB\R2009a\toolbox\vipblks\vipdemos\vipmen.avi';
mov = aviread(movie_file);
file_info = aviinfo(movie_file);
% Number of frames
no_of_frames = file_info.NumFrames;
% Get the dimension of one picture
sizes = [size(mov(1).cdata)];
% If we only have greyscale
if(length(sizes) == 2)
sizes(3) = 1; % Pretend we have a third dimension
end
% Get frames in cell f
i = 1;
for t = 1:16
f(t) = {mov(i).cdata};
i = i + fix(no_of_frames/16);
end;
display_frames(f, 4, 4, 'Original frames (every sixteenth frame)');
for t = 1:16
% Apply gaussian low pass smoothing filter
% h = fspecial('gaussian', hsize, sigma) returns a rotationally symmetric
% Gaussian lowpass filter of size hsize with standard deviation sigma
% (positive). hsize can be a vector specifying the number of rows and
% columns in h, or it can be a scalar, in which case h is a square
% matrix. The default value for hsize is [3 3]; the default value for
% sigma is 0.5.
hsize = [3 3];
sigma = 0.5;
H = fspecial('gaussian', hsize, sigma);
Smoothed = imfilter(f{t},H,'replicate');
% Store it back into original structure array
f{t} = Smoothed;
end;
display_frames(f, 4, 4, 'Smoothened frames using gaussian low pass smoothing filter');
% Two frame difference
for t = 1:16
two_frame_diff{t} = imsubtract(f{t}, f{1});
end;
display_frames(two_frame_diff, 4, 4, 'Two frame difference output')
% Two frame difference and otsu thresholding
for t = 1:16
I = imsubtract(f{t}, f{1});
level = graythresh(I);
two_frame_diff_otsu{t}= im2bw(I,level);
end;
display_frames(two_frame_diff_otsu, 4, 4, 'Frame difference using Otsu thresholding');
% Make images gray for further processing
for t = 1:16
f{t} = rgb2gray(f{t});
end;
display_frames(f, 4, 4, 'Grayscale images');
% Three frame difference
th1 = 5;
th2 = 5;
t = 1;
blank = false(sizes(1), sizes(2));
temporal_diff{t} = blank;
R{t} = blank;
temporal_foreground{t} = blank;
spatial_diff{t} = blank;
background{1} = f{1};
alpha = 0.005;
spatial_foreground{t} = spatial_diff{t} > th2;
for t = 2:16
temporal_diff{t} = f{t} - f{t-1};
R{t} = temporal_diff{t} >= th1;
temporal_foreground{t-1} = R{t-1}.*R{t};
background{t} = (1-alpha)*background{t-1} + alpha*f{t};
spatial_diff{t} = f{t} - background{t-1};
spatial_foreground{t} = spatial_diff{t} > th2;
end;
temporal_foreground{t} = blank;
display_frames(temporal_foreground, 4, 4, 'temporal_foreground');
display_frames(background, 4, 4, 'Background');
display_frames(spatial_diff, 4, 4, 'spatial_diff');
display_frames(spatial_foreground, 4, 4, 'spatial_foreground');
for t = 1:16
initial_motion_foreground{t} = temporal_foreground{t} + spatial_foreground{t};
end;
display_frames(initial_motion_foreground, 4, 4, 'initial_motion_foreground');
maximize_all_figures;