Skip to content

Commit 06b0fe3

Browse files
committed
Merge pull request opencv#8609 from LukeZheZhu:pyrlk_err_ocl_fix
2 parents 833832a + 65be9e1 commit 06b0fe3

File tree

3 files changed

+17
-2
lines changed

3 files changed

+17
-2
lines changed

modules/video/src/lkpyramid.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -849,6 +849,9 @@ namespace
849849
return false;
850850
if (maxLevel < 0 || winSize.width <= 2 || winSize.height <= 2)
851851
return false;
852+
if (winSize.width < 16 || winSize.height < 16 ||
853+
winSize.width > 24 || winSize.height > 24)
854+
return false;
852855
calcPatchSize();
853856
if (patch.x <= 0 || patch.x >= 6 || patch.y <= 0 || patch.y >= 6)
854857
return false;

modules/video/src/opencl/pyrlk.cl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,7 @@ inline void GetPatch(image2d_t J, float x, float y,
260260

261261
inline void GetError(image2d_t J, const float x, const float y, const float* Pch, float* errval)
262262
{
263-
float diff = read_imagef(J, sampler, (float2)(x,y)).x-*Pch;
263+
float diff = (((read_imagef(J, sampler, (float2)(x,y)).x * 16384) + 256) / 512) - (((*Pch * 16384) + 256) /512);
264264
*errval += fabs(diff);
265265
}
266266

@@ -526,6 +526,6 @@ __kernel void lkSparse(image2d_t I, image2d_t J,
526526
nextPts[gid] = prevPt;
527527

528528
if (calcErr)
529-
err[gid] = smem1[0] / (float)(c_winSize_x * c_winSize_y);
529+
err[gid] = smem1[0] / (float)(32 * c_winSize_x * c_winSize_y);
530530
}
531531
}

modules/video/test/ocl/test_optflowpyrlk.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ OCL_TEST_P(PyrLKOpticalFlow, Mat)
7777
{
7878
static const int npoints = 1000;
7979
static const float eps = 0.03f;
80+
static const float erreps = 0.1f;
8081

8182
cv::Mat frame0 = readImage("optflow/RubberWhale1.png", cv::IMREAD_GRAYSCALE);
8283
ASSERT_FALSE(frame0.empty());
@@ -104,6 +105,8 @@ OCL_TEST_P(PyrLKOpticalFlow, Mat)
104105
ASSERT_EQ(cpuStatusCPU.size(), status.size());
105106

106107
size_t mistmatch = 0;
108+
size_t errmatch = 0;
109+
107110
for (size_t i = 0; i < nextPts.size(); ++i)
108111
{
109112
if (status[i] != cpuStatusCPU[i])
@@ -121,13 +124,22 @@ OCL_TEST_P(PyrLKOpticalFlow, Mat)
121124
float errdiff = 0.0f;
122125

123126
if (!eq || errdiff > 1e-1)
127+
{
124128
++mistmatch;
129+
continue;
130+
}
131+
132+
eq = std::abs(cpuErr[i] - err[i]) < 0.01;
133+
if(!eq)
134+
++errmatch;
125135
}
126136
}
127137

128138
double bad_ratio = static_cast<double>(mistmatch) / (nextPts.size());
139+
double err_ratio = static_cast<double>(errmatch) / (nextPts.size());
129140

130141
ASSERT_LE(bad_ratio, eps);
142+
ASSERT_LE(err_ratio, erreps);
131143
}
132144

133145
OCL_INSTANTIATE_TEST_CASE_P(Video, PyrLKOpticalFlow,

0 commit comments

Comments
 (0)