-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
59 lines (50 loc) · 1.47 KB
/
Copy pathmain.cpp
File metadata and controls
59 lines (50 loc) · 1.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#include <iostream>
#include <ctime>
#include <cmath>
#include "bits/time.h"
#include <opencv2/opencv.hpp>
#include <cassert>
#include <cmath>
#include <chrono>
#include <opencv2/core/cuda.hpp>
#include <opencv2/cudaarithm.hpp>
#include <opencv2/cudaimgproc.hpp>
#include <opencv2/cudaoptflow.hpp>
#define TestCUDA true
using namespace cv;
using namespace std;
int main()
{
Mat frame;
Mat prevFrame;
cuda::GpuMat prevFrameGPU;
cuda::GpuMat frameGPU;
VideoCapture cap("3.mp4");
Ptr<cuda::FarnebackOpticalFlow> farn = cuda::FarnebackOpticalFlow::create();
cap >> prevFrame;
cv::cvtColor(prevFrame, prevFrame, cv::COLOR_BGR2GRAY);
Mat flow;
for (;;)
{
cap >> frame;
cv::resize(frame, frame, prevFrame.size());
cv::cvtColor(frame, frame, cv::COLOR_BGR2GRAY);
cuda::GpuMat prevFrameGPU(prevFrame);
cuda::GpuMat frameGPU(frame);
cuda::GpuMat gflow(frame.size(), CV_32FC2);
farn->calc(prevFrameGPU, frameGPU, gflow);
gflow.download(flow);
for (int y = 0; y < frame.rows - 1; y += 10)
{
for (int x = 0; x < frame.cols - 1; x += 10)
{
const Point2f xyFlow = flow.at<Point2f>(y, x) * 5;
line(frame, Point(x, y), Point(cvRound(x + xyFlow.x), cvRound(y + xyFlow.y)), Scalar(0, 255, 0), 2);
circle(frame, Point(x, y), 1, Scalar(0, 0, 255), -1);
}
}
imshow("Display window", frame);
waitKey(25);
frame.copyTo(prevFrame);
}
}