1 #include <opencv2/video/tracking.hpp>
2 #include <opencv2/highgui/highgui.hpp>
7 "This program demonstrate dense \"Farneback\n optical flow\n"
8 "It read from camera 0, and shows how to use and display dense Franeback optical flow\n"
13 void drawOptFlowMap(const CvMat* flow, CvMat* cflowmap, int step,
14 double scale, CvScalar color)
17 for( y = 0; y < cflowmap->rows; y += step)
18 for( x = 0; x < cflowmap->cols; x += step)
20 CvPoint2D32f fxy = CV_MAT_ELEM(*flow, CvPoint2D32f, y, x);
21 cvLine(cflowmap, cvPoint(x,y), cvPoint(cvRound(x+fxy.x), cvRound(y+fxy.y)),
23 cvCircle(cflowmap, cvPoint(x,y), 2, color, -1, 8, 0);
27 int main(int argc, char** argv)
29 CvCapture* capture = cvCreateCameraCapture(0);
30 CvMat* prevgray = 0, *gray = 0, *flow = 0, *cflow = 0;
37 cvNamedWindow("flow", 1);
41 int firstFrame = gray == 0;
42 IplImage* frame = cvQueryFrame(capture);
47 gray = cvCreateMat(frame->height, frame->width, CV_8UC1);
48 prevgray = cvCreateMat(gray->rows, gray->cols, gray->type);
49 flow = cvCreateMat(gray->rows, gray->cols, CV_32FC2);
50 cflow = cvCreateMat(gray->rows, gray->cols, CV_8UC3);
52 cvCvtColor(frame, gray, CV_BGR2GRAY);
56 cvCalcOpticalFlowFarneback(prevgray, gray, flow, 0.5, 3, 15, 3, 5, 1.2, 0);
57 cvCvtColor(prevgray, cflow, CV_GRAY2BGR);
58 drawOptFlowMap(flow, cflow, 16, 1.5, CV_RGB(0, 255, 0));
59 cvShowImage("flow", cflow);
65 CV_SWAP(prevgray, gray, temp);
68 cvReleaseCapture(&capture);