1 /* Demo of modified Lucas-Kanade optical flow algorithm.
2 See the printf below */
5 #pragma package <opencv>
15 IplImage *image = 0, *grey = 0, *prev_grey = 0, *pyramid = 0, *prev_pyramid = 0, *swap_temp;
18 const int MAX_COUNT = 500;
19 CvPoint2D32f* points[2] = {0,0}, *swap_points;
25 int add_remove_pt = 0;
29 void on_mouse( int event, int x, int y, int flags, void* param )
35 y = image->height - y;
37 if( event == CV_EVENT_LBUTTONDOWN )
45 int main( int argc, char** argv )
47 CvCapture* capture = 0;
49 if( argc == 1 || (argc == 2 && strlen(argv[1]) == 1 && isdigit(argv[1][0])))
50 capture = cvCaptureFromCAM( argc == 2 ? argv[1][0] - '0' : 0 );
52 capture = cvCaptureFromAVI( argv[1] );
56 fprintf(stderr,"Could not initialize capturing...\n");
60 /* print a welcome message, and the OpenCV version */
61 printf ("Welcome to lkdemo, using OpenCV version %s (%d.%d.%d)\n",
63 CV_MAJOR_VERSION, CV_MINOR_VERSION, CV_SUBMINOR_VERSION);
65 printf( "Hot keys: \n"
66 "\tESC - quit the program\n"
67 "\tr - auto-initialize tracking\n"
68 "\tc - delete all the points\n"
69 "\tn - switch the \"night\" mode on/off\n"
70 "To add/remove a feature point click it\n" );
72 cvNamedWindow( "LkDemo", 0 );
73 cvSetMouseCallback( "LkDemo", on_mouse, 0 );
80 frame = cvQueryFrame( capture );
86 /* allocate all the buffers */
87 image = cvCreateImage( cvGetSize(frame), 8, 3 );
88 image->origin = frame->origin;
89 grey = cvCreateImage( cvGetSize(frame), 8, 1 );
90 prev_grey = cvCreateImage( cvGetSize(frame), 8, 1 );
91 pyramid = cvCreateImage( cvGetSize(frame), 8, 1 );
92 prev_pyramid = cvCreateImage( cvGetSize(frame), 8, 1 );
93 points[0] = (CvPoint2D32f*)cvAlloc(MAX_COUNT*sizeof(points[0][0]));
94 points[1] = (CvPoint2D32f*)cvAlloc(MAX_COUNT*sizeof(points[0][0]));
95 status = (char*)cvAlloc(MAX_COUNT);
99 cvCopy( frame, image, 0 );
100 cvCvtColor( image, grey, CV_BGR2GRAY );
107 /* automatic initialization */
108 IplImage* eig = cvCreateImage( cvGetSize(grey), 32, 1 );
109 IplImage* temp = cvCreateImage( cvGetSize(grey), 32, 1 );
110 double quality = 0.01;
111 double min_distance = 10;
114 cvGoodFeaturesToTrack( grey, eig, temp, points[1], &count,
115 quality, min_distance, 0, 3, 0, 0.04 );
116 cvFindCornerSubPix( grey, points[1], count,
117 cvSize(win_size,win_size), cvSize(-1,-1),
118 cvTermCriteria(CV_TERMCRIT_ITER|CV_TERMCRIT_EPS,20,0.03));
119 cvReleaseImage( &eig );
120 cvReleaseImage( &temp );
126 cvCalcOpticalFlowPyrLK( prev_grey, grey, prev_pyramid, pyramid,
127 points[0], points[1], count, cvSize(win_size,win_size), 3, status, 0,
128 cvTermCriteria(CV_TERMCRIT_ITER|CV_TERMCRIT_EPS,20,0.03), flags );
129 flags |= CV_LKFLOW_PYR_A_READY;
130 for( i = k = 0; i < count; i++ )
134 double dx = pt.x - points[1][i].x;
135 double dy = pt.y - points[1][i].y;
137 if( dx*dx + dy*dy <= 25 )
147 points[1][k++] = points[1][i];
148 cvCircle( image, cvPointFrom32f(points[1][i]), 3, CV_RGB(0,255,0), -1, 8,0);
153 if( add_remove_pt && count < MAX_COUNT )
155 points[1][count++] = cvPointTo32f(pt);
156 cvFindCornerSubPix( grey, points[1] + count - 1, 1,
157 cvSize(win_size,win_size), cvSize(-1,-1),
158 cvTermCriteria(CV_TERMCRIT_ITER|CV_TERMCRIT_EPS,20,0.03));
162 CV_SWAP( prev_grey, grey, swap_temp );
163 CV_SWAP( prev_pyramid, pyramid, swap_temp );
164 CV_SWAP( points[0], points[1], swap_points );
166 cvShowImage( "LkDemo", image );
187 cvReleaseCapture( &capture );
188 cvDestroyWindow("LkDemo");