Gunnar Farnebak algorithm for dense optical flow calculation
Computes a dense optical flow using the Gunnar farneback ' s algorithm.
Using Gunnar Farnebak algorithm to compute dense optical flow C + +: void Calcopticalflowfarneback (Inputarray prev, Inputarray next , Inputoutputarray flow, double pyr_scale, int levels, int winsize, int it Erations, int poly_n, double poly_sigma, int flags ) Example:
#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:\\ picture \ \ video Material \\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, +, 1.5, Scalar (0, 255, 0));
Imshow ("Flow", cflow);
} if (Waitkey () >=0) break;
Std::swap (Prevgray, Gray); } RetuRN 0; }
Run results