Gunnar Farnebak algorithm for dense optical flow calculation

Source: Internet
Author: User
Tags scalar

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


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.