4 #include "opencv2/core.hpp"
5 #include <opencv2/core/utility.hpp>
6 #include "opencv2/highgui.hpp"
7 #include "opencv2/cudaoptflow.hpp"
11 using namespace cv::cuda;
13 inline bool isFlowCorrect(Point2f u)
15 return !cvIsNaN(u.x) && !cvIsNaN(u.y) && fabs(u.x) < 1e9 && fabs(u.y) < 1e9;
18 static Vec3b computeColor(float fx, float fy)
20 static bool first = true;
22 // relative lengths of color transitions:
23 // these are chosen based on perceptual similarity
24 // (e.g. one can distinguish more shades between red and yellow
25 // than between yellow and green)
32 const int NCOLS = RY + YG + GC + CB + BM + MR;
33 static Vec3i colorWheel[NCOLS];
39 for (int i = 0; i < RY; ++i, ++k)
40 colorWheel[k] = Vec3i(255, 255 * i / RY, 0);
42 for (int i = 0; i < YG; ++i, ++k)
43 colorWheel[k] = Vec3i(255 - 255 * i / YG, 255, 0);
45 for (int i = 0; i < GC; ++i, ++k)
46 colorWheel[k] = Vec3i(0, 255, 255 * i / GC);
48 for (int i = 0; i < CB; ++i, ++k)
49 colorWheel[k] = Vec3i(0, 255 - 255 * i / CB, 255);
51 for (int i = 0; i < BM; ++i, ++k)
52 colorWheel[k] = Vec3i(255 * i / BM, 0, 255);
54 for (int i = 0; i < MR; ++i, ++k)
55 colorWheel[k] = Vec3i(255, 0, 255 - 255 * i / MR);
60 const float rad = sqrt(fx * fx + fy * fy);
61 const float a = atan2(-fy, -fx) / (float) CV_PI;
63 const float fk = (a + 1.0f) / 2.0f * (NCOLS - 1);
64 const int k0 = static_cast<int>(fk);
65 const int k1 = (k0 + 1) % NCOLS;
66 const float f = fk - k0;
70 for (int b = 0; b < 3; b++)
72 const float col0 = colorWheel[k0][b] / 255.0f;
73 const float col1 = colorWheel[k1][b] / 255.0f;
75 float col = (1 - f) * col0 + f * col1;
78 col = 1 - rad * (1 - col); // increase saturation with radius
80 col *= .75; // out of range
82 pix[2 - b] = static_cast<uchar>(255.0 * col);
88 static void drawOpticalFlow(const Mat_<float>& flowx, const Mat_<float>& flowy, Mat& dst, float maxmotion = -1)
90 dst.create(flowx.size(), CV_8UC3);
91 dst.setTo(Scalar::all(0));
93 // determine motion range:
94 float maxrad = maxmotion;
99 for (int y = 0; y < flowx.rows; ++y)
101 for (int x = 0; x < flowx.cols; ++x)
103 Point2f u(flowx(y, x), flowy(y, x));
105 if (!isFlowCorrect(u))
108 maxrad = max(maxrad, sqrt(u.x * u.x + u.y * u.y));
113 for (int y = 0; y < flowx.rows; ++y)
115 for (int x = 0; x < flowx.cols; ++x)
117 Point2f u(flowx(y, x), flowy(y, x));
119 if (isFlowCorrect(u))
120 dst.at<Vec3b>(y, x) = computeColor(u.x / maxrad, u.y / maxrad);
125 static void showFlow(const char* name, const GpuMat& d_flowx, const GpuMat& d_flowy)
131 drawOpticalFlow(flowx, flowy, out, 10);
136 int main(int argc, const char* argv[])
140 cerr << "Usage : " << argv[0] << "<frame0> <frame1>" << endl;
144 Mat frame0 = imread(argv[1], IMREAD_GRAYSCALE);
145 Mat frame1 = imread(argv[2], IMREAD_GRAYSCALE);
149 cerr << "Can't open image [" << argv[1] << "]" << endl;
154 cerr << "Can't open image [" << argv[2] << "]" << endl;
158 if (frame1.size() != frame0.size())
160 cerr << "Images should be of equal sizes" << endl;
164 GpuMat d_frame0(frame0);
165 GpuMat d_frame1(frame1);
167 GpuMat d_flowx(frame0.size(), CV_32FC1);
168 GpuMat d_flowy(frame0.size(), CV_32FC1);
170 BroxOpticalFlow brox(0.197f, 50.0f, 0.8f, 10, 77, 10);
171 PyrLKOpticalFlow lk; lk.winSize = Size(7, 7);
172 FarnebackOpticalFlow farn;
173 OpticalFlowDual_TVL1_CUDA tvl1;
174 FastOpticalFlowBM fastBM;
180 d_frame0.convertTo(d_frame0f, CV_32F, 1.0 / 255.0);
181 d_frame1.convertTo(d_frame1f, CV_32F, 1.0 / 255.0);
183 const int64 start = getTickCount();
185 brox(d_frame0f, d_frame1f, d_flowx, d_flowy);
187 const double timeSec = (getTickCount() - start) / getTickFrequency();
188 cout << "Brox : " << timeSec << " sec" << endl;
190 showFlow("Brox", d_flowx, d_flowy);
194 const int64 start = getTickCount();
196 lk.dense(d_frame0, d_frame1, d_flowx, d_flowy);
198 const double timeSec = (getTickCount() - start) / getTickFrequency();
199 cout << "LK : " << timeSec << " sec" << endl;
201 showFlow("LK", d_flowx, d_flowy);
205 const int64 start = getTickCount();
207 farn(d_frame0, d_frame1, d_flowx, d_flowy);
209 const double timeSec = (getTickCount() - start) / getTickFrequency();
210 cout << "Farn : " << timeSec << " sec" << endl;
212 showFlow("Farn", d_flowx, d_flowy);
216 const int64 start = getTickCount();
218 tvl1(d_frame0, d_frame1, d_flowx, d_flowy);
220 const double timeSec = (getTickCount() - start) / getTickFrequency();
221 cout << "TVL1 : " << timeSec << " sec" << endl;
223 showFlow("TVL1", d_flowx, d_flowy);
227 const int64 start = getTickCount();
230 calcOpticalFlowBM(d_frame0, d_frame1, Size(7, 7), Size(1, 1), Size(21, 21), false, d_flowx, d_flowy, buf);
232 const double timeSec = (getTickCount() - start) / getTickFrequency();
233 cout << "BM : " << timeSec << " sec" << endl;
235 showFlow("BM", d_flowx, d_flowy);
239 const int64 start = getTickCount();
241 fastBM(d_frame0, d_frame1, d_flowx, d_flowy);
243 const double timeSec = (getTickCount() - start) / getTickFrequency();
244 cout << "Fast BM : " << timeSec << " sec" << endl;
246 showFlow("Fast BM", d_flowx, d_flowy);
249 imshow("Frame 0", frame0);
250 imshow("Frame 1", frame1);