image thumbnail

Survillance System using Motion Detection



26 May 2004 (Updated )

Survillance system using motion detection.

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % %
%This MATLAB code is used to detect motion                                          % % 
% It apply the sum of absolute difference algorithm SAD                             % %
% When motion is detected i)write date,time and frame number into log file  log.txt % %
%                         ii)activate serial port for external circuit interface    % %
%                         iii)save images with motion into a movie                  % %
% NB: This code is running with MATLAB version 6.5 or higher                        % %                
% NB: To run the video object DIRECT X V9.0 or higer is required                    % %          
% DIRECT X is available on this link      % %      
% This code was written by ABDULLA ALAWADI   ID:1108                                % % 
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % %

vobj=videoinput('winvideo',2)                       %initializes a video object to connect to camera
s=serial ('com2')                                   %initializes a serial port object com1
fopen(s)                                            %connect and open serial port object
qq=0;                                               %initializes film counter
a=1;                                                %initializes frame number counter
preview(vobj);                                      %display camera output on the screen
threshold = get(handles.slider1,'value')            %read the value of threshold from GUI slider

%start a for loop that equal the number of images usually infinity
for l = 1:inf
    flag=get(handles.togglebutton1,'value');        %This flag is used to check if stop button was pressed
    if (flag==1)
        v= getsnapshot(vobj);                       %read image from camera and save it in variable v
        w= getsnapshot(vobj);                       %read image from camera and save it in variable w
        x= rgb2gray (v);                            %convert image to gray scale
        y= rgb2gray(w);                             %convert image to gray scale
        z = imabsdiff(x,y);                         %get absolute diffrence between both images
        zz= sum(z,1);                               %calculate SAD
        zzz= sum(zz)/ 76800;                        %scale the value of SAD by dividing by number of pixels
        h(a) = zzz;                                 %put values of SAD in array h
        hh=[ h(a-1) h(a) ];                         %calculate variance value
        var_values(a)=var_value;                    %put values of variance in array var_value
        if (var_value>threshold)                    %check if value of variance greater than threshold  
            fid = fopen('log.txt','a');             %write date,time and frame number into log file  log.txt
            fprintf(fid,'MOTION WAS DETECTED AT %-100.20s\n',time);
            fprintf(fid,'in frame number %-110.5d\n',a);
            fwrite(s,127,'uint');                   %write a value to activate serial port
            imshow(v);                              %display image with motion on screen
            film(qq) = im2frame(v);                 %add image to film structure
        end                                         %end of if statment
        break                                       %if stop button was pressed exit for loop
    end                                             %end of if statment (flag)
end                                                 %end of for loop
fclose(s);                                          %close serial port object
delete(s);                                          %delete serial port object
delete(vobj);                                       %delete video object
savefile = 'var.mat';                               %initialize file name
save(savefile,'var_values','film')                   %save variables var_values and film to file name

Contact us