% the read operation on this object returns you the image-sequence as a 4D array (please check
% the size of it, which should be equal to [240x320x3x1001])
video_obj = VideoReader('video1.avi');
video_matrix = video_obj.read();
video_matrix = video_matrix(:,:,:,1:100);

[X, Y, Z, t] = size(video_matrix);
gray_video = zeros(X,Y,t);
parfor i = 1:size(video_matrix,4)
    gray_video(:,:,i) = rgb2gray(video_matrix(:,:,:,i));
end

T = 50;
background = zeros(X,Y,t-T);
diff_video = zeros(X,Y,t-T);
% figure(1)
endT = uint8(size(gray_video,3)-T);
parfor i = 1:endT
    background(:,:,i) = mode(gray_video(:,:,i:i+T),3);
    diff_video(:,:,i) = abs(gray_video(:,:,i) - background(:,:,i));
    diff_frame = diff_video(:,:,i);
    diff_frame(diff_frame > 50) = 255;
    diff_frame(diff_frame <= 50) = 0;
%     diff_video(:,:,i) = diff_frame;
end

output = cat(1,gray_video(:,:,t-T), background), cat(diff_frame, diff_frame);

figure(1)
for i = 1:size(diff_video,3)
    imshow(output
    drawnow;
%     subplot(2,2,4)
%     imshow(diff_video(:,:,i));
end

% convert your 4D color image array to a 3D grayscale image array
% you can do it only frame-by-frame
% ? the way you can create the next frame of your output video:
% ? update the subplots of your existing figure
% ? call the writeVideo method on your output video object, with the current frame as a second
% parameter:
% writeVideo(video_out, getframe(gcf));
% ? be careful with the length of the processed video: experiment only with a shorter sequence (eq.
% length of 10 or 20 frames)


% every output frame of your video should contain 5 subplots:
% 1. the actual grayscale frame from the input sequence
% 2. the statistical background in a predefined time-window (eg. T=100)
% with the built-in mode function you can compute the mode of an array along a specified dimension, eg.
% temporal_background = mode(grayscale_video(:, :, idx-T:idx-1), 3);
% 3. the difference (positive number) between the current frame and the temporal background
% 4. the b&w version of the difference, computed with a predefined threshold (eg. 50)
% think about the particularly excellent array indexing capabilities of MATLAB, like
% data_array = [1, 2, 3; 4, 5, 6];
% threshold_value = 3;
% data_array_copy = 1.* data_array;
% data_array_copy(data_array_copy > threshold_value) = 6;
% data_array_copy(data_array_copy <= threshold_value) = 1;
% 5. apply the morphological opening (erosion + dilation) on your threshold image, useful MATLAB
% commands: imerode, imdilate
% both of them will need a structuring element (strel) as a second parameter (it can be disk-shaped
% with radius 1, eg strel(‘disk’, 1) --- theory recap on the next slides


% open the output video file
% ? in MATLAB, you will need a VideoWriter object:
% video_out = VideoWriter(‘video1_your_output.avi’);
% (if you can’t see the output later, try to set the format to ‘Uncompressed AVI’ but be careful, it will result you a huge file!)
% ? in the case of your video writer: you can specify the framerate only before the opening:
% video_out.FrameRate = video_obj.FrameRate;
% ? the open operation on this object opens you the writer
% ? do not forget to call close operation on this object at the end of your script:
% close(video_out); 