catching OpenCL double not supported exceptions
[profile/ivi/opencv.git] / modules / ocl / test / test_optflow.cpp
1 ///////////////////////////////////////////////////////////////////////////////////////
2 //
3 //  IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
4 //
5 //  By downloading, copying, installing or using the software you agree to this license.
6 //  If you do not agree to this license, do not download, install,
7 //  copy or use the software.
8 //
9 //
10 //                           License Agreement
11 //                For Open Source Computer Vision Library
12 // Copyright (C) 2010-2012, Multicoreware, Inc., all rights reserved.
13 // Copyright (C) 2010-2012, Institute Of Software Chinese Academy Of Science, all rights reserved.
14 // Copyright (C) 2010-2012, Advanced Micro Devices, Inc., all rights reserved.
15 // Third party copyrights are property of their respective owners.
16 //
17 // @Authors
18 //
19 //
20 // Redistribution and use in source and binary forms, with or without modification,
21 // are permitted provided that the following conditions are met:
22 //
23 //   * Redistribution's of source code must retain the above copyright notice,
24 //     this list of conditions and the following disclaimer.
25 //
26 //   * Redistribution's in binary form must reproduce the above copyright notice,
27 //     this list of conditions and the following disclaimer in the documentation
28 //     and/or other oclMaterials provided with the distribution.
29 //
30 //   * The name of the copyright holders may not be used to endorse or promote products
31 //     derived from this software without specific prior written permission.
32 //
33 // This software is provided by the copyright holders and contributors "as is" and
34 // any express or implied warranties, including, but not limited to, the implied
35 // warranties of merchantability and fitness for a particular purpose are disclaimed.
36 // In no event shall the Intel Corporation or contributors be liable for any direct,
37 // indirect, incidental, special, exemplary, or consequential damages
38 // (including, but not limited to, procurement of substitute goods or services;
39 // loss of use, data, or profits; or business interruption) however caused
40 // and on any theory of liability, whether in contract, strict liability,
41 // or tort (including negligence or otherwise) arising in any way out of
42 // the use of this software, even if advised of the possibility of such damage.
43 //
44 //M*/
45
46 #include "test_precomp.hpp"
47 #include <iomanip>
48
49 #ifdef HAVE_OPENCL
50
51 using namespace cv;
52 using namespace cv::ocl;
53 using namespace cvtest;
54 using namespace testing;
55 using namespace std;
56
57 //////////////////////////////////////////////////////
58 // GoodFeaturesToTrack
59 namespace
60 {
61     IMPLEMENT_PARAM_CLASS(MinDistance, double)
62 }
63 PARAM_TEST_CASE(GoodFeaturesToTrack, MinDistance)
64 {
65     double minDistance;
66
67     virtual void SetUp()
68     {
69         minDistance = GET_PARAM(0);
70     }
71 };
72
73 OCL_TEST_P(GoodFeaturesToTrack, Accuracy)
74 {
75     cv::Mat frame = readImage("gpu/opticalflow/rubberwhale1.png", cv::IMREAD_GRAYSCALE);
76     ASSERT_FALSE(frame.empty());
77
78     int maxCorners = 1000;
79     double qualityLevel = 0.01;
80
81     cv::ocl::GoodFeaturesToTrackDetector_OCL detector(maxCorners, qualityLevel, minDistance);
82
83     cv::ocl::oclMat d_pts;
84     detector(oclMat(frame), d_pts);
85
86     ASSERT_FALSE(d_pts.empty());
87
88     std::vector<cv::Point2f> pts(d_pts.cols);
89
90     detector.downloadPoints(d_pts, pts);
91
92     std::vector<cv::Point2f> pts_gold;
93     cv::goodFeaturesToTrack(frame, pts_gold, maxCorners, qualityLevel, minDistance);
94
95     ASSERT_EQ(pts_gold.size(), pts.size());
96
97     size_t mistmatch = 0;
98     for (size_t i = 0; i < pts.size(); ++i)
99     {
100         cv::Point2i a = pts_gold[i];
101         cv::Point2i b = pts[i];
102
103         bool eq = std::abs(a.x - b.x) < 1 && std::abs(a.y - b.y) < 1;
104
105         if (!eq)
106             ++mistmatch;
107     }
108
109     double bad_ratio = static_cast<double>(mistmatch) / pts.size();
110
111     ASSERT_LE(bad_ratio, 0.01);
112 }
113
114 OCL_TEST_P(GoodFeaturesToTrack, EmptyCorners)
115 {
116     int maxCorners = 1000;
117     double qualityLevel = 0.01;
118
119     cv::ocl::GoodFeaturesToTrackDetector_OCL detector(maxCorners, qualityLevel, minDistance);
120
121     cv::ocl::oclMat src(100, 100, CV_8UC1, cv::Scalar::all(0));
122     cv::ocl::oclMat corners(1, maxCorners, CV_32FC2);
123
124     detector(src, corners);
125
126     ASSERT_TRUE(corners.empty());
127 }
128
129 INSTANTIATE_TEST_CASE_P(OCL_Video, GoodFeaturesToTrack,
130     testing::Values(MinDistance(0.0), MinDistance(3.0)));
131
132 //////////////////////////////////////////////////////////////////////////
133 PARAM_TEST_CASE(TVL1, bool)
134 {
135     bool useRoi;
136
137     virtual void SetUp()
138     {
139         useRoi = GET_PARAM(0);
140     }
141
142 };
143
144 OCL_TEST_P(TVL1, Accuracy)
145 {
146     cv::Mat frame0 = readImage("gpu/opticalflow/rubberwhale1.png", cv::IMREAD_GRAYSCALE);
147     ASSERT_FALSE(frame0.empty());
148
149     cv::Mat frame1 = readImage("gpu/opticalflow/rubberwhale2.png", cv::IMREAD_GRAYSCALE);
150     ASSERT_FALSE(frame1.empty());
151
152     cv::ocl::OpticalFlowDual_TVL1_OCL d_alg;
153     cv::Mat flowx = randomMat(frame0.size(), CV_32FC1, 0, 0, useRoi);
154     cv::Mat flowy = randomMat(frame0.size(), CV_32FC1, 0, 0, useRoi);
155     cv::ocl::oclMat d_flowx(flowx), d_flowy(flowy);
156     d_alg(oclMat(frame0), oclMat(frame1), d_flowx, d_flowy);
157
158     cv::Ptr<cv::DenseOpticalFlow> alg = cv::createOptFlow_DualTVL1();
159     cv::Mat flow;
160     alg->calc(frame0, frame1, flow);
161     cv::Mat gold[2];
162     cv::split(flow, gold);
163
164     EXPECT_MAT_SIMILAR(gold[0], d_flowx, 3e-3);
165     EXPECT_MAT_SIMILAR(gold[1], d_flowy, 3e-3);
166 }
167 INSTANTIATE_TEST_CASE_P(OCL_Video, TVL1, Values(false, true));
168
169
170 /////////////////////////////////////////////////////////////////////////////////////////////////
171 // PyrLKOpticalFlow
172
173 PARAM_TEST_CASE(Sparse, bool, bool)
174 {
175     bool useGray;
176     bool UseSmart;
177
178     virtual void SetUp()
179     {
180         UseSmart = GET_PARAM(0);
181         useGray = GET_PARAM(1);
182     }
183 };
184
185 OCL_TEST_P(Sparse, Mat)
186 {
187     cv::Mat frame0 = readImage("gpu/opticalflow/rubberwhale1.png", useGray ? cv::IMREAD_GRAYSCALE : cv::IMREAD_COLOR);
188     ASSERT_FALSE(frame0.empty());
189
190     cv::Mat frame1 = readImage("gpu/opticalflow/rubberwhale2.png", useGray ? cv::IMREAD_GRAYSCALE : cv::IMREAD_COLOR);
191     ASSERT_FALSE(frame1.empty());
192
193     cv::Mat gray_frame;
194     if (useGray)
195         gray_frame = frame0;
196     else
197         cv::cvtColor(frame0, gray_frame, cv::COLOR_BGR2GRAY);
198
199     std::vector<cv::Point2f> pts;
200     cv::goodFeaturesToTrack(gray_frame, pts, 1000, 0.01, 0.0);
201
202     cv::ocl::oclMat d_pts;
203     cv::Mat pts_mat(1, (int)pts.size(), CV_32FC2, (void *)&pts[0]);
204     d_pts.upload(pts_mat);
205
206     cv::ocl::PyrLKOpticalFlow pyrLK;
207
208     cv::ocl::oclMat oclFrame0;
209     cv::ocl::oclMat oclFrame1;
210     cv::ocl::oclMat d_nextPts;
211     cv::ocl::oclMat d_status;
212     cv::ocl::oclMat d_err;
213
214     oclFrame0 = frame0;
215     oclFrame1 = frame1;
216
217     pyrLK.sparse(oclFrame0, oclFrame1, d_pts, d_nextPts, d_status, &d_err);
218
219     std::vector<cv::Point2f> nextPts(d_nextPts.cols);
220     cv::Mat nextPts_mat(1, d_nextPts.cols, CV_32FC2, (void *)&nextPts[0]);
221     d_nextPts.download(nextPts_mat);
222
223     std::vector<unsigned char> status(d_status.cols);
224     cv::Mat status_mat(1, d_status.cols, CV_8UC1, (void *)&status[0]);
225     d_status.download(status_mat);
226
227     std::vector<float> err(d_err.cols);
228     cv::Mat err_mat(1, d_err.cols, CV_32FC1, (void*)&err[0]);
229     d_err.download(err_mat);
230
231     std::vector<cv::Point2f> nextPts_gold;
232     std::vector<unsigned char> status_gold;
233     std::vector<float> err_gold;
234     cv::calcOpticalFlowPyrLK(frame0, frame1, pts, nextPts_gold, status_gold, err_gold);
235
236     ASSERT_EQ(nextPts_gold.size(), nextPts.size());
237     ASSERT_EQ(status_gold.size(), status.size());
238
239     size_t mistmatch = 0;
240     for (size_t i = 0; i < nextPts.size(); ++i)
241     {
242         if (status[i] != status_gold[i])
243         {
244             ++mistmatch;
245             continue;
246         }
247
248         if (status[i])
249         {
250             cv::Point2i a = nextPts[i];
251             cv::Point2i b = nextPts_gold[i];
252
253             bool eq = std::abs(a.x - b.x) < 1 && std::abs(a.y - b.y) < 1;
254             //float errdiff = std::abs(err[i] - err_gold[i]);
255             float errdiff = 0.0f;
256
257             if (!eq || errdiff > 1e-1)
258                 ++mistmatch;
259         }
260     }
261
262     double bad_ratio = static_cast<double>(mistmatch) / (nextPts.size());
263
264     ASSERT_LE(bad_ratio, 0.02f);
265
266 }
267
268 INSTANTIATE_TEST_CASE_P(OCL_Video, Sparse, Combine(
269     Values(false, true),
270     Values(false, true)));
271 //////////////////////////////////////////////////////
272 // FarnebackOpticalFlow
273
274 namespace
275 {
276     IMPLEMENT_PARAM_CLASS(PyrScale, double)
277         IMPLEMENT_PARAM_CLASS(PolyN, int)
278         CV_FLAGS(FarnebackOptFlowFlags, 0, OPTFLOW_FARNEBACK_GAUSSIAN)
279         IMPLEMENT_PARAM_CLASS(UseInitFlow, bool)
280 }
281
282 PARAM_TEST_CASE(Farneback, PyrScale, PolyN, FarnebackOptFlowFlags, UseInitFlow)
283 {
284     double pyrScale;
285     int polyN;
286     int flags;
287     bool useInitFlow;
288
289     virtual void SetUp()
290     {
291         pyrScale = GET_PARAM(0);
292         polyN = GET_PARAM(1);
293         flags = GET_PARAM(2);
294         useInitFlow = GET_PARAM(3);
295     }
296 };
297
298 OCL_TEST_P(Farneback, Accuracy)
299 {
300     cv::Mat frame0 = readImage("gpu/opticalflow/rubberwhale1.png", cv::IMREAD_GRAYSCALE);
301     ASSERT_FALSE(frame0.empty());
302
303     cv::Mat frame1 = readImage("gpu/opticalflow/rubberwhale2.png", cv::IMREAD_GRAYSCALE);
304     ASSERT_FALSE(frame1.empty());
305
306     double polySigma = polyN <= 5 ? 1.1 : 1.5;
307
308     cv::ocl::FarnebackOpticalFlow farn;
309     farn.pyrScale = pyrScale;
310     farn.polyN = polyN;
311     farn.polySigma = polySigma;
312     farn.flags = flags;
313
314     cv::ocl::oclMat d_flowx, d_flowy;
315     farn(oclMat(frame0), oclMat(frame1), d_flowx, d_flowy);
316
317     cv::Mat flow;
318     if (useInitFlow)
319     {
320         cv::Mat flowxy[] = {cv::Mat(d_flowx), cv::Mat(d_flowy)};
321         cv::merge(flowxy, 2, flow);
322
323         farn.flags |= cv::OPTFLOW_USE_INITIAL_FLOW;
324         farn(oclMat(frame0), oclMat(frame1), d_flowx, d_flowy);
325     }
326
327     cv::calcOpticalFlowFarneback(
328         frame0, frame1, flow, farn.pyrScale, farn.numLevels, farn.winSize,
329         farn.numIters, farn.polyN, farn.polySigma, farn.flags);
330
331     std::vector<cv::Mat> flowxy;
332     cv::split(flow, flowxy);
333
334     EXPECT_MAT_SIMILAR(flowxy[0], d_flowx, 0.1);
335     EXPECT_MAT_SIMILAR(flowxy[1], d_flowy, 0.1);
336 }
337
338 INSTANTIATE_TEST_CASE_P(OCL_Video, Farneback, testing::Combine(
339     testing::Values(PyrScale(0.3), PyrScale(0.5), PyrScale(0.8)),
340     testing::Values(PolyN(5), PolyN(7)),
341     testing::Values(FarnebackOptFlowFlags(0), FarnebackOptFlowFlags(cv::OPTFLOW_FARNEBACK_GAUSSIAN)),
342     testing::Values(UseInitFlow(false), UseInitFlow(true))));
343
344 #endif // HAVE_OPENCL