%load SurvTestFrames.mat
% by looking at Background picture(mean of all) and histogram
% also foregraound picture (mean of all) and  its  histogram we can see the
% similarity between them but there is  a subtle  difference between these
% two
% which We can see by Subtracting both and showing the picture followed by
% its histogram

%use BG1 (the mean of all frames)
bgMean1 = mean(ag,3);
bgStd1 = std(ag,1,3);
M=[];
for i=1:56
    diff1(:,:,i)= abs(ag(:,:,i)-bgMean1)>2*bgStd1;
    V1(i)=sum(sum(diff1(:,:,i)));
    imshow(diff1(:,:,i));
    M=[M getframe()];
end

movie2avi(M,'bg1.avi');

bar(V1);
f=find (V1<mean(V1));
NewMean=mean(ag(:,:,f),3);
NewStd=std(ag(:,:,f),1,3);
M1=[]
for i=1:56
    diff2(:,:,i)=abs(ag(:,:,i)-NewMean)>2*NewStd;
    imshow(diff2(:,:,i));
    M1=[M1 getframe()];
end

movie2avi(M,'bg2.avi');
Bg2= histeq(abs(bgMean1-NewMean));
subplot(3,3,1)
imshow(bgMean1)
 text(0, -40, ['background picture ' ]);
subplot(3,3,3)
imhist(bgMean1)
subplot(3,3,4)
imshow(NewMean)
 text(0, -40, ['foreground picture ' ]);
subplot(3,3,6)
imhist(NewMean)
subplot(3,3,7)
imshow(Bg2)
text(0, -40, ['Background-foreground ' ]);
subplot(3,3,9)
imhist(Bg2)
