clear ag;
%ag=aga;  %old movie
ag=agb;   %new movie

mask=[0 -1 0;-1 5 -1; 0 -1 0];

%Compute the background image BG1 from the fames as the mean of all frames. 
BG1=mean(ag,3);  

%Compute the pixelwise standard deviation in each pixel 
BGstd=std(ag,0,3); 

%For all frames, show all the difference images
for i=1:size(ag,3)
    image=ag(1:240,1:320,i);
    cc=abs(image-BG1);
    %ee=imfilter(cc, mask);;
    ee=edge(cc,'sobel');
    dd=find(cc<(2*BGstd));
    cc(dd)=0;
    cc=medfilt2(cc,[2 2]); 
    %cc=imfilter(cc, mask);;
    %se = strel('disk',1);
    %cc = imopen(cc,se);
    cc=cc+ee;
    cc=cc>(2*BGstd);
    imshow(cc);
    pause(0.01);
end;