Code covered by the BSD License  

Highlights from
Motion Detector

Motion Detector

by

 

A video based function which detects when someone/something moves in the frame.

motiondetector2(error)
%Copywrite Abhilash Harpale (c) 2009.

%   MOTION DETECTOR VERSION 1.1
%   Based on grayscale images 
%   This version is not color based but is error prone.
%   'error' is proportional to the sensitivity required(around 75 to 90)
%   The program takes a few seconds to calibrate.
%   ENSURE THAT THERE IS NO MOTION WHEN THE PROGRAM IS CALIBRATING ITSELF.
function motiondetector2(error)
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


disp(' checking background noise, please wait...');

%adjusting for background noise

for n=1:50
    trigger(vid);
    temp=getdata(vid,1);
    f(:,:,mod(n,2)+1)=rgb2gray(temp);
    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));
if (nargin==0)
    error=2;
end
s=(s*(error+100))/100;
clear sm;
clc
disp('backbround sensitivity=');
disp(s);


disp('MOTION DETECTOR ON ->');

while(1)
    trigger(vid);
    temp=getdata(vid,1);
    %imshow(temp);
    f(:,:,mod(i,2)+1)=rgb2gray(temp);
    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
    %disp('difference');
    %disp(sum(sum(sum(g))));
    i=i+1;
end

Contact us