1#include <iostream>
2#include <vector>
3
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"
11
12using namespace std;
13using namespace cv;
14using namespace cv::cuda;
15
16static void download(const GpuMat& d_mat, vector<Point2f>& vec)
17{
18    vec.resize(d_mat.cols);
19    Mat mat(1, d_mat.cols, CV_32FC2, (void*)&vec[0]);
20    d_mat.download(mat);
21}
22
23static void download(const GpuMat& d_mat, vector<uchar>& vec)
24{
25    vec.resize(d_mat.cols);
26    Mat mat(1, d_mat.cols, CV_8UC1, (void*)&vec[0]);
27    d_mat.download(mat);
28}
29
30static void drawArrows(Mat& frame, const vector<Point2f>& prevPts, const vector<Point2f>& nextPts, const vector<uchar>& status, Scalar line_color = Scalar(0, 0, 255))
31{
32    for (size_t i = 0; i < prevPts.size(); ++i)
33    {
34        if (status[i])
35        {
36            int line_thickness = 1;
37
38            Point p = prevPts[i];
39            Point q = nextPts[i];
40
41            double angle = atan2((double) p.y - q.y, (double) p.x - q.x);
42
43            double hypotenuse = sqrt( (double)(p.y - q.y)*(p.y - q.y) + (double)(p.x - q.x)*(p.x - q.x) );
44
45            if (hypotenuse < 1.0)
46                continue;
47
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));
51
52            // Now we draw the main line of the arrow.
53            line(frame, p, q, line_color, line_thickness);
54
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.
57
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);
61
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);
65        }
66    }
67}
68
69template <typename T> inline T clamp (T x, T a, T b)
70{
71    return ((x) > (a) ? ((x) < (b) ? (x) : (b)) : (a));
72}
73
74template <typename T> inline T mapValue(T x, T a, T b, T c, T d)
75{
76    x = clamp(x, a, b);
77    return c + (d - c) * (x - a) / (b - a);
78}
79
80int main(int argc, const char* argv[])
81{
82    const char* keys =
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] }";
92
93    CommandLineParser cmd(argc, argv, keys);
94
95    if (cmd.has("help") || !cmd.check())
96    {
97        cmd.printMessage();
98        cmd.printErrors();
99        return 0;
100    }
101
102    string fname0 = cmd.get<string>("left");
103    string fname1 = cmd.get<string>("right");
104
105    if (fname0.empty() || fname1.empty())
106    {
107        cerr << "Missing input file names" << endl;
108        return -1;
109    }
110
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");
117
118    Mat frame0 = imread(fname0);
119    Mat frame1 = imread(fname1);
120
121    if (frame0.empty() || frame1.empty())
122    {
123        cout << "Can't load input images" << endl;
124        return -1;
125    }
126
127    namedWindow("PyrLK [Sparse]", WINDOW_NORMAL);
128    namedWindow("PyrLK [Dense] Flow Field", WINDOW_NORMAL);
129
130    cout << "Image size : " << frame0.cols << " x " << frame0.rows << endl;
131    cout << "Points count : " << points << endl;
132
133    cout << endl;
134
135    Mat frame0Gray;
136    cv::cvtColor(frame0, frame0Gray, COLOR_BGR2GRAY);
137    Mat frame1Gray;
138    cv::cvtColor(frame1, frame1Gray, COLOR_BGR2GRAY);
139
140    // goodFeaturesToTrack
141
142    GpuMat d_frame0Gray(frame0Gray);
143    GpuMat d_prevPts;
144
145    Ptr<cuda::CornersDetector> detector = cuda::createGoodFeaturesToTrackDetector(d_frame0Gray.type(), points, 0.01, minDist);
146
147    detector->detect(d_frame0Gray, d_prevPts);
148
149    // Sparse
150
151    Ptr<cuda::SparsePyrLKOpticalFlow> d_pyrLK = cuda::SparsePyrLKOpticalFlow::create(
152                Size(winSize, winSize), maxLevel, iters);
153
154    GpuMat d_frame0(frame0);
155    GpuMat d_frame1(frame1);
156    GpuMat d_frame1Gray(frame1Gray);
157    GpuMat d_nextPts;
158    GpuMat d_status;
159
160    d_pyrLK->calc(useGray ? d_frame0Gray : d_frame0, useGray ? d_frame1Gray : d_frame1, d_prevPts, d_nextPts, d_status);
161
162    // Draw arrows
163
164    vector<Point2f> prevPts(d_prevPts.cols);
165    download(d_prevPts, prevPts);
166
167    vector<Point2f> nextPts(d_nextPts.cols);
168    download(d_nextPts, nextPts);
169
170    vector<uchar> status(d_status.cols);
171    download(d_status, status);
172
173    drawArrows(frame0, prevPts, nextPts, status, Scalar(255, 0, 0));
174
175    imshow("PyrLK [Sparse]", frame0);
176
177    waitKey();
178
179    return 0;
180}
181