% Jingting Zeng  02/20/07
% detect people

% midian filter, dilation, erosion
ag1= ag;

bgMean1 = mean(ag1,3);
bgStd1 = std(ag1,1,3);
M=[];
for i=1:size(ag,3)
    diff1(:,:,i)= abs(ag1(:,:,i)-bgMean1)>2*bgStd1;
    diff1(:,:,i) = medfilt2(diff1(:,:,i),[3 3]);
    se=strel('disk',2);
    diff1(:,:,i) = imerode(diff1(:,:,i),se);
    diff1(:,:,i) = imdilate(diff1(:,:,i),se);
    diff1(:,:,i) = imdilate(diff1(:,:,i),se);
    imshow(diff1(:,:,i));
    M=[M getframe()];
end
movie2avi(M,'1_1.avi');


% blur, median
for i=1:size(ag,3)
    ag1(:,:,i) = im2double(ag(:,:,i));
    ag1(:,:,i) = filter2(fspecial('motion'),ag1(:,:,i));
    ag1(:,:,i) = filter2(fspecial('unsharp'),ag1(:,:,i));
end

bgMean1 = mean(ag1,3);
bgStd1 = std(ag1,1,3);
M=[];
for i=1:size(ag,3)
    diff1(:,:,i)= abs(ag1(:,:,i)-bgMean1)>2*bgStd1;
    diff1(:,:,i) = medfilt2(diff1(:,:,i),[3 3]);
    imshow(diff1(:,:,i));
    M=[M getframe()];
end
movie2avi(M,'1_2.avi');

%laplacian, erosion, dilation
for i=1:size(ag,3)
    ag1(:,:,i) = im2double(ag(:,:,i));
    ag2(:,:,i) = imfilter(ag1(:,:,i), fspecial('laplacian',0),'replicate');
    ag1(:,:,i)= ag1(:,:,i)-ag2(:,:,i);
end

bgMean1 = mean(ag1,3);
bgStd1 = std(ag1,1,3);
M=[];
for i=1:size(ag,3)
    diff1(:,:,i)= abs(ag1(:,:,i)-bgMean1)>2*bgStd1;
    se=strel('disk',1);
    diff1(:,:,i) = imdilate(diff1(:,:,i),se);
    se = strel('disk',1);
    diff1(:,:,i) = imerode(diff1(:,:,i),se);
    imshow(diff1(:,:,i));
    M=[M getframe()];
end
movie2avi(M,'1_3.avi');
