2011-05-22 02:32:34 +08:00
|
|
|
#include "opencv2/video/tracking.hpp"
|
|
|
|
#include "opencv2/highgui/highgui.hpp"
|
2012-05-30 17:00:32 +08:00
|
|
|
#include "opencv2/imgproc/imgproc_c.h"
|
2010-12-02 17:40:58 +08:00
|
|
|
#include <stdio.h>
|
2012-06-15 21:04:17 +08:00
|
|
|
|
|
|
|
static void help(void)
|
2010-12-02 17:40:58 +08:00
|
|
|
{
|
2012-06-15 21:04:17 +08:00
|
|
|
printf(
|
2011-06-03 22:53:38 +08:00
|
|
|
"\n This program demonstrate dense \"Farneback\n optical flow\n"
|
2012-06-15 21:04:17 +08:00
|
|
|
"It read from camera 0, and shows how to use and display dense Franeback optical flow\n"
|
2011-06-03 22:53:38 +08:00
|
|
|
"Usage: \n"
|
|
|
|
"./fback_c \n");
|
2010-05-12 01:44:00 +08:00
|
|
|
|
2010-12-02 17:40:58 +08:00
|
|
|
}
|
2012-06-15 21:04:17 +08:00
|
|
|
|
|
|
|
static void drawOptFlowMap(const CvMat* flow, CvMat* cflowmap, int step,
|
2010-05-12 01:44:00 +08:00
|
|
|
double scale, CvScalar color)
|
|
|
|
{
|
|
|
|
int x, y;
|
|
|
|
for( y = 0; y < cflowmap->rows; y += step)
|
|
|
|
for( x = 0; x < cflowmap->cols; x += step)
|
|
|
|
{
|
|
|
|
CvPoint2D32f fxy = CV_MAT_ELEM(*flow, CvPoint2D32f, y, x);
|
|
|
|
cvLine(cflowmap, cvPoint(x,y), cvPoint(cvRound(x+fxy.x), cvRound(y+fxy.y)),
|
|
|
|
color, 1, 8, 0);
|
|
|
|
cvCircle(cflowmap, cvPoint(x,y), 2, color, -1, 8, 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-06-15 21:04:17 +08:00
|
|
|
int main( int argc, char** argv )
|
2010-05-12 01:44:00 +08:00
|
|
|
{
|
|
|
|
CvCapture* capture = cvCreateCameraCapture(0);
|
|
|
|
CvMat* prevgray = 0, *gray = 0, *flow = 0, *cflow = 0;
|
|
|
|
|
2010-12-02 17:40:58 +08:00
|
|
|
help();
|
|
|
|
|
2010-05-12 01:44:00 +08:00
|
|
|
if( !capture )
|
|
|
|
return -1;
|
2012-06-15 21:04:17 +08:00
|
|
|
|
2010-05-12 01:44:00 +08:00
|
|
|
cvNamedWindow("flow", 1);
|
2012-06-15 21:04:17 +08:00
|
|
|
|
2010-05-12 01:44:00 +08:00
|
|
|
for(;;)
|
|
|
|
{
|
|
|
|
int firstFrame = gray == 0;
|
|
|
|
IplImage* frame = cvQueryFrame(capture);
|
|
|
|
if(!frame)
|
|
|
|
break;
|
|
|
|
if(!gray)
|
|
|
|
{
|
|
|
|
gray = cvCreateMat(frame->height, frame->width, CV_8UC1);
|
|
|
|
prevgray = cvCreateMat(gray->rows, gray->cols, gray->type);
|
|
|
|
flow = cvCreateMat(gray->rows, gray->cols, CV_32FC2);
|
|
|
|
cflow = cvCreateMat(gray->rows, gray->cols, CV_8UC3);
|
|
|
|
}
|
|
|
|
cvCvtColor(frame, gray, CV_BGR2GRAY);
|
2012-06-15 21:04:17 +08:00
|
|
|
|
2010-05-12 01:44:00 +08:00
|
|
|
if( !firstFrame )
|
|
|
|
{
|
|
|
|
cvCalcOpticalFlowFarneback(prevgray, gray, flow, 0.5, 3, 15, 3, 5, 1.2, 0);
|
|
|
|
cvCvtColor(prevgray, cflow, CV_GRAY2BGR);
|
|
|
|
drawOptFlowMap(flow, cflow, 16, 1.5, CV_RGB(0, 255, 0));
|
|
|
|
cvShowImage("flow", cflow);
|
|
|
|
}
|
|
|
|
if(cvWaitKey(30)>=0)
|
|
|
|
break;
|
|
|
|
{
|
2012-06-15 21:04:17 +08:00
|
|
|
CvMat* temp;
|
2010-05-12 01:44:00 +08:00
|
|
|
CV_SWAP(prevgray, gray, temp);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
cvReleaseCapture(&capture);
|
|
|
|
return 0;
|
|
|
|
}
|