Gunnar Farnebak的算法计算稠密光流

Gunnar Farnebak的算法计算稠密光流


Computes a dense optical flow using the Gunnar Farneback’s algorithm.

Gunnar Farnebak的算法计算稠密光流

C++:  void  calcOpticalFlowFarneback (InputArray  prev, InputArray  next, InputOutputArray  flow, double  pyr_scale, int  levels, int  winsize, int  iterations, int  poly_n, double  poly_sigma, int  flags )
示例:
#include "opencv2/video/tracking.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/highgui/highgui.hpp"
#include <iostream>
using namespace cv;
using namespace std;

static void help()
{
	cout <<"\nThis program demonstrates dense optical flow algorithm by Gunnar Farneback\n"
	"Mainly the function: calcOpticalFlowFarneback()\n""Call:\n"
	"./fback\n""This reads from video camera 0\n" << 	endl;
}

static void drawOptFlowMap(const Mat& flow, Mat& cflowmap, int step,double, const Scalar& color)
{
	for(int y = 0; y < cflowmap.rows; y += step)
		for(int x = 0; x < cflowmap.cols; x += step)
		{
			const Point2f& fxy = flow.at<Point2f>(y, x);
			line(cflowmap, Point(x,y), Point(cvRound(x+fxy.x), cvRound(y+fxy.y)),color);
			circle(cflowmap, Point(x,y), 2, color, -1);
		}
}

int main(int, char**)
{
	//VideoCapture cap(0);
	VideoCapture cap("E:\\图片\\视频材料\\Megamind.avi");
	help();
	if( !cap.isOpened() )
		return -1;
	Mat prevgray, gray, flow, cflow, frame;
	namedWindow("flow", 1);
	for(;;)
	{
		cap >> frame;
		cvtColor(frame, gray, COLOR_BGR2GRAY);
		if( prevgray.data )
		{
			calcOpticalFlowFarneback(prevgray, gray, flow, 0.5, 3, 15, 3, 5, 1.2, 0);
			cvtColor(prevgray, cflow, COLOR_GRAY2BGR);
			drawOptFlowMap(flow, cflow, 16, 1.5, Scalar(0, 255, 0));
			imshow("flow", cflow);
		}
		if(waitKey(30)>=0)
			break;
		std::swap(prevgray, gray);
	}
	return 0;
}

运行结果


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值