14 orient = []; # orientation
15 mask = []; # valid orientation mask
16 segmask = []; # motion segmentation map
17 storage = []; # temporary storage
19 function update_mhi( img, dst, diff_threshold )
26 timestamp = time.clock()/CLOCKS_PER_SEC; # get current time in seconds
27 size = cvSize(img.width,img.height); # get current frame size
29 if (! mhi || mhi.width != size.width || mhi.height != size.height)
31 buf[i] = cvCreateImage( size, IPL_DEPTH_8U, 1 );
33 mhi = cvCreateImage( size, IPL_DEPTH_32F, 1 );
34 cvZero( mhi ); # clear MHI at the beginning
35 orient = cvCreateImage( size, IPL_DEPTH_32F, 1 );
36 segmask = cvCreateImage( size, IPL_DEPTH_32F, 1 );
37 mask = cvCreateImage( size, IPL_DEPTH_8U, 1 );
39 cvCvtColor( img, buf[last], CV_BGR2GRAY ); # convert frame to grayscale
40 idx2 = (last + 1) % N; # index of (last - (N-1))th frame
43 cvAbsDiff( buf[idx1], buf[idx2], silh ); # get difference between frames
44 cvThreshold( silh, silh, diff_threshold, 1, CV_THRESH_BINARY ); # and threshold it
45 cvUpdateMotionHistory( silh, mhi, timestamp, MHI_DURATION ); # update MHI
46 cvCvtScale( mhi, mask, 255./MHI_DURATION,
47 (MHI_DURATION - timestamp)*255./MHI_DURATION );
49 cvMerge( mask, [], [], [], dst );
50 cvCalcMotionGradient( mhi, mask, orient, MAX_TIME_DELTA, MIN_TIME_DELTA, 3 );
52 storage = cvCreateMemStorage(0);
54 cvClearMemStorage(storage);
55 seq = cvSegmentMotion( mhi, segmask, storage, timestamp, MAX_TIME_DELTA );
57 if( i < 0 ) # case of the whole image
58 comp_rect = cvRect( 0, 0, size.width, size.height );
59 color = CV_RGB(255,255,255);
61 else # i-th motion component
62 comp_rect = seq[i].rect
63 if( comp_rect.width + comp_rect.height < 100 ) # reject very small components
67 color = CV_RGB(255,0,0);
69 silh_roi = cvGetSubRect(silh, comp_rect);
70 mhi_roi = cvGetSubRect( mhi, comp_rect );
71 orient_roi = cvGetSubRect( orient, comp_rect );
72 mask_roi = cvGetSubRect( mask, comp_rect );
73 angle = cvCalcGlobalOrientation( orient_roi, mask_roi, mhi_roi, timestamp, MHI_DURATION);
74 angle = 360.0 - angle; # adjust for images with top-left origin
75 count = cvNorm( silh_roi, [], CV_L1, [] ); # calculate number of points within silhouette ROI
76 if( count < comp_rect.width * comp_rect.height * 0.05 )
79 center = cvPoint( (comp_rect.x + comp_rect.width/2),
80 (comp_rect.y + comp_rect.height/2) );
81 cvCircle( dst, center, cvRound(magnitude*1.2), color, 3, CV_AA, 0 );
82 cvLine( dst, center, cvPoint( cvRound( center.x + magnitude*cos(angle*CV_PI/180)),
83 cvRound( center.y - magnitude*sin(angle*CV_PI/180))), \
95 capture = cvCreateCameraCapture( 0 )
96 elseif (size(argv, 1)==2 && all(isdigit(argv(1, :))))
97 capture = cvCreateCameraCapture( int32(argv(1, :)) )
98 elseif (size(argv, 1)==2)
99 capture = cvCreateFileCapture( argv(1, :) );
103 print "Could not initialize capturing..."
107 cvNamedWindow( "Motion", 1 );
109 image = cvQueryFrame( capture );
112 motion = cvCreateImage( cvSize(image.width,image.height), 8, 3 );
114 motion.origin = image.origin;
116 update_mhi( image, motion, 30 );
117 cvShowImage( "Motion", motion );
118 if( cvWaitKey(10) != -1 )
126 cvDestroyWindow( "Motion" );