1 #include <iostream>
2 #include <fstream>
3
4 #include "opencv2/core.hpp"
5 #include <opencv2/core/utility.hpp>
6 #include "opencv2/highgui.hpp"
7 #include "opencv2/cudaoptflow.hpp"
8 #include "opencv2/cudaarithm.hpp"
9
10 using namespace std;
11 using namespace cv;
12 using namespace cv::cuda;
13
isFlowCorrect(Point2f u)14 inline bool isFlowCorrect(Point2f u)
15 {
16 return !cvIsNaN(u.x) && !cvIsNaN(u.y) && fabs(u.x) < 1e9 && fabs(u.y) < 1e9;
17 }
18
computeColor(float fx,float fy)19 static Vec3b computeColor(float fx, float fy)
20 {
21 static bool first = true;
22
23 // relative lengths of color transitions:
24 // these are chosen based on perceptual similarity
25 // (e.g. one can distinguish more shades between red and yellow
26 // than between yellow and green)
27 const int RY = 15;
28 const int YG = 6;
29 const int GC = 4;
30 const int CB = 11;
31 const int BM = 13;
32 const int MR = 6;
33 const int NCOLS = RY + YG + GC + CB + BM + MR;
34 static Vec3i colorWheel[NCOLS];
35
36 if (first)
37 {
38 int k = 0;
39
40 for (int i = 0; i < RY; ++i, ++k)
41 colorWheel[k] = Vec3i(255, 255 * i / RY, 0);
42
43 for (int i = 0; i < YG; ++i, ++k)
44 colorWheel[k] = Vec3i(255 - 255 * i / YG, 255, 0);
45
46 for (int i = 0; i < GC; ++i, ++k)
47 colorWheel[k] = Vec3i(0, 255, 255 * i / GC);
48
49 for (int i = 0; i < CB; ++i, ++k)
50 colorWheel[k] = Vec3i(0, 255 - 255 * i / CB, 255);
51
52 for (int i = 0; i < BM; ++i, ++k)
53 colorWheel[k] = Vec3i(255 * i / BM, 0, 255);
54
55 for (int i = 0; i < MR; ++i, ++k)
56 colorWheel[k] = Vec3i(255, 0, 255 - 255 * i / MR);
57
58 first = false;
59 }
60
61 const float rad = sqrt(fx * fx + fy * fy);
62 const float a = atan2(-fy, -fx) / (float) CV_PI;
63
64 const float fk = (a + 1.0f) / 2.0f * (NCOLS - 1);
65 const int k0 = static_cast<int>(fk);
66 const int k1 = (k0 + 1) % NCOLS;
67 const float f = fk - k0;
68
69 Vec3b pix;
70
71 for (int b = 0; b < 3; b++)
72 {
73 const float col0 = colorWheel[k0][b] / 255.0f;
74 const float col1 = colorWheel[k1][b] / 255.0f;
75
76 float col = (1 - f) * col0 + f * col1;
77
78 if (rad <= 1)
79 col = 1 - rad * (1 - col); // increase saturation with radius
80 else
81 col *= .75; // out of range
82
83 pix[2 - b] = static_cast<uchar>(255.0 * col);
84 }
85
86 return pix;
87 }
88
drawOpticalFlow(const Mat_<float> & flowx,const Mat_<float> & flowy,Mat & dst,float maxmotion=-1)89 static void drawOpticalFlow(const Mat_<float>& flowx, const Mat_<float>& flowy, Mat& dst, float maxmotion = -1)
90 {
91 dst.create(flowx.size(), CV_8UC3);
92 dst.setTo(Scalar::all(0));
93
94 // determine motion range:
95 float maxrad = maxmotion;
96
97 if (maxmotion <= 0)
98 {
99 maxrad = 1;
100 for (int y = 0; y < flowx.rows; ++y)
101 {
102 for (int x = 0; x < flowx.cols; ++x)
103 {
104 Point2f u(flowx(y, x), flowy(y, x));
105
106 if (!isFlowCorrect(u))
107 continue;
108
109 maxrad = max(maxrad, sqrt(u.x * u.x + u.y * u.y));
110 }
111 }
112 }
113
114 for (int y = 0; y < flowx.rows; ++y)
115 {
116 for (int x = 0; x < flowx.cols; ++x)
117 {
118 Point2f u(flowx(y, x), flowy(y, x));
119
120 if (isFlowCorrect(u))
121 dst.at<Vec3b>(y, x) = computeColor(u.x / maxrad, u.y / maxrad);
122 }
123 }
124 }
125
showFlow(const char * name,const GpuMat & d_flow)126 static void showFlow(const char* name, const GpuMat& d_flow)
127 {
128 GpuMat planes[2];
129 cuda::split(d_flow, planes);
130
131 Mat flowx(planes[0]);
132 Mat flowy(planes[1]);
133
134 Mat out;
135 drawOpticalFlow(flowx, flowy, out, 10);
136
137 imshow(name, out);
138 }
139
main(int argc,const char * argv[])140 int main(int argc, const char* argv[])
141 {
142 string filename1, filename2;
143 if (argc < 3)
144 {
145 cerr << "Usage : " << argv[0] << " <frame0> <frame1>" << endl;
146 filename1 = "../data/basketball1.png";
147 filename2 = "../data/basketball2.png";
148 }
149 else
150 {
151 filename1 = argv[1];
152 filename2 = argv[2];
153 }
154
155 Mat frame0 = imread(filename1, IMREAD_GRAYSCALE);
156 Mat frame1 = imread(filename2, IMREAD_GRAYSCALE);
157
158 if (frame0.empty())
159 {
160 cerr << "Can't open image [" << filename1 << "]" << endl;
161 return -1;
162 }
163 if (frame1.empty())
164 {
165 cerr << "Can't open image [" << filename2 << "]" << endl;
166 return -1;
167 }
168
169 if (frame1.size() != frame0.size())
170 {
171 cerr << "Images should be of equal sizes" << endl;
172 return -1;
173 }
174
175 GpuMat d_frame0(frame0);
176 GpuMat d_frame1(frame1);
177
178 GpuMat d_flow(frame0.size(), CV_32FC2);
179
180 Ptr<cuda::BroxOpticalFlow> brox = cuda::BroxOpticalFlow::create(0.197f, 50.0f, 0.8f, 10, 77, 10);
181 Ptr<cuda::DensePyrLKOpticalFlow> lk = cuda::DensePyrLKOpticalFlow::create(Size(7, 7));
182 Ptr<cuda::FarnebackOpticalFlow> farn = cuda::FarnebackOpticalFlow::create();
183 Ptr<cuda::OpticalFlowDual_TVL1> tvl1 = cuda::OpticalFlowDual_TVL1::create();
184
185 {
186 GpuMat d_frame0f;
187 GpuMat d_frame1f;
188
189 d_frame0.convertTo(d_frame0f, CV_32F, 1.0 / 255.0);
190 d_frame1.convertTo(d_frame1f, CV_32F, 1.0 / 255.0);
191
192 const int64 start = getTickCount();
193
194 brox->calc(d_frame0f, d_frame1f, d_flow);
195
196 const double timeSec = (getTickCount() - start) / getTickFrequency();
197 cout << "Brox : " << timeSec << " sec" << endl;
198
199 showFlow("Brox", d_flow);
200 }
201
202 {
203 const int64 start = getTickCount();
204
205 lk->calc(d_frame0, d_frame1, d_flow);
206
207 const double timeSec = (getTickCount() - start) / getTickFrequency();
208 cout << "LK : " << timeSec << " sec" << endl;
209
210 showFlow("LK", d_flow);
211 }
212
213 {
214 const int64 start = getTickCount();
215
216 farn->calc(d_frame0, d_frame1, d_flow);
217
218 const double timeSec = (getTickCount() - start) / getTickFrequency();
219 cout << "Farn : " << timeSec << " sec" << endl;
220
221 showFlow("Farn", d_flow);
222 }
223
224 {
225 const int64 start = getTickCount();
226
227 tvl1->calc(d_frame0, d_frame1, d_flow);
228
229 const double timeSec = (getTickCount() - start) / getTickFrequency();
230 cout << "TVL1 : " << timeSec << " sec" << endl;
231
232 showFlow("TVL1", d_flow);
233 }
234
235 imshow("Frame 0", frame0);
236 imshow("Frame 1", frame1);
237 waitKey();
238
239 return 0;
240 }
241