Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Tetragramm
GitHub Repository: Tetragramm/opencv
Path: blob/master/modules/video/test/ocl/test_optflowpyrlk.cpp
16354 views
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
namespace opencv_test { namespace ocl {
52
53
/////////////////////////////////////////////////////////////////////////////////////////////////
54
// PyrLKOpticalFlow
55
56
PARAM_TEST_CASE(PyrLKOpticalFlow, int, int)
57
{
58
Size winSize;
59
int maxLevel;
60
TermCriteria criteria;
61
int flags;
62
double minEigThreshold;
63
64
virtual void SetUp()
65
{
66
winSize = Size(GET_PARAM(0), GET_PARAM(0));
67
maxLevel = GET_PARAM(1);
68
criteria = TermCriteria(TermCriteria::COUNT+TermCriteria::EPS, 30, 0.01);
69
flags = 0;
70
minEigThreshold = 1e-4f;
71
}
72
};
73
74
OCL_TEST_P(PyrLKOpticalFlow, Mat)
75
{
76
static const int npoints = 1000;
77
static const float eps = 0.03f;
78
static const float erreps = 0.1f;
79
80
cv::Mat frame0 = readImage("optflow/RubberWhale1.png", cv::IMREAD_GRAYSCALE);
81
ASSERT_FALSE(frame0.empty());
82
UMat umatFrame0; frame0.copyTo(umatFrame0);
83
84
cv::Mat frame1 = readImage("optflow/RubberWhale2.png", cv::IMREAD_GRAYSCALE);
85
ASSERT_FALSE(frame1.empty());
86
UMat umatFrame1; frame1.copyTo(umatFrame1);
87
88
// SKIP unstable tests
89
#ifdef __linux__
90
if (cvtest::skipUnstableTests && ocl::useOpenCL())
91
{
92
if (ocl::Device::getDefault().isIntel())
93
throw cvtest::SkipTestException("Skip unstable test");
94
}
95
#endif
96
97
std::vector<cv::Point2f> pts;
98
cv::goodFeaturesToTrack(frame0, pts, npoints, 0.01, 0.0);
99
100
std::vector<cv::Point2f> cpuNextPts;
101
std::vector<unsigned char> cpuStatusCPU;
102
std::vector<float> cpuErr;
103
OCL_OFF(cv::calcOpticalFlowPyrLK(frame0, frame1, pts, cpuNextPts, cpuStatusCPU, cpuErr, winSize, maxLevel, criteria, flags, minEigThreshold));
104
105
UMat umatNextPts, umatStatus, umatErr;
106
OCL_ON(cv::calcOpticalFlowPyrLK(umatFrame0, umatFrame1, pts, umatNextPts, umatStatus, umatErr, winSize, maxLevel, criteria, flags, minEigThreshold));
107
std::vector<cv::Point2f> nextPts; umatNextPts.reshape(2, 1).copyTo(nextPts);
108
std::vector<unsigned char> status; umatStatus.reshape(1, 1).copyTo(status);
109
std::vector<float> err; umatErr.reshape(1, 1).copyTo(err);
110
111
ASSERT_EQ(cpuNextPts.size(), nextPts.size());
112
ASSERT_EQ(cpuStatusCPU.size(), status.size());
113
114
size_t mistmatch = 0;
115
size_t errmatch = 0;
116
117
for (size_t i = 0; i < nextPts.size(); ++i)
118
{
119
if (status[i] != cpuStatusCPU[i])
120
{
121
++mistmatch;
122
continue;
123
}
124
125
if (status[i])
126
{
127
cv::Point2i a = nextPts[i];
128
cv::Point2i b = cpuNextPts[i];
129
130
bool eq = std::abs(a.x - b.x) < 1 && std::abs(a.y - b.y) < 1;
131
float errdiff = 0.0f;
132
133
if (!eq || errdiff > 1e-1)
134
{
135
++mistmatch;
136
continue;
137
}
138
139
eq = std::abs(cpuErr[i] - err[i]) <= (0.01 * std::max(1.0f, cpuErr[i]));
140
if(!eq)
141
++errmatch;
142
}
143
}
144
145
double bad_ratio = static_cast<double>(mistmatch) / (nextPts.size());
146
double err_ratio = static_cast<double>(errmatch) / (nextPts.size());
147
148
ASSERT_LE(bad_ratio, eps);
149
ASSERT_LE(err_ratio, erreps);
150
}
151
152
OCL_INSTANTIATE_TEST_CASE_P(Video, PyrLKOpticalFlow,
153
Combine(
154
Values(11, 15, 21, 25),
155
Values(3, 5)
156
)
157
);
158
159
160
}} // namespace
161
#endif // HAVE_OPENCL
162
163