1 ///////////////////////////////////////////////////////////////////////////////////////
3 // IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
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.
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.
20 // Redistribution and use in source and binary forms, with or without modification,
21 // are permitted provided that the following conditions are met:
23 // * Redistribution's of source code must retain the above copyright notice,
24 // this list of conditions and the following disclaimer.
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.
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.
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.
46 #include "test_precomp.hpp"
52 using namespace cv::ocl;
53 using namespace cvtest;
54 using namespace testing;
57 //////////////////////////////////////////////////////
58 // GoodFeaturesToTrack
61 IMPLEMENT_PARAM_CLASS(MinDistance, double)
63 PARAM_TEST_CASE(GoodFeaturesToTrack, MinDistance)
69 minDistance = GET_PARAM(0);
73 OCL_TEST_P(GoodFeaturesToTrack, Accuracy)
75 cv::Mat frame = readImage("gpu/opticalflow/rubberwhale1.png", cv::IMREAD_GRAYSCALE);
76 ASSERT_FALSE(frame.empty());
78 int maxCorners = 1000;
79 double qualityLevel = 0.01;
81 cv::ocl::GoodFeaturesToTrackDetector_OCL detector(maxCorners, qualityLevel, minDistance);
83 cv::ocl::oclMat d_pts;
84 detector(oclMat(frame), d_pts);
86 ASSERT_FALSE(d_pts.empty());
88 std::vector<cv::Point2f> pts(d_pts.cols);
90 detector.downloadPoints(d_pts, pts);
92 std::vector<cv::Point2f> pts_gold;
93 cv::goodFeaturesToTrack(frame, pts_gold, maxCorners, qualityLevel, minDistance);
95 ASSERT_EQ(pts_gold.size(), pts.size());
98 for (size_t i = 0; i < pts.size(); ++i)
100 cv::Point2i a = pts_gold[i];
101 cv::Point2i b = pts[i];
103 bool eq = std::abs(a.x - b.x) < 1 && std::abs(a.y - b.y) < 1;
109 double bad_ratio = static_cast<double>(mistmatch) / pts.size();
111 ASSERT_LE(bad_ratio, 0.01);
114 OCL_TEST_P(GoodFeaturesToTrack, EmptyCorners)
116 int maxCorners = 1000;
117 double qualityLevel = 0.01;
119 cv::ocl::GoodFeaturesToTrackDetector_OCL detector(maxCorners, qualityLevel, minDistance);
121 cv::ocl::oclMat src(100, 100, CV_8UC1, cv::Scalar::all(0));
122 cv::ocl::oclMat corners(1, maxCorners, CV_32FC2);
124 detector(src, corners);
126 ASSERT_TRUE(corners.empty());
129 INSTANTIATE_TEST_CASE_P(OCL_Video, GoodFeaturesToTrack,
130 testing::Values(MinDistance(0.0), MinDistance(3.0)));
132 //////////////////////////////////////////////////////////////////////////
133 PARAM_TEST_CASE(TVL1, bool)
139 useRoi = GET_PARAM(0);
144 OCL_TEST_P(TVL1, Accuracy)
146 cv::Mat frame0 = readImage("gpu/opticalflow/rubberwhale1.png", cv::IMREAD_GRAYSCALE);
147 ASSERT_FALSE(frame0.empty());
149 cv::Mat frame1 = readImage("gpu/opticalflow/rubberwhale2.png", cv::IMREAD_GRAYSCALE);
150 ASSERT_FALSE(frame1.empty());
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);
158 cv::Ptr<cv::DenseOpticalFlow> alg = cv::createOptFlow_DualTVL1();
160 alg->calc(frame0, frame1, flow);
162 cv::split(flow, gold);
164 EXPECT_MAT_SIMILAR(gold[0], d_flowx, 3e-3);
165 EXPECT_MAT_SIMILAR(gold[1], d_flowy, 3e-3);
167 INSTANTIATE_TEST_CASE_P(OCL_Video, TVL1, Values(false, true));
170 /////////////////////////////////////////////////////////////////////////////////////////////////
173 PARAM_TEST_CASE(Sparse, bool, bool)
180 UseSmart = GET_PARAM(0);
181 useGray = GET_PARAM(1);
185 OCL_TEST_P(Sparse, Mat)
187 cv::Mat frame0 = readImage("gpu/opticalflow/rubberwhale1.png", useGray ? cv::IMREAD_GRAYSCALE : cv::IMREAD_COLOR);
188 ASSERT_FALSE(frame0.empty());
190 cv::Mat frame1 = readImage("gpu/opticalflow/rubberwhale2.png", useGray ? cv::IMREAD_GRAYSCALE : cv::IMREAD_COLOR);
191 ASSERT_FALSE(frame1.empty());
197 cv::cvtColor(frame0, gray_frame, cv::COLOR_BGR2GRAY);
199 std::vector<cv::Point2f> pts;
200 cv::goodFeaturesToTrack(gray_frame, pts, 1000, 0.01, 0.0);
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);
206 cv::ocl::PyrLKOpticalFlow pyrLK;
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;
217 pyrLK.sparse(oclFrame0, oclFrame1, d_pts, d_nextPts, d_status, &d_err);
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);
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);
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);
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);
236 ASSERT_EQ(nextPts_gold.size(), nextPts.size());
237 ASSERT_EQ(status_gold.size(), status.size());
239 size_t mistmatch = 0;
240 for (size_t i = 0; i < nextPts.size(); ++i)
242 if (status[i] != status_gold[i])
250 cv::Point2i a = nextPts[i];
251 cv::Point2i b = nextPts_gold[i];
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;
257 if (!eq || errdiff > 1e-1)
262 double bad_ratio = static_cast<double>(mistmatch) / (nextPts.size());
264 ASSERT_LE(bad_ratio, 0.02f);
268 INSTANTIATE_TEST_CASE_P(OCL_Video, Sparse, Combine(
270 Values(false, true)));
271 //////////////////////////////////////////////////////
272 // FarnebackOpticalFlow
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)
282 PARAM_TEST_CASE(Farneback, PyrScale, PolyN, FarnebackOptFlowFlags, UseInitFlow)
291 pyrScale = GET_PARAM(0);
292 polyN = GET_PARAM(1);
293 flags = GET_PARAM(2);
294 useInitFlow = GET_PARAM(3);
298 OCL_TEST_P(Farneback, Accuracy)
300 cv::Mat frame0 = readImage("gpu/opticalflow/rubberwhale1.png", cv::IMREAD_GRAYSCALE);
301 ASSERT_FALSE(frame0.empty());
303 cv::Mat frame1 = readImage("gpu/opticalflow/rubberwhale2.png", cv::IMREAD_GRAYSCALE);
304 ASSERT_FALSE(frame1.empty());
306 double polySigma = polyN <= 5 ? 1.1 : 1.5;
308 cv::ocl::FarnebackOpticalFlow farn;
309 farn.pyrScale = pyrScale;
311 farn.polySigma = polySigma;
314 cv::ocl::oclMat d_flowx, d_flowy;
315 farn(oclMat(frame0), oclMat(frame1), d_flowx, d_flowy);
320 cv::Mat flowxy[] = {cv::Mat(d_flowx), cv::Mat(d_flowy)};
321 cv::merge(flowxy, 2, flow);
323 farn.flags |= cv::OPTFLOW_USE_INITIAL_FLOW;
324 farn(oclMat(frame0), oclMat(frame1), d_flowx, d_flowy);
327 cv::calcOpticalFlowFarneback(
328 frame0, frame1, flow, farn.pyrScale, farn.numLevels, farn.winSize,
329 farn.numIters, farn.polyN, farn.polySigma, farn.flags);
331 std::vector<cv::Mat> flowxy;
332 cv::split(flow, flowxy);
334 EXPECT_MAT_SIMILAR(flowxy[0], d_flowx, 0.1);
335 EXPECT_MAT_SIMILAR(flowxy[1], d_flowy, 0.1);
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))));
344 #endif // HAVE_OPENCL