%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% This function 'histogramBasedMotionDetector' detects motion in an input .avi movie file using the concept of vector distance between
% two consecutive frames of the movie based on histogram differences. Histogram is the distribution of intensity level amongst the
% pixels in the frame. This function also used local threshold method based on 'window' concept, where the euclidean distance of
% the frame under analysis with respect to its consecutive frame is compared to the mean value of the euclidean distances of the 
% frames in the left window of 'n' frames and also compared to the mean value of the euclidean distances of the frames in the right
% window of 'n' frames, and if it is greater, then motion has occured at this particular frame number in the movie file.
%
% Programmer : Nilesh Ghubade (nileshg@temple.edu)
% Date : March 2003
% Advisor : Dr.Longin Jan Latecki (latecki@temple.edu )
% Course : CIS 750 (Spring 2003) --> Video Analysis and Video Mining.
% Assignment : Histogram based Motion detector.
% Department : Computer and Information Science Department, Temple University, Philadelphia, PA-19122, USA
%
% Input arguments:-
% aviFileName = Input .avi movie file from whom motion is to be detected.
% windowSize = The window of frames to be used for computation in the local threshold method.
%
% Output : -
% vectorOfShotFrames = This vector (obtained from 'window' method) contains the frame numbers where motion occurs in the given movie.
% A plot of vector distance between HISTOGRAMS of consecutive images.
% A plot of frames detected as having motion, obtained using window method.
% 
% Example : -
% 1) [vectorOfShotFrames] = histogramBasedMotionDetector('security1.avi',10);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


function [vectorOfShotFrames] = histogramBasedMotionDetector(aviFileName,windowSize)
movieObj = aviread(aviFileName);
noOfFrameImages = size(movieObj,2);


for count = 1:1:noOfFrameImages
    fileName = strcat('frame',num2str(count),'.bmp');
    imwrite(movieObj(count).cdata,fileName,'bmp');
end

for count = 1:1:(noOfFrameImages-1)
    fileName = strcat('frame',num2str(count),'.bmp');
    [firstImgBinCount,firstImgColormap] = imhist(imread(fileName));
    fileName = strcat('frame',num2str(count + 1),'.bmp');    
    [secondImgBinCount,secondImgColormap] = imhist(imread(fileName));  

    vectorOfImgDist(count) = norm(double(secondImgBinCount) - double(firstImgBinCount));
end

figure;plot(vectorOfImgDist);xlabel('Frame Number');title('HistogramBasedMotionDetector: Plot of vector distance between HISTOGRAMS of consecutive images');

vectorOfShotFrames = zeros(noOfFrameImages-1,1);
for frameUnderAnalysis = (windowSize+1):(noOfFrameImages-windowSize-1)
    leftWindowMeanBinCount = mean(vectorOfImgDist(frameUnderAnalysis-windowSize:frameUnderAnalysis-1));
    rightWindowMeanBinCount = mean(vectorOfImgDist(frameUnderAnalysis+1:frameUnderAnalysis+windowSize));
    
    if ((vectorOfImgDist(frameUnderAnalysis) > leftWindowMeanBinCount) & (vectorOfImgDist(frameUnderAnalysis) > rightWindowMeanBinCount))
        vectorOfShotFrames(frameUnderAnalysis) = vectorOfImgDist(frameUnderAnalysis);
    end
end


figure;plot(vectorOfShotFrames);xlabel('Frame Number');title('HistogramBasedMotionDetector: Plot of frames indicating motion in given video file');


for count = 1:1:noOfFrameImages
    fileName = strcat('frame',num2str(count),'.bmp');
    delete(fileName);
end

return;