NUM_DAYS = 20;
NUM_PROPERTIES = 5; % 1: top left x-coordinate, 2: top left y-coordinate, 3: box height, 4: box width, 5: behaviour

% Loop over each day
for day = 1 : 1%NUM_DAYS
day    
   % File have the format DayN.mat, where N is a number from 01 to 20
   if day < 10
       singleDigitDay = '0';
   else
       singleDigitDay = '';
   end

   % Load the day''s Ground Truth and the filenames
   % We assume that the Ground Truth files are in folder GT
   % and the files of frame names is in folder NAMES
   load(['GT/day', singleDigitDay, int2str(day), '.mat']);
   load(['NAMES/Name', singleDigitDay, int2str(day), '.mat']);
   
   % Example showing number of frames / images in current day
   NumFrames = length(name)
   
   % Example showing properties for each frame
   %  1 - bounding box top left column coordinate
   %  2 - bounding box top row column coordinate
   %  3 - bounding box height
   %  4 - bounding box width
   %  5 - behaviour
   for frame = 1400:1402 %1 : NumFrames
     FILENAME=name{frame}
     LABELS=labels{frame}

     % draw a bounding box around the first person
     % Assume that all frames for this day are in the folder FRAMES
     figure(1)
     clf
     Video_Frame = imread(['FRAMES/',FILENAME]);
     hold on
     imshow(Video_Frame)

           
     %#############################First Person case #################
     if LABELS(1,1)~=0 && LABELS(1,2)~=0  && LABELS(1,3)~=0 && LABELS(1,4)~=0 

       % A white rectangle is constructed in an all black image i.e., Silhouette1,
       % which is equal in size to the video frame.
       % A bounding box is constructed around the white rectangle pixels locations
       % and will be displayed on the color image.
       % First construct a filled rectangle
       Silhouette1=zeros(size(rgb2gray(Video_Frame)));  
       Silhouette1(LABELS(1,2):LABELS(1,2)+LABELS(1,4),LABELS(1,1):LABELS(1,1)+LABELS(1,3))=255;
       st = regionprops(Silhouette1, 'BoundingBox' ); % then get its BoundingBox
       for k = 1 : length(st)  % then draw the BoundingBox
         thisBB = st(k).BoundingBox;
         Rect=rectangle('Position', [thisBB(1),thisBB(2),thisBB(3),thisBB(4)], 'EdgeColor','r','LineWidth',2 );
       end
     end
     
     %####################### If a second person present ##################################
     if LABELS(2,1)~=0 && LABELS(2,2)~=0  && LABELS(2,3)~=0 && LABELS(2,4)~=0 
       Silhouette2=zeros(size(rgb2gray(Video_Frame)));  
       Silhouette2(LABELS(2,2):LABELS(2,2)+LABELS(2,4),LABELS(2,1):LABELS(2,1)+LABELS(2,3))=255;
       st = regionprops(Silhouette2, 'BoundingBox' ); % then get its BoundingBox
       for k = 1 : length(st)  % then draw the BoundingBox
         thisBB = st(k).BoundingBox;
         Rect=rectangle('Position', [thisBB(1),thisBB(2),thisBB(3),thisBB(4)], 'EdgeColor','g','LineWidth',2 );
       end
     end
     
     %################### If a third person present ############
     if LABELS(3,1)~=0 && LABELS(3,2)~=0  && LABELS(3,3)~=0 && LABELS(3,4)~=0
       Silhouette3=zeros(size(rgb2gray(Video_Frame)));  
       Silhouette3(LABELS(3,2):LABELS(3,2)+LABELS(3,4),LABELS(3,1):LABELS(3,1)+LABELS(3,3))=255;
       st = regionprops(Silhouette3, 'BoundingBox' ); % then get its BoundingBox
       for k = 1 : length(st)  % then draw the BoundingBox
        thisBB = st(k).BoundingBox;
        Rect=rectangle('Position', [thisBB(1),thisBB(2),thisBB(3),thisBB(4)], 'EdgeColor','b','LineWidth',2 );
      end
     end
     
      
     pause       

   end % frame loop
   
   % Example results for 1400:1402
   % FILENAME = 'inspacecam163_2016_02_18_16_18_36.jpg'
   % LABELS =
   %    215   257   223   441     3
   %    359   392   241   326     3
   %    629   140   233   573     3
   % 
   % FILENAME = 'inspacecam163_2016_02_18_16_18_37.jpg'
   % LABELS =
   %    215   257   223   441     3
   %    359   392   241   326     3
   %    629   140   233   573     3
   % 
   % FILENAME = 'inspacecam163_2016_02_18_16_18_38.jpg'
   % LABELS =
   %    215   257   223   441     3
   %    359   392   241   326     3
   %    629   140   233   573     3

   % histogram the number of each type of activity
   action = zeros(5,1);
   for frame =  1:NumFrames
     action(1+labels{frame}(1,5)) = action(1+labels{frame}(1,5)) + 1;
   end   
   ACTION_HIST=action'
   ACTION_COUNT=sum(action)

   % results
   % ACTION_HIST = 1158         155        4108        4626           0
   % ACTION_COUNT = 10047

end % day loop
