4
#include "opencv2/core.hpp"
5
#include <opencv2/core/utility.hpp>
6
#include "opencv2/imgproc.hpp"
7
#include "opencv2/highgui.hpp"
8
#include "opencv2/video.hpp"
9
#include "opencv2/cudaoptflow.hpp"
10
#include "opencv2/cudaimgproc.hpp"
14
using namespace cv::cuda;
16
static void download(const GpuMat& d_mat, vector<Point2f>& vec)
18
vec.resize(d_mat.cols);
19
Mat mat(1, d_mat.cols, CV_32FC2, (void*)&vec[0]);
23
static void download(const GpuMat& d_mat, vector<uchar>& vec)
25
vec.resize(d_mat.cols);
26
Mat mat(1, d_mat.cols, CV_8UC1, (void*)&vec[0]);
30
static void drawArrows(Mat& frame, const vector<Point2f>& prevPts, const vector<Point2f>& nextPts, const vector<uchar>& status, Scalar line_color = Scalar(0, 0, 255))
32
for (size_t i = 0; i < prevPts.size(); ++i)
36
int line_thickness = 1;
41
double angle = atan2((double) p.y - q.y, (double) p.x - q.x);
43
double hypotenuse = sqrt( (double)(p.y - q.y)*(p.y - q.y) + (double)(p.x - q.x)*(p.x - q.x) );
48
// Here we lengthen the arrow by a factor of three.
49
q.x = (int) (p.x - 3 * hypotenuse * cos(angle));
50
q.y = (int) (p.y - 3 * hypotenuse * sin(angle));
52
// Now we draw the main line of the arrow.
53
line(frame, p, q, line_color, line_thickness);
55
// Now draw the tips of the arrow. I do some scaling so that the
56
// tips look proportional to the main line of the arrow.
58
p.x = (int) (q.x + 9 * cos(angle + CV_PI / 4));
59
p.y = (int) (q.y + 9 * sin(angle + CV_PI / 4));
60
line(frame, p, q, line_color, line_thickness);
62
p.x = (int) (q.x + 9 * cos(angle - CV_PI / 4));
63
p.y = (int) (q.y + 9 * sin(angle - CV_PI / 4));
64
line(frame, p, q, line_color, line_thickness);
69
template <typename T> inline T clamp (T x, T a, T b)
71
return ((x) > (a) ? ((x) < (b) ? (x) : (b)) : (a));
74
template <typename T> inline T mapValue(T x, T a, T b, T c, T d)
77
return c + (d - c) * (x - a) / (b - a);
80
int main(int argc, const char* argv[])
83
"{ h help | | print help message }"
84
"{ l left | ../data/pic1.png | specify left image }"
85
"{ r right | ../data/pic2.png | specify right image }"
86
"{ gray | | use grayscale sources [PyrLK Sparse] }"
87
"{ win_size | 21 | specify windows size [PyrLK] }"
88
"{ max_level | 3 | specify max level [PyrLK] }"
89
"{ iters | 30 | specify iterations count [PyrLK] }"
90
"{ points | 4000 | specify points count [GoodFeatureToTrack] }"
91
"{ min_dist | 0 | specify minimal distance between points [GoodFeatureToTrack] }";
93
CommandLineParser cmd(argc, argv, keys);
95
if (cmd.has("help") || !cmd.check())
102
string fname0 = cmd.get<string>("left");
103
string fname1 = cmd.get<string>("right");
105
if (fname0.empty() || fname1.empty())
107
cerr << "Missing input file names" << endl;
111
bool useGray = cmd.has("gray");
112
int winSize = cmd.get<int>("win_size");
113
int maxLevel = cmd.get<int>("max_level");
114
int iters = cmd.get<int>("iters");
115
int points = cmd.get<int>("points");
116
double minDist = cmd.get<double>("min_dist");
118
Mat frame0 = imread(fname0);
119
Mat frame1 = imread(fname1);
121
if (frame0.empty() || frame1.empty())
123
cout << "Can't load input images" << endl;
127
namedWindow("PyrLK [Sparse]", WINDOW_NORMAL);
128
namedWindow("PyrLK [Dense] Flow Field", WINDOW_NORMAL);
130
cout << "Image size : " << frame0.cols << " x " << frame0.rows << endl;
131
cout << "Points count : " << points << endl;
136
cv::cvtColor(frame0, frame0Gray, COLOR_BGR2GRAY);
138
cv::cvtColor(frame1, frame1Gray, COLOR_BGR2GRAY);
140
// goodFeaturesToTrack
142
GpuMat d_frame0Gray(frame0Gray);
145
Ptr<cuda::CornersDetector> detector = cuda::createGoodFeaturesToTrackDetector(d_frame0Gray.type(), points, 0.01, minDist);
147
detector->detect(d_frame0Gray, d_prevPts);
151
Ptr<cuda::SparsePyrLKOpticalFlow> d_pyrLK = cuda::SparsePyrLKOpticalFlow::create(
152
Size(winSize, winSize), maxLevel, iters);
154
GpuMat d_frame0(frame0);
155
GpuMat d_frame1(frame1);
156
GpuMat d_frame1Gray(frame1Gray);
160
d_pyrLK->calc(useGray ? d_frame0Gray : d_frame0, useGray ? d_frame1Gray : d_frame1, d_prevPts, d_nextPts, d_status);
164
vector<Point2f> prevPts(d_prevPts.cols);
165
download(d_prevPts, prevPts);
167
vector<Point2f> nextPts(d_nextPts.cols);
168
download(d_nextPts, nextPts);
170
vector<uchar> status(d_status.cols);
171
download(d_status, status);
173
drawArrows(frame0, prevPts, nextPts, status, Scalar(255, 0, 0));
175
imshow("PyrLK [Sparse]", frame0);