Update to 2.0.0 tree from current Fremantle build
[opencv] / samples / octave / motempl.m
1 #! /usr/bin/env octave
2
3 cv
4 highgui
5
6 CLOCKS_PER_SEC = 1.0
7 MHI_DURATION = 1;
8 MAX_TIME_DELTA = 0.5;
9 MIN_TIME_DELTA = 0.05;
10 N = 4;
11 buf = range(10) 
12 last = 0;
13 mhi = []; # MHI
14 orient = []; # orientation
15 mask = []; # valid orientation mask
16 segmask = []; # motion segmentation map
17 storage = []; # temporary storage
18
19 function update_mhi( img, dst, diff_threshold )
20   global last
21   global mhi
22   global storage
23   global mask
24   global orient
25   global segmask
26   timestamp = time.clock()/CLOCKS_PER_SEC; # get current time in seconds
27   size = cvSize(img.width,img.height); # get current frame size
28   idx1 = last;
29   if (! mhi || mhi.width != size.width || mhi.height != size.height)
30     for i=0:N-1,
31       buf[i] = cvCreateImage( size, IPL_DEPTH_8U, 1 );
32       cvZero( buf[i] );
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 );
38
39       cvCvtColor( img, buf[last], CV_BGR2GRAY ); # convert frame to grayscale
40       idx2 = (last + 1) % N; # index of (last - (N-1))th frame
41       last = idx2;
42       silh = buf[idx2];
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 );
48       cvZero( dst );
49       cvMerge( mask, [], [], [], dst );
50       cvCalcMotionGradient( mhi, mask, orient, MAX_TIME_DELTA, MIN_TIME_DELTA, 3 );
51       if( not storage )
52         storage = cvCreateMemStorage(0);
53       else
54         cvClearMemStorage(storage);
55         seq = cvSegmentMotion( mhi, segmask, storage, timestamp, MAX_TIME_DELTA );
56         for i=-1:seq.total-1,
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);
60             magnitude = 100.;
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
64               continue;
65             endif
66           endif
67           color = CV_RGB(255,0,0);
68           magnitude = 30.;
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 )
77             continue;
78           endif
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))), \
84                  color, 3, CV_AA, 0 );
85         endfor
86       endif
87     endfor
88   endif
89 endfunction
90
91 motion = 0;
92 capture = 0;
93
94 if (size(argv, 1)==1)
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, :) ); 
100 endif
101
102 if (!capture)
103   print "Could not initialize capturing..."
104   exit(-1)
105 endif
106
107 cvNamedWindow( "Motion", 1 );
108 while (true)
109   image = cvQueryFrame( capture );
110   if( image )
111     if( ! motion )
112       motion = cvCreateImage( cvSize(image.width,image.height), 8, 3 );
113       cvZero( motion );
114       motion.origin = image.origin;
115     endif
116     update_mhi( image, motion, 30 );
117     cvShowImage( "Motion", motion );
118     if( cvWaitKey(10) != -1 )
119       break;
120     endif
121   else
122     break
123   endif
124 endwhile
125
126 cvDestroyWindow( "Motion" );