% This fnction is used to get a frame difference sequence PDIFF(x,y,:,k),
% which k is the frame number. here PDIFF records every pixel's variation curve along time axis at coordinat
% (x,y). 

function pixdif = getpixdiff(movieObj)
% movieObj = aviread('hall_monitor.avi');

T = 2;
noOfFrameImages = size(movieObj,2);

for k =1:1:noOfFrameImages
    image = movieObj(k).cdata;
    I(:,:,:,k)=image;
end
im = I(:,:,:,1);
pre_frame = double(im);

% compute every adjacent frame pixel value difference
for k = 2: noOfFrameImages	
	im = I(:,:,:,k);
    cur_frame = double(im);
    frameDif = cur_frame - pre_frame; 
    i = size(frameDif,1);
    j = size(frameDif,2);
    h = size(frameDif,3);
    
    frameDif = reshape(frameDif,i*j,h);
    for i1 = 1:size(frameDif,1)
        dist = norm(frameDif(i1,:));
        if dist<T %if pixel value difference is below threshold T
            frameDif(i1,:) = 0;
        else
            frameDif(i1,:) = 255;
        end
        
    end
    frameDif = uint8(frameDif);
    frameDif = reshape(frameDif,i,j,h);
%    figure;
 %   imshow(frameDif);
    
%     frameDif = erodeFramediff(frameDif);% here the filter is used to denoise for every frame difference image
    
    PDIFF(:,:,:,k-1) = frameDif; %PDIFF will be a frame difference sequence
    
    pre_frame = cur_frame;
    
end

figure;
imshow(frameDif);

numrow = i; %image frame rows
numcol = j; %image frame columns

save result noOfFrameImages I PDIFF numrow numcol framedif
return

