% Sravani Chandaka
% Assignment #3

imgmat = zeros(240, 320);
for i=1:56
%calculate sum of images.
    imgmat = plus(imgmat, ag(:, :, i));
end

%find the background image.
BG1 = imgmat ./ 56;

%find the standard deviation
BGstd = std(ag, 0, 3);

vect = zeros(1, 56);
%find no of foreground pixels and store in a vector
foreground1 = zeros(240, 320, 56);
for j=1:56
    im1 = abs(ag(:, :, j) - BG1);
    im2 = BGstd*2;
    
    %finding difference between the images.
    imgdiff = im1 - im2;
    
    for m=1:240
        for n=1:320
            if imgdiff(m, n)<0
                imgdiff(m, n)=0;
            else
                imgdiff(m, n)=1;
            end
        end
        
        %find imgdiff for each image.
        foreground1(:, :, j) = imgdiff;
        
        %no of ones in imgidff.
        onesvector = sum(sum(imgdiff));
        
    end
       %find no of foreground pixels and store in vector
            vect(i) = onesvector;
            
end

%average number of foreground pixels.
avgv = sum(vect) ./ 56;


%deviation from foreground pixels
newvect = vect - avgv;

newsamplespace = zeros(240, 320, 56);
% background detection.
for t=1:56
    if newvect(t)<0
        newsamplespace(:, :, t)=ag(:, :, t);
    else
        newsamplespace(:, :, t)=zeros(240, 320);
    end
end

imgmat1 = zeros(240, 320);
for x=1:56
    %calculate the sum of images.
imgmat1 = plus(imgmat1, newsamplespace(:, :, x));

end

%Calculate the background image.
BG2 = imgmat1 ./ 56;

%Calculate the standard deviation.
BGstd1 = std(newsamplespace, 0, 3);

%define a matrix to store the sum of the images for the second version .
imgmat2 = zeros(240, 320);

for y=1:56
    %Calculating the sum of images for the second version
imgmat2 = plus(imgmat2, newsamplespace(:, :, y));

end
newvector = zeros(1, 56);
for e=1:56
    newvector(e) = 0;
    if newvect(e)<0;
    else
        newvector(e) = 1;
    end
end
%Finding the number of images 
lengthsample = 56 - sum(newvector);

%the background image for second version
BG3 = imgmat1 ./ lengthsample;

%Calculating the standard deviation
BGstd1 = std(newsamplespace, 0, 3);
foreground2 = zeros(240, 320, lengthsample);

for z=1:size(foreground2, 3)
    im11 = abs(newsamplespace(:, :, z) - BG3);
    im22 = BGstd1*2;
    
    %the difference image for the second version
    imgdiff1 = im11 - im22;
       for c=1:240
        for d=1:320
            if imgdiff1(c, d)<0
                imgdiff1(c, d)=0;
            else
                imgdiff1(c,d)=1;
            end
        end
        %Filling the imgdiff for the second version
        foreground2(:, :, z) = imgdiff1;
        
    end
end

%difference image sample size taken to fifty six.
diffback1 = abs(BG2 - BG1); 

%difference image where sample size taken to be smaller.
diffback2= abs(BG3 - BG1);

%showing the background image BG1.
imshow(BG1)
figure;
%compute the pixelwise standard deviation.
imshow(BGstd)
figure;

%generating movie 
for a=1:56
imshow(foreground1(:,:,a));
pause(0.03);
end

%creating BG2 as specified in the instructions.
figure;
imshow(BG2)

%creating BG3 as specified in the instructions.
figure;
imshow(BG3)

figure;
%generating movie for the second version
for b=1:lengthsample
imshow(foreground2(:,:,b));
pause(0.03);
end




        