%Copywrite Abhilash Harpale (c) 2009.
%%%%%%%%MOTION DETECTOR VERSION 1.2%%%%%%%%
% Based on black and white images(binary images),highly accurate.
% 's' is the background noise
% The program takes a few seconds to calibrate.
% ENSURE THAT THERE IS NO MOTION WHEN THE PROGRAM IS CALIBRATING ITSELF.
% If you do not enter 's' the program calibrates itself to adjust for the
% back ground noise.
function motiondetector3(s)
vid=videoinput('winvideo',1);
triggerconfig(vid,'manual');
set(vid,'FramesPerTrigger',1);
set(vid,'TriggerRepeat', Inf);
set(vid,'ReturnedColorSpace','rgb');
start(vid);
i=0;
j=0;
f=zeros(480,640,2);
load splat
if(nargin==0)
disp(' checking background noise, please wait...');
%adjusting for background noise.
for n=1:50
trigger(vid);
temp=getdata(vid,1);
temp=rgb2gray(temp);
l=graythresh(temp);
f(:,:,mod(n,2)+1)=im2bw(temp,l);
g=f(:,:,1)~=f(:,:,2);
sm(1,n)=backgroundcheck(g);
end
clear g;
clear temp;
f=zeros(480,640,2);
s=max(sm(25:50));
s=s*1.01;
clear sm;
clc
disp('backbround sensitivity=');
disp(s);
end
%Motion will be detected if the image has more noise than the background noise present when
%the function was started.
while(1)
trigger(vid);
temp=getdata(vid,1);
temp=rgb2gray(temp);
l=graythresh(temp);
f(:,:,mod(i,2)+1)=im2bw(temp,l);
g=f(:,:,1)~=f(:,:,2);
if(sum(sum(g))>(((s)/100)*480*640))
disp('motion detected');
j=j+1;
end
imshow(g);
if(mod(j,6)==0)
clc
disp('MOTION DETECTOR ON ->');
y=y(1:3000);
for a=1:4
wavplay(y,4500,'sync');
end
end
%The alarm is played if motion is detected more than 6 times.
%disp('difference');
%disp(sum(sum(sum(g))));
i=i+1;
end