Fixed calc_output_scale with NO_OUTPUT_SCALE flag set.
[profile/ivi/opencv.git] / samples / gpu / optical_flow.cpp
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
9 using namespace std;
10 using namespace cv;
11 using namespace cv::cuda;
12
13 inline bool isFlowCorrect(Point2f u)
14 {
15     return !cvIsNaN(u.x) && !cvIsNaN(u.y) && fabs(u.x) < 1e9 && fabs(u.y) < 1e9;
16 }
17
18 static Vec3b computeColor(float fx, float fy)
19 {
20     static bool first = true;
21
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)
26     const int RY = 15;
27     const int YG = 6;
28     const int GC = 4;
29     const int CB = 11;
30     const int BM = 13;
31     const int MR = 6;
32     const int NCOLS = RY + YG + GC + CB + BM + MR;
33     static Vec3i colorWheel[NCOLS];
34
35     if (first)
36     {
37         int k = 0;
38
39         for (int i = 0; i < RY; ++i, ++k)
40             colorWheel[k] = Vec3i(255, 255 * i / RY, 0);
41
42         for (int i = 0; i < YG; ++i, ++k)
43             colorWheel[k] = Vec3i(255 - 255 * i / YG, 255, 0);
44
45         for (int i = 0; i < GC; ++i, ++k)
46             colorWheel[k] = Vec3i(0, 255, 255 * i / GC);
47
48         for (int i = 0; i < CB; ++i, ++k)
49             colorWheel[k] = Vec3i(0, 255 - 255 * i / CB, 255);
50
51         for (int i = 0; i < BM; ++i, ++k)
52             colorWheel[k] = Vec3i(255 * i / BM, 0, 255);
53
54         for (int i = 0; i < MR; ++i, ++k)
55             colorWheel[k] = Vec3i(255, 0, 255 - 255 * i / MR);
56
57         first = false;
58     }
59
60     const float rad = sqrt(fx * fx + fy * fy);
61     const float a = atan2(-fy, -fx) / (float) CV_PI;
62
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;
67
68     Vec3b pix;
69
70     for (int b = 0; b < 3; b++)
71     {
72         const float col0 = colorWheel[k0][b] / 255.0f;
73         const float col1 = colorWheel[k1][b] / 255.0f;
74
75         float col = (1 - f) * col0 + f * col1;
76
77         if (rad <= 1)
78             col = 1 - rad * (1 - col); // increase saturation with radius
79         else
80             col *= .75; // out of range
81
82         pix[2 - b] = static_cast<uchar>(255.0 * col);
83     }
84
85     return pix;
86 }
87
88 static void drawOpticalFlow(const Mat_<float>& flowx, const Mat_<float>& flowy, Mat& dst, float maxmotion = -1)
89 {
90     dst.create(flowx.size(), CV_8UC3);
91     dst.setTo(Scalar::all(0));
92
93     // determine motion range:
94     float maxrad = maxmotion;
95
96     if (maxmotion <= 0)
97     {
98         maxrad = 1;
99         for (int y = 0; y < flowx.rows; ++y)
100         {
101             for (int x = 0; x < flowx.cols; ++x)
102             {
103                 Point2f u(flowx(y, x), flowy(y, x));
104
105                 if (!isFlowCorrect(u))
106                     continue;
107
108                 maxrad = max(maxrad, sqrt(u.x * u.x + u.y * u.y));
109             }
110         }
111     }
112
113     for (int y = 0; y < flowx.rows; ++y)
114     {
115         for (int x = 0; x < flowx.cols; ++x)
116         {
117             Point2f u(flowx(y, x), flowy(y, x));
118
119             if (isFlowCorrect(u))
120                 dst.at<Vec3b>(y, x) = computeColor(u.x / maxrad, u.y / maxrad);
121         }
122     }
123 }
124
125 static void showFlow(const char* name, const GpuMat& d_flowx, const GpuMat& d_flowy)
126 {
127     Mat flowx(d_flowx);
128     Mat flowy(d_flowy);
129
130     Mat out;
131     drawOpticalFlow(flowx, flowy, out, 10);
132
133     imshow(name, out);
134 }
135
136 int main(int argc, const char* argv[])
137 {
138     if (argc < 3)
139     {
140         cerr << "Usage : " << argv[0] << "<frame0> <frame1>" << endl;
141         return -1;
142     }
143
144     Mat frame0 = imread(argv[1], IMREAD_GRAYSCALE);
145     Mat frame1 = imread(argv[2], IMREAD_GRAYSCALE);
146
147     if (frame0.empty())
148     {
149         cerr << "Can't open image ["  << argv[1] << "]" << endl;
150         return -1;
151     }
152     if (frame1.empty())
153     {
154         cerr << "Can't open image ["  << argv[2] << "]" << endl;
155         return -1;
156     }
157
158     if (frame1.size() != frame0.size())
159     {
160         cerr << "Images should be of equal sizes" << endl;
161         return -1;
162     }
163
164     GpuMat d_frame0(frame0);
165     GpuMat d_frame1(frame1);
166
167     GpuMat d_flowx(frame0.size(), CV_32FC1);
168     GpuMat d_flowy(frame0.size(), CV_32FC1);
169
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;
175
176     {
177         GpuMat d_frame0f;
178         GpuMat d_frame1f;
179
180         d_frame0.convertTo(d_frame0f, CV_32F, 1.0 / 255.0);
181         d_frame1.convertTo(d_frame1f, CV_32F, 1.0 / 255.0);
182
183         const int64 start = getTickCount();
184
185         brox(d_frame0f, d_frame1f, d_flowx, d_flowy);
186
187         const double timeSec = (getTickCount() - start) / getTickFrequency();
188         cout << "Brox : " << timeSec << " sec" << endl;
189
190         showFlow("Brox", d_flowx, d_flowy);
191     }
192
193     {
194         const int64 start = getTickCount();
195
196         lk.dense(d_frame0, d_frame1, d_flowx, d_flowy);
197
198         const double timeSec = (getTickCount() - start) / getTickFrequency();
199         cout << "LK : " << timeSec << " sec" << endl;
200
201         showFlow("LK", d_flowx, d_flowy);
202     }
203
204     {
205         const int64 start = getTickCount();
206
207         farn(d_frame0, d_frame1, d_flowx, d_flowy);
208
209         const double timeSec = (getTickCount() - start) / getTickFrequency();
210         cout << "Farn : " << timeSec << " sec" << endl;
211
212         showFlow("Farn", d_flowx, d_flowy);
213     }
214
215     {
216         const int64 start = getTickCount();
217
218         tvl1(d_frame0, d_frame1, d_flowx, d_flowy);
219
220         const double timeSec = (getTickCount() - start) / getTickFrequency();
221         cout << "TVL1 : " << timeSec << " sec" << endl;
222
223         showFlow("TVL1", d_flowx, d_flowy);
224     }
225
226     {
227         const int64 start = getTickCount();
228
229         GpuMat buf;
230         calcOpticalFlowBM(d_frame0, d_frame1, Size(7, 7), Size(1, 1), Size(21, 21), false, d_flowx, d_flowy, buf);
231
232         const double timeSec = (getTickCount() - start) / getTickFrequency();
233         cout << "BM : " << timeSec << " sec" << endl;
234
235         showFlow("BM", d_flowx, d_flowy);
236     }
237
238     {
239         const int64 start = getTickCount();
240
241         fastBM(d_frame0, d_frame1, d_flowx, d_flowy);
242
243         const double timeSec = (getTickCount() - start) / getTickFrequency();
244         cout << "Fast BM : " << timeSec << " sec" << endl;
245
246         showFlow("Fast BM", d_flowx, d_flowy);
247     }
248
249     imshow("Frame 0", frame0);
250     imshow("Frame 1", frame1);
251     waitKey();
252
253     return 0;
254 }