load SurvTestFrames.mat

%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);

%use BG2 (the mean of No.44 frames to No. 56 frames )
figure;
bgMean2 = mean(ag(:,:,44:56),3);%start from 44
bgStd2 = std(ag(:,:,44:56),1,3);
M=[];
for i=1:56
    diff2(:,:,i)= abs(ag(:,:,i)-bgMean2)>2*bgStd2;
    V2(i)=sum(sum(diff2(:,:,i)));
    imshow(diff2(:,:,i));
    M=[M getframe()];
end
movie2avi(M,'bg2.avi');
bar(V2);

figure;
histeq(abs(bgMean2 - bgMean1));

%use BG3 (a foreground activity that's less than the mean activity )
figure;
frameNo= find(V1<mean(V1));
bgMean3 = mean(ag(:,:,frameNo),3);
bgStd3 = std(ag(:,:,frameNo),1,3);
M=[];
for i=1:56
    diff3(:,:,i)= abs(ag(:,:,i)-bgMean3)>2*bgStd3;
    V3(i)=sum(sum(diff3(:,:,i)));
    imshow(diff3(:,:,i));
    M=[M getframe()];
end
movie2avi(M,'bg3.avi');
bar(V3);

figure;
histeq(abs(bgMean2 - bgMean1));