• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*M///////////////////////////////////////////////////////////////////////////////////////
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 //
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 // Copyright (C) 2010-2012, Multicoreware, Inc., all rights reserved.
16 // Third party copyrights are property of their respective owners.
17 //
18 // Redistribution and use in source and binary forms, with or without modification,
19 // are permitted provided that the following conditions are met:
20 //
21 //   * Redistribution's of source code must retain the above copyright notice,
22 //     this list of conditions and the following disclaimer.
23 //
24 //   * Redistribution's in binary form must reproduce the above copyright notice,
25 //     this list of conditions and the following disclaimer in the documentation
26 //     and/or other materials provided with the distribution.
27 //
28 //   * The name of the copyright holders may not be used to endorse or promote products
29 //     derived from this software without specific prior written permission.
30 //
31 // This software is provided by the copyright holders and contributors "as is" and
32 // any express or implied warranties, including, but not limited to, the implied
33 // warranties of merchantability and fitness for a particular purpose are disclaimed.
34 // In no event shall the Intel Corporation or contributors be liable for any direct,
35 // indirect, incidental, special, exemplary, or consequential damages
36 // (including, but not limited to, procurement of substitute goods or services;
37 // loss of use, data, or profits; or business interruption) however caused
38 // and on any theory of liability, whether in contract, strict liability,
39 // or tort (including negligence or otherwise) arising in any way out of
40 // the use of this software, even if advised of the possibility of such damage.
41 //
42 //M*/
43 
44 
45 #include "../test_precomp.hpp"
46 #include "opencv2/ts/ocl_test.hpp"
47 
48 
49 #ifdef HAVE_OPENCL
50 
51 
52 namespace cvtest {
53 namespace ocl {
54 
55 /////////////////////////////////////////////////////////////////////////////////////////////////
56 // PyrLKOpticalFlow
57 
PARAM_TEST_CASE(PyrLKOpticalFlow,int,int)58 PARAM_TEST_CASE(PyrLKOpticalFlow, int, int)
59 {
60     Size winSize;
61     int maxLevel;
62     TermCriteria criteria;
63     int flags;
64     double minEigThreshold;
65 
66     virtual void SetUp()
67     {
68         winSize = Size(GET_PARAM(0), GET_PARAM(0));
69         maxLevel = GET_PARAM(1);
70         criteria = TermCriteria(TermCriteria::COUNT+TermCriteria::EPS, 30, 0.01);
71         flags = 0;
72         minEigThreshold = 1e-4f;
73     }
74 };
75 
OCL_TEST_P(PyrLKOpticalFlow,Mat)76 OCL_TEST_P(PyrLKOpticalFlow, Mat)
77 {
78     static const int npoints = 1000;
79     static const float eps = 0.03f;
80 
81     cv::Mat frame0 = readImage("optflow/RubberWhale1.png", cv::IMREAD_GRAYSCALE);
82     ASSERT_FALSE(frame0.empty());
83     UMat umatFrame0; frame0.copyTo(umatFrame0);
84 
85     cv::Mat frame1 = readImage("optflow/RubberWhale2.png", cv::IMREAD_GRAYSCALE);
86     ASSERT_FALSE(frame1.empty());
87     UMat umatFrame1; frame1.copyTo(umatFrame1);
88 
89     std::vector<cv::Point2f> pts;
90     cv::goodFeaturesToTrack(frame0, pts, npoints, 0.01, 0.0);
91 
92     std::vector<cv::Point2f> cpuNextPts;
93     std::vector<unsigned char> cpuStatusCPU;
94     std::vector<float> cpuErr;
95     OCL_OFF(cv::calcOpticalFlowPyrLK(frame0, frame1, pts, cpuNextPts, cpuStatusCPU, cpuErr, winSize, maxLevel, criteria, flags, minEigThreshold));
96 
97     UMat umatNextPts, umatStatus, umatErr;
98     OCL_ON(cv::calcOpticalFlowPyrLK(umatFrame0, umatFrame1, pts, umatNextPts, umatStatus, umatErr, winSize, maxLevel, criteria, flags, minEigThreshold));
99     std::vector<cv::Point2f> nextPts; umatNextPts.reshape(2, 1).copyTo(nextPts);
100     std::vector<unsigned char> status; umatStatus.reshape(1, 1).copyTo(status);
101     std::vector<float> err; umatErr.reshape(1, 1).copyTo(err);
102 
103     ASSERT_EQ(cpuNextPts.size(), nextPts.size());
104     ASSERT_EQ(cpuStatusCPU.size(), status.size());
105 
106     size_t mistmatch = 0;
107     for (size_t i = 0; i < nextPts.size(); ++i)
108     {
109         if (status[i] != cpuStatusCPU[i])
110         {
111             ++mistmatch;
112             continue;
113         }
114 
115         if (status[i])
116         {
117             cv::Point2i a = nextPts[i];
118             cv::Point2i b = cpuNextPts[i];
119 
120             bool eq = std::abs(a.x - b.x) < 1 && std::abs(a.y - b.y) < 1;
121             float errdiff = 0.0f;
122 
123             if (!eq || errdiff > 1e-1)
124                 ++mistmatch;
125         }
126     }
127 
128     double bad_ratio = static_cast<double>(mistmatch) / (nextPts.size());
129 
130     ASSERT_LE(bad_ratio, eps);
131 }
132 
133 OCL_INSTANTIATE_TEST_CASE_P(Video, PyrLKOpticalFlow,
134                             Combine(
135                                 Values(21, 25),
136                                 Values(3, 5)
137                                 )
138                            );
139 
140 } } // namespace cvtest::ocl
141 
142 
143 #endif // HAVE_OPENCL