Code covered by the BSD License  

Highlights from
Intellicam(Real time moving object tracker)

image thumbnail

Intellicam(Real time moving object tracker)

by

 

The above code used to track the real time moving objects

lokfinal.m
%LOK's Matlab Coding for real time moving object tracking
%Intellicam-Real time object tracking

ser = serial('COM1','BaudRate',9600);
%to track the object if object moves out of camera range interface PIC16F877a/887 to pc and interface stepper motor and mount camera on it 
%let matlab send serial data 1 r 2 to pic serially to turn stepper motor left or right

fopen(ser);
%open serial object

% Create video input object. 
vid = videoinput('winvideo',1);

set(vid,'ReturnedColorSpace','rgb');
%if you are acquiring video from survillance camera through tv tuner card use this 
%Frame rate is good for such camera's
% Set video input object properties for this Intellicam project.
% Note that example uses both SET method and dot notation method.
set(vid,'TriggerRepeat',Inf);
vid.FrameGrabInterval = 5;
% Set value of a video source property.
vid_src = getselectedsource(vid);
set(vid_src,'Tag','Intellicam setup');

% Create a figure window.
figure; 

% Start acquiring frames.
start(vid)
% Calculate difference image and display it.
while(vid.FramesAcquired<=1000) % Stop after 1000 frames
    data = getdata(vid,2); 
    diff_im = imabsdiff(data(:,:,:,1),data(:,:,:,2));
    diff = rgb2gray(diff_im);
    %diff_range = rangefilt(diff);
    %you can use the range filter for better analysis
    diff_bw = im2bw(diff,0.2);
    %noi_red = medfilt2(diff_bw,[3 3]);
    %if more noise then use median filter or weiner filt to remove salt n pepper noise
    %ulta = imadjust(diff_bw,[0;1],[1;0]);
    bw2 = imfill(diff_bw,'holes');%you can specify any other location
    s  = regionprops(bw2, 'centroid');
    cd = s.Centroid
    centroids = cat(1, s.Centroid);
    imshow(data(:,:,:,2));
    %imshow(diff_bw)
    hold(imgca,'on');
    plot(imgca,centroids(:,1),centroids(:,2),'g*');
   %imshow(diff_bw);
   %hold on;
   rectangle('Position',[cd 60 33],'LineWidth',1,'EdgeColor','b');%this is for edges
   hold(imgca,'off');
   
   %our default camera width x height is around 352 x 288
   %so width is 352 so i will set the threshold to 20 at left n 330 r 340 at the right
   %when ever my centroid reaches those threshold i will send 1 r 2 serially to pic to intimate it to rotate left or right

   if (cd(1)<=20)
     fprintf(ser, '%s','1');
          %cd(1) because we r considering width i.e X,u can change your threshold's
  elseif (cd(1) >= 330)
      fprintf(ser, '%s','2');
    end  
  %for normal webcam this program will work fine if you interface PIC then un comment required
end
fclose(ser);
stop(vid)


%build and burn this assembly code into pic16f887a removing percentage symbol and connect stepper motor to port A-0to4 bits
%mount camera on stepper motor 
%;*************************************************************
%list      p=16f877a      ; list directive to define processor
%#include p16f877a.inc        ; processor specific variable definitions
%
%var1 EQU 0x20
%var2 EQU 0x21
%dat  equ 0x113
%dat1 equ 0x114
%
%;**********************************************************************
%		ORG     0x000             ; processor reset vector
%		clrf    PCLATH            ; ensure page bits are cleared
 % 		goto    main              ; go to beginning of program
%
%main
%
%;    BANKSEL ANSEL
%	;MOVLW 0X00
%;	clrf ANSEL
 %   banksel TRISD
  %  CLRF TRISD
%	BCF INTCON,7   
%	BANKSEL TRISC
%	MOVLW B'11000000'
%	MOVWF TRISC
%	
%	MOVLW .25
%	MOVWF SPBRG
%	BSF TXSTA,BRGH 
 %   	BCF TXSTA,SYNC
%	BSF TXSTA,TXEN
%
%	BANKSEL PORTB
%	BSF RCSTA,SPEN
%	BCF RCSTA,ADDEN
%	
%	BANKSEL TXREG
%	
%	MOVLW 'L'  ; Transmit 'L' to Laptop/Desktop
%	CALL SEND
%
%	MOVLW 'O' ; Transmit '0' to Laptop/Desktop
%	CALL SEND
%
%	MOVLW 'K' ; Transmit 'K' to Laptop/Desktop
%	CALL SEND
%
%	MOVLW 'S' ; Transmit 's' to Laptop/Desktop
%	CALL SEND
%
%	BANKSEL RCSTA
%	MOVLW 0X90
%	MOVWF RCSTA
%	
%LOOP
 %   
%	BANKSEL PIR1 
%	BTFSS PIR1,RCIF ; check whether we have Recieved data from Laptop/Desktop
%	GOTO LOOP
%	BANKSEL RCREG	; Recieved data from Laptop/Desktop
%	MOVF RCREG,W
%	CALL SEND 		; Transmitting the recieved data to Laptop/Desktop
 %   call check
 %   BANKSEL EEDATA
  %  MOVF dat,W
%    CALL next
%    GOTO LOOP 		; Recieve the next data
%	GOTO $
%
%check
%    bcf STATUS,Z
%    SUBLW .49
%    BTFSS STATUS,Z
%    RETURN
%	call st1
%    RETURN
%next  
%      BCF STATUS,Z    
%      SUBLW .50
%     BTFSS STATUS,Z
%     goto LOOP
%    call st2
%    return
%
%SEND
%	BANKSEL TXREG
%	MOVWF TXREG
%    BANKSEL EEDATA
%    movwf dat
%    movwf dat1
%    MOVWF PORTD
%WAIT
%	BANKSEL TXSTA
%	BTFSS TXSTA,TRMT
%	GOTO WAIT	
%	RETURN
%
%st1
%         BANKSEL PORTA
%    	 CLRF    PORTA
%    	BANKSEL TRISA
%    	movlw b'11110000'
%        MOVWF TRISA	
%    	BCF STATUS,RP0
%back1
%	MOVLW B'00000001'
%	MOVWF PORTA
%	call delay
%	MOVLW B'00000010'
%	MOVWF PORTA
%	call delay	
%	MOVLW B'00000100'
%	MOVWF PORTA
%	call delay	
%	MOVLW B'00001000'
%	MOVWF PORTA
%	call delay	
%	;goto back1
%	return
%
%st2
 %        BANKSEL PORTA
%    	 CLRF    PORTA
%    	BANKSEL TRISA
%    	movlw b'11110000'
 %       MOVWF TRISA	
%	    BCF STATUS,RP0
%back2
%	MOVLW B'00001000'
%	MOVWF PORTA
%	call delay
%	MOVLW B'00000100'
%	MOVWF PORTA
%	call delay	
%	MOVLW B'00000010'
%	MOVWF PORTA
%	call delay	
%	MOVLW B'00000001'
%	MOVWF PORTA
%	call delay	
%	;goto back2
%	return
%
%delay
%	movlw .250
%	movwf var2
%loop1
%	movlw .250
 %       movwf var1
%loop
%	NOP
%	DECFSZ var1,f
 %   goto loop
%	DECFSZ var2,f
 %   goto loop1
%	return
%END
%$$$$$$$$$$$$$$$$$$$$$$$$$$$$$the End$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$444

Contact us