%load SurvTestFrames

for i=1:size(ag,3)
imshow(ag(:,:,i));
pause(0.03);
end

image=ag;
BG1=sum(ag,3)/56;
imshow(BG1)
BGstd=std(ag,1,3);

for n=1:56
    for i=1:240
        for j=1:320
            if (abs(image(i,j,n)-BG1(i,j))>2*BGstd(i,j))
                image(i,j,n)=1;
            else
                image(i,j,n)=0;
            end
        end
    end
end

for i=1:56
imshow(image(:,:,i));
pause(0.03);
end

v=zeros(1,56);
for n=1:56
    d=find(image(:,:,n)==1);
    v(n)=size(d,1);
end

bar(v)

a=mean(v);
b=find(v<a);
l=size(b,2);

BG2=sum(ag(:,:,b),3)/l;
imshow(BG2);

BGstd=std(ag(:,:,b),1,3);

image1=ag;
for n=1:56
    for i=1:240
        for j=1:320
            if (abs(image1(i,j,n)-BG2(i,j))>2*BGstd(i,j))
                image1(i,j,n)=1;
            else
                image1(i,j,n)=0;
            end
        end
    end
end

for i=1:56
imshow(image1(:,:,i));
pause(0.03);
end

diff=(abs(BG2-BG1));
diff=histeq(diff);
imshow(diff);

                