% usuage motion_detect_hist(avifilename,5);

function [] = motion_detect_hist(aviFileName,win_size);

close all;
movieObj = aviread(aviFileName);
noOfFrameImages = size(movieObj,2)

for i = 1:1:noOfFrameImages
    directory = 'images\frame';
    filename = strcat(directory,num2str(i),'.jpg');
    
    imwrite(movieObj(i).cdata,filename,'jpg');
    
end


File1 = strcat(directory,num2str(1),'.jpg');

for i = 2:1:noOfFrameImages
    
    File2 = strcat(directory,num2str(i),'.jpg');
     
          
    dsim(i-1, 1) = histo(File1,File2);
        
       
    File1 = File2;
end

k = 1;
for i = 1:1:(noOfFrameImages - (2*win_size) - 1)
    slidewin = dsim([i:(i+2*win_size)],1);
    answer =  threshold(slidewin, 5);
    
    if answer == 1
        dissimilar(i,:) = dsim(((i + 2*win_size) - win_size),1);
    end
end
x = 1;
for i = 1:1:size(dsim)
    enigma = find(dissimilar == dsim(i,1));
    if isempty(enigma) == 0
        suit(x,2) = dsim(i,1);
        suit(x,1) = i;
        x = x+1;
    end
end

figure
plot(dsim(:,1))
hold on
plot(suit(:,1),suit(:,2),'*')
TITLE('Motion detection using Histogram')


