%
% Copyright (c) 2011, Kai Guo, Prakash Ishwar, and Janusz Konrad. All
% rights reserved.
%
% Redistribution and use in source and binary forms, with or without
% modification, are permitted provided that the following conditions are
% met:
%
% 1) Redistributions of source code must retain the above copyright
%    notice, this list of conditions and the following disclaimer.
%
% 2) Redistributions in binary form must reproduce the above copyright
%    notice, this list of conditions and the following disclaimer in the
%    documentation and/or other materials provided with the distribution.
%
% THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
% "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
% LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
% A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
% HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
% SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
% LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
% DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
% THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
% (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
% OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
%

% The main function that computes the optical flow based covariance matrices

clear
clc

act={'bend';'jack';'jump';'pjump';'run';'side';'skip';'walk';'wave1';'wave2'};
name={'daria';'denis';'eli';'ido';'ira';'lena';'lyova';'moshe';'shahar'};

directory='E:\database\Weizmann'; %local directory that needs to be altered
directory2='E:\database\Weizmann_flow'; %local directory that needs to be altered

cube_length=20; %length of each video segment
jump_dataset=4; %the overlap of two neighboring segments is: (cube_legnth-jump_dataset)

Cov_dataset_all=cell(10, 9); %save all the feature covariance matrices

for i=1:10
   cur_act=act{i};
   for j=1:9
         cur_name=name{i};
         name1=[directory,'\',cur_act,'\',cur_name,'_',cur_act,'.mat'];
         name2=[directory2,'\',cur_act,'\','flow_',cur_name,'_',cur_act,'.mat'];
         %
         % load raw videos and corresponding optical flows of videos
         %
         load(name1);
         load(name2);
         
         seq=seq(:,:,1:end-1);
         %
         % N: number of segments
         %
         N=floor((size(flow,3)-cube_length)/jump_dataset+1);
         Cov_dataset=[];
         count=1;
         for k=1:N
             seq_segment=seq(:,:,jump_dataset*(k-1)+1:jump_dataset*(k-1)+cube_length);
             flow_segment=flow(:,:,jump_dataset*(k-1)+1:jump_dataset*(k-1)+cube_length,:);
             temp_cov=optical_flow_feature(flow_segment,seq_segment);
             if ~isempty(temp_cov) && cond(temp_cov)<1e8
                 Cov_dataset(:,:,count)=temp_cov;
                 count=count+1;
             end
         end
         Cov_dataset_all{i,j,s}=Cov_dataset;
   end
end

%%%%%%%%
