Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Tetragramm
GitHub Repository: Tetragramm/opencv
Path: blob/master/samples/cpp/fback.cpp
16337 views
1
#include "opencv2/video/tracking.hpp"
2
#include "opencv2/imgproc.hpp"
3
#include "opencv2/videoio.hpp"
4
#include "opencv2/highgui.hpp"
5
6
#include <iostream>
7
8
using namespace cv;
9
using namespace std;
10
11
static void help()
12
{
13
cout <<
14
"\nThis program demonstrates dense optical flow algorithm by Gunnar Farneback\n"
15
"Mainly the function: calcOpticalFlowFarneback()\n"
16
"Call:\n"
17
"./fback\n"
18
"This reads from video camera 0\n" << endl;
19
}
20
static void drawOptFlowMap(const Mat& flow, Mat& cflowmap, int step,
21
double, const Scalar& color)
22
{
23
for(int y = 0; y < cflowmap.rows; y += step)
24
for(int x = 0; x < cflowmap.cols; x += step)
25
{
26
const Point2f& fxy = flow.at<Point2f>(y, x);
27
line(cflowmap, Point(x,y), Point(cvRound(x+fxy.x), cvRound(y+fxy.y)),
28
color);
29
circle(cflowmap, Point(x,y), 2, color, -1);
30
}
31
}
32
33
int main(int argc, char** argv)
34
{
35
cv::CommandLineParser parser(argc, argv, "{help h||}");
36
if (parser.has("help"))
37
{
38
help();
39
return 0;
40
}
41
VideoCapture cap(0);
42
help();
43
if( !cap.isOpened() )
44
return -1;
45
46
Mat flow, cflow, frame;
47
UMat gray, prevgray, uflow;
48
namedWindow("flow", 1);
49
50
for(;;)
51
{
52
cap >> frame;
53
cvtColor(frame, gray, COLOR_BGR2GRAY);
54
55
if( !prevgray.empty() )
56
{
57
calcOpticalFlowFarneback(prevgray, gray, uflow, 0.5, 3, 15, 3, 5, 1.2, 0);
58
cvtColor(prevgray, cflow, COLOR_GRAY2BGR);
59
uflow.copyTo(flow);
60
drawOptFlowMap(flow, cflow, 16, 1.5, Scalar(0, 255, 0));
61
imshow("flow", cflow);
62
}
63
if(waitKey(30)>=0)
64
break;
65
std::swap(prevgray, gray);
66
}
67
return 0;
68
}
69
70