%%%------------------------------------------------------------------------
%%% Image Processing HW1. part 1
%%% Xin Li, Feb 4,2008
%%%------------------------------------------------------------------------

% clear;
% close all;
% clc;
% 
% % DATAPATH = 'C:\Documents and Settings\Administrator\Desktop\08Spring\image_processing\HW1\';
% DATAPATH = '/usr/home/h/133/tua739/image_processing/';
% filename = strcat(DATAPATH,'SurvTestFrames.mat');
% load(filename,'ag');

M = [];
BG1 = mean(ag,3);
save BG1.mat BG1;
BGstd = std(ag,0,3);
[row, col] = size(ag(:,:,1));

for n = 1:size(ag,3)
    foreground = zeros(row,col);
    image = ag(:,:,n);
    foreground(abs(image-BG1)>2*BGstd) = 1;
    v(n) = length(find(foreground == 1));
    imshow(foreground);
    title(['frame',num2str(n)]);
      
%     if n == 30
% % %         pause;
%         saveas(gcf,['The thirty frame(part1).png']);
%     end
    M=[M getframe()];
end
movie2avi(M,'System1.avi');

colormap gray;
bar(v)
title('Foreground Pixels(first part)')
saveas(gcf,['first bar.png']);
save v.mat v;
close;

%%%---------------compare the thirty figures-----------------------------

% I = im2bw(ag(:,:,30) - BG1);
% threshold = mean(I);
% imshow(I,[]);
% foreground = zeros(row,col);
% imshow((imsubtract(ag(:,:,30), BG1)),[]);








