image thumbnail

Survillance System using Motion Detection

by

 

26 May 2004 (Updated )

Survillance system using motion detection.

corrolation_algorithm.m
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % %
%This MATLAB code is used to detect motion                                          % % 
% It apply the 2 dimensions cross correlation algorithm 2D corr                     % %
% 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 http://www.microsoft.com/windows/directx/      % %      
% This code was written by ABDULLA ALAWADI   ID:1108                                % % 
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % %


tic
cla;
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.slider2,'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)
        a=a+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
        %dividing a 240 by 320 into 4 pictures
        x1=x(1:144,1:176);
        x2=x(1:144,177:352);
        x3=x(145:288,177:352);
        x4=x(145:288,1:176);
        
        y= rgb2gray(w);                             %convert image to gray scale
        %dividing a 240 by 320 into 4 pictures
        y1=y(1:144,1:176);
        y2=y(1:144,177:352);
        y3=y(145:288,177:352);
        y4=y(145:288,1:176);
        
        %mesure of 2 dimensions cross correlation for each picture
        z= corr2(x,y);
        z1= corr2(x1,y1);
        z2= corr2(x2,y2);
        z3= corr2(x3,y3);
        z4= corr2(x4,y4);
        
        %put values of correlation into arrays
        h(a) = z;
        h1(a)=z1;
        h2(a)=z2;
        h3(a)=z3;
        h4(a)=z4;
        
        %measure the minimum value for correlation
        hh=[z1,z2,z3,z4];
        zz=min(hh);
        hmin(a)=zz;                                         %put the values of min. corrolation in array hmin
        
        kk=[hmin(a-1),hmin(a) ];                            %calculate variance value
        var_value=var(kk,1);
        var_values(a)=var_value;                            %put values of variance in array var_values
        
        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
            time=datestr(now)
            fprintf(fid,'MOTION WAS DETECTED AT %-100.20s\n',time);
            fprintf(fid,'in frame number %-110.5d\n',a);
            fclose(fid);
            
            fwrite(s,127,'uint')                            %write a value to activate serial port
            
            qq=qq+1;
            imshow(v);                                      %display image with motion on screen
            film(qq) = im2frame(v);                         %add image to film structure
            %image(film.cdata)
            %colormap(film.colormap) 
        else  
        end                                                 %end of if statment
    else
        break                                               %if stop button was pressed exit for loop
    end                                                     %end of if statment (flag)
    toc
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