Vous êtes sur la page 1sur 3

India Accelerating the pace of engineering and science

Contact Us

How To Buy Create Account Log In

Products & Services Products & Services

Solutions

Academ ia

Support

User Com m unity

Events

Com pany

Computer Vision System Toolbox

Code Examples

Computer Vision System Toolbox


Tracking Cars Using Optical Flow
This demo tracks cars in a video by detecting motion using optical flow. The cars are segmented from the background by thresholding the motion vector magnitudes. Then, blob analysis is used to identify the cars. TRY OR BUY
Contact Sales Product Trial

Share

Pricing and Licensing

Contents
Initialization Tracking Cars in Video

Initialization
Create the System objects outside of the main video processing loop.

%O b j e c tf o rr e a d i n gv i d e of i l e . f i l e n a m e=' v i p t r a f f i c . a v i ' ; h V i d R e a d e r=v i s i o n . V i d e o F i l e R e a d e r ( f i l e n a m e ,' I m a g e C o l o r S p a c e ' ,' R G B ' , . . . ' V i d e o O u t p u t D a t a T y p e ' ,' s i n g l e ' ) ;

Optical flow object for estimating direction and speed of object motion.

h O p t i c a l F l o w=v i s i o n . O p t i c a l F l o w (. . . ' O u t p u t V a l u e ' ,' H o r i z o n t a la n dv e r t i c a lc o m p o n e n t si nc o m p l e xf o r m ' ,. . . ' R e f e r e n c e F r a m e D e l a y ' ,3 ) ;

Create two objects for analyzing optical flow vectors.

h M e a n 1=v i s i o n . M e a n ; h M e a n 2=v i s i o n . M e a n ( ' R u n n i n g M e a n ' ,t r u e ) ;

Filter object for removing speckle noise introduced during segmentation.

h M e d i a n F i l t=v i s i o n . M e d i a n F i l t e r ;

Morphological closing object for filling holes in blobs.

h c l o s e=v i s i o n . M o r p h o l o g i c a l C l o s e ( ' N e i g h b o r h o o d ' ,s t r e l ( ' l i n e ' , 5 , 4 5 ) ) ;

Create a blob analysis System object to segment cars in the video.

h b l o b=v i s i o n . B l o b A n a l y s i s ( . . . ' C e n t r o i d O u t p u t P o r t ' ,f a l s e ,' A r e a O u t p u t P o r t ' ,t r u e ,. . . ' B o u n d i n g B o x O u t p u t P o r t ' ,t r u e ,' O u t p u t D a t a T y p e ' ,' d o u b l e ' ,. . . ' M i n i m u m B l o b A r e a ' ,2 5 0 ,' M a x i m u m B l o b A r e a ' ,3 6 0 0 ,' M a x i m u m C o u n t ' ,8 0 ) ;

Morphological erosion object for removing portions of the road and other unwanted objects.

h e r o d e=v i s i o n . M o r p h o l o g i c a l E r o d e ( ' N e i g h b o r h o o d ' ,s t r e l ( ' s q u a r e ' , 2 ) ) ;

Create objects for drawing the bounding boxes and motion vectors.

h s h a p e i n s 1=v i s i o n . S h a p e I n s e r t e r ( ' B o r d e r C o l o r ' ,' C u s t o m ' ,. . . ' C u s t o m B o r d e r C o l o r ' ,[ 010 ] ) ; h s h a p e i n s 2=v i s i o n . S h a p e I n s e r t e r (' S h a p e ' , ' L i n e s ' ,. . .

' B o r d e r C o l o r ' ,' C u s t o m ' ,. . . ' C u s t o m B o r d e r C o l o r ' ,[ 2 5 52 5 50 ] ) ;

This object will write the number of tracked cars in the output image.

h t e x t i n s=v i s i o n . T e x t I n s e r t e r ( ' T e x t ' ,' % 4 d ' ,' L o c a t i o n ' , [ 11 ] ,. . . ' C o l o r ' ,[ 111 ] ,' F o n t S i z e ' ,1 2 ) ;

Create System objects to display the original video, motion vector video, the thresholded video and the final result.

s z=g e t ( 0 , ' S c r e e n S i z e ' ) ; p o s=[ 2 0s z ( 4 ) 3 0 02 0 02 0 0 ] ; h V i d e o 1=v i s i o n . V i d e o P l a y e r ( ' N a m e ' , ' O r i g i n a lV i d e o ' , ' P o s i t i o n ' , p o s ) ; p o s ( 1 )=p o s ( 1 ) + 2 2 0 ;%m o v et h en e x tv i e w e rt ot h er i g h t h V i d e o 2=v i s i o n . V i d e o P l a y e r ( ' N a m e ' , ' M o t i o nV e c t o r ' , ' P o s i t i o n ' , p o s ) ; p o s ( 1 )=p o s ( 1 ) + 2 2 0 ; h V i d e o 3=v i s i o n . V i d e o P l a y e r ( ' N a m e ' , ' T h r e s h o l d e dV i d e o ' , ' P o s i t i o n ' , p o s ) ; p o s ( 1 )=p o s ( 1 ) + 2 2 0 ; h V i d e o 4=v i s i o n . V i d e o P l a y e r ( ' N a m e ' , ' R e s u l t s ' , ' P o s i t i o n ' , p o s ) ; %I n i t i a l i z ev a r i a b l e su s e di np l o t t i n gm o t i o nv e c t o r s . l i n e R o w = 2 2 ; f i r s t T i m e=t r u e ; m o t i o n V e c G a i n =2 0 ; b o r d e r O f f s e t =5 ; d e c i m F a c t o r R o w=5 ; d e c i m F a c t o r C o l=5 ;

Tracking Cars in Video


Create the processing loop to track the cars in video.

w h i l e~ i s D o n e ( h V i d R e a d e r ) %S t o pw h e ne n do ff i l ei sr e a c h e d f r a m e =s t e p ( h V i d R e a d e r ) ; %R e a di n p u tv i d e of r a m e g r a y F r a m e=r g b 2 g r a y ( f r a m e ) ; o f V e c t o r s=s t e p ( h O p t i c a l F l o w ,g r a y F r a m e ) ; %E s t i m a t eo p t i c a lf l o w

%T h eo p t i c a lf l o wv e c t o r sa r es t o r e da sc o m p l e xn u m b e r s .C o m p u t et h e i r %m a g n i t u d es q u a r e dw h i c hw i l ll a t e rb eu s e df o rt h r e s h o l d i n g . y 1=o f V e c t o r s. *c o n j ( o f V e c t o r s ) ; %C o m p u t et h ev e l o c i t yt h r e s h o l df r o mt h em a t r i xo fc o m p l e xv e l o c i t i e s . v e l _ t h=0 . 5*s t e p ( h M e a n 2 ,s t e p ( h M e a n 1 ,y 1 ) ) ; %T h r e s h o l dt h ei m a g ea n dt h e nf i l t e ri tt or e m o v es p e c k l en o i s e . s e g m e n t e d O b j e c t s=s t e p ( h M e d i a n F i l t ,y 1> =v e l _ t h ) ; %T h i n o u tt h ep a r t so ft h er o a da n df i l lh o l e si nt h eb l o b s . s e g m e n t e d O b j e c t s=s t e p ( h c l o s e ,s t e p ( h e r o d e ,s e g m e n t e d O b j e c t s ) ) ; %E s t i m a t et h ea r e aa n db o u n d i n gb o xo ft h eb l o b s . [ a r e a ,b b o x ]=s t e p ( h b l o b ,s e g m e n t e d O b j e c t s ) ; %S e l e c tb o x e si n s i d eR O I( b e l o ww h i t el i n e ) . I d x=b b o x ( : , 1 )>l i n e R o w ; %B a s e do nb l o bs i z e s ,f i l t e ro u to b j e c t sw h i c hc a nn o tb ec a r s . %W h e nt h er a t i ob e t w e e nt h ea r e ao ft h eb l o ba n dt h ea r e ao ft h e %b o u n d i n gb o xi sa b o v e0 . 4( 4 0 % ) ,c l a s s i f yi ta sac a r . r a t i o=z e r o s ( l e n g t h ( I d x ) ,1 ) ; r a t i o ( I d x )=s i n g l e ( a r e a ( I d x , 1 ) ) . / s i n g l e ( b b o x ( I d x , 3 ) . * b b o x ( I d x , 4 ) ) ; r a t i o b=r a t i o>0 . 4 ; c o u n t=i n t 3 2 ( s u m ( r a t i o b ) ) ; b b o x ( ~ r a t i o b ,: )=i n t 3 2 ( 1 ) ; %D r a wb o u n d i n gb o x e sa r o u n dt h et r a c k e dc a r s . y 2=s t e p ( h s h a p e i n s 1 ,f r a m e ,b b o x ) ; %D i s p l a yt h en u m b e ro fc a r st r a c k e da n daw h i t el i n es h o w i n gt h eR O I . y 2 ( 2 2 : 2 3 , : , : ) =1 ; %T h ew h i t el i n e . %B a c k g r o u n df o rd i s p l a y i n gc o u n t y 2 ( 1 : 1 5 , 1 : 3 0 , : )=0 ; %N u m b e ro fc a r s

r e s u l t=s t e p ( h t e x t i n s ,y 2 ,c o u n t ) ;

%G e n e r a t ec o o r d i n a t e sf o rp l o t t i n gm o t i o nv e c t o r s . i ff i r s t T i m e [ RC ]=s i z e ( o f V e c t o r s ) ; %H e i g h ta n dw i d t hi np i x e l s R V=b o r d e r O f f s e t : d e c i m F a c t o r R o w : ( R b o r d e r O f f s e t ) ; C V=b o r d e r O f f s e t : d e c i m F a c t o r C o l : ( C b o r d e r O f f s e t ) ; [ YX ]=m e s h g r i d ( C V , R V ) ; f i r s t T i m e=f a l s e ; e n d %C a l c u l a t ea n dd r a wt h em o t i o nv e c t o r s . t m p=o f V e c t o r s ( R V , C V ). *m o t i o n V e c G a i n ; l i n e s=[ Y ( : ) ,X ( : ) ,Y ( : )+r e a l ( t m p ( : ) ) ,X ( : )+i m a g ( t m p ( : ) ) ] ; m o t i o n V e c t o r s=s t e p ( h s h a p e i n s 2 ,f r a m e ,l i n e s ) ; %D i s p l a yt h er e s u l t s s t e p ( h V i d e o 1 ,f r a m e ) ; s t e p ( h V i d e o 2 ,m o t i o n V e c t o r s ) ; s t e p ( h V i d e o 4 ,r e s u l t ) ; e n d r e l e a s e ( h V i d R e a d e r ) ; %O r i g i n a lv i d e o %V i d e ow i t hm o t i o nv e c t o r s %V i d e ow i t hb o u n d i n gb o x e s

s t e p ( h V i d e o 3 ,s e g m e n t e d O b j e c t s ) ;%T h r e s h o l d e dv i d e o

The output video shows the cars which were tracked by drawing boxes around them. The video also displays the number of tracked cars.

1994-2014 The MathWorks, Inc.

Site Help

Patents

Trademarks

Privacy Policy

Preventing Piracy Join the conversation

Vous aimerez peut-être aussi