%% analyse_all_pixelcount

% Script goes through all videos in designated file and applies Pixel Count
% 
% Pixel Count
% - go from frame to frame
% - find difference between succeeding frames
% - threshold the difference
% - summ up the thresholded difference
% - save

%%
clear;

%% Load all videos

% Set path to video folder
currfoldervid = 'C:\motol\data\jc20181218_1\mp4_5fps\'; % video

% Load all videos, prepare iterating variable,
% display number of loaded items.
folder_data_vid = [currfoldervid, '*.mp4'];
loads_vid = dir(folder_data_vid);
disp(['Data from folder ', folder_data_vid, ' loaded.']);
disp('//------------------------\\');
L = 1:length(loads_vid);
disp(['Number of loaded items: ', num2str(L(end)), '.']);

%% Load names of mat files

% Load the MATLAB data files in the folder
% This is done to look for already existing analysed Pixel Count files
folder_data_mat = [currfoldervid, '*.mat'];
x_mat = dir(folder_data_mat);
for i = 1:length(x_mat)
    loads_mat(i,:) = x_mat(i).name;
end
if length(x_mat) == 0
    loads_mat = '';
end
loads_mat = num2cell(loads_mat, 2);

% Mat file names loaded

%% Start analysing

% Go through all videos
for i = L

    video_name = [loads_vid(i).name(1:end-4), '.mat'];

    % If Pixel Count file already exists, skip
    if any(strcmp(video_name, loads_mat))
        disp(['File ', video_name, ' already created.']);
    else
        
        % Save the start timestamp of analysis
        fprintf('Starting video analysis...');
        t_start = tic;

        % Load the video, initialize variables
        video = VideoReader([currfoldervid, loads_vid(i).name]);
        count = [];
        temp = readFrame(video);
        temp = im2gray(temp);
        frame_counter = 0;

        % Go through all frames
        while hasFrame(video)
            frame_counter = frame_counter + 1;
            
            % Read and convert current frame
            % Videos are saved in 3 channels, conv rgb => gray
            frame = readFrame(video);
            frame = im2gray(frame);
    
            % Compute the difference between succeeding frames
            dif = (cast(frame, 'double') - cast(temp,'double'));
            temp = frame;
    
            % Newpos = thresholded difference
            newpos = dif < -20;
    
            % Summ up and write newpos to Pixel Count vector
            count = [count, sum(newpos, 'all')];
            
        end

        % Save the Pixel Count data
        save_file_name = [loads_vid(i).name(1:end-4)]; % cut .mp4
        save([currfoldervid, save_file_name, '.mat'], 'count');
        
        % Report and save the Pixel Count time information
        % Note: this could be saved in 1 file, but due to the fact that in
        % further analyses we do not work with times, this simplistic
        % method was chosen
        t_end = toc(t_start);
        save([currfoldervid, 'time_', save_file_name, '.mat'], 't_end');

        disp(['Video ', video_name, ' analysed in ', num2str(t_end), ' s.']);
    end

end

% Analysis complete