1
0
mirror of https://github.com/opencv/opencv.git synced 2026-07-29 23:33:05 +04:00

features2d: add OpenCL acceleration for BFMatcher cross-check

BFMatcher::match() with crossCheck=true previously skipped the OCL
dispatch in knnMatchImpl entirely, falling back to CPU even when UMat
inputs and an OpenCL device were available. This adds
ocl_matchWithCrossCheck() for CV_32FC1 descriptors (e.g. SIFT, SURF):
both the forward and reverse nearest-neighbour passes run on the GPU
via the existing ocl_matchSingle() kernel, then the cross-check filter
runs on the CPU. Only two small index arrays (1×N ints) are downloaded
— the O(N²×D) distance work stays on the device.

The OCL dispatch in knnMatchImpl is also refactored to unify the
Mat/UMat train collection selection before branching on crossCheck.

On a NVIDIA RTX 3060 with SIFT descriptors the OCL path is 6–9×
faster than CPU at 2k–10k features per image. Binary descriptors
(ORB, BRIEF — CV_8U) are unaffected; the existing type guard in
ocl_matchSingle keeps them on the CPU path as before.

Also adds a correctness test (Features2d_BFMatcher_CrossCheck) and an
OCL perf test (BruteForceMatcherFixture/MatchCrossCheck).
This commit is contained in:
Anand Mahesh
2026-03-29 21:43:23 +05:30
parent c7732e1043
commit 4acd2ed6cc
3 changed files with 115 additions and 14 deletions
@@ -123,6 +123,27 @@ OCL_PERF_TEST_P(BruteForceMatcherFixture, RadiusMatch, ::testing::Combine(OCL_PE
SANITY_CHECK_MATCHES(matches1, 1e-3);
}
OCL_PERF_TEST_P(BruteForceMatcherFixture, MatchCrossCheck, ::testing::Combine(OCL_PERF_ENUM(OCL_SIZE_1, OCL_SIZE_2, OCL_SIZE_3), OCL_PERF_ENUM((MatType)CV_32FC1) ) )
{
const Size_MatType_t params = GetParam();
const Size srcSize = get<0>(params);
const int type = get<1>(params);
checkDeviceMaxMemoryAllocSize(srcSize, type);
vector<DMatch> matches;
UMat uquery(srcSize, type), utrain(srcSize, type);
declare.in(uquery, utrain, WARMUP_RNG);
BFMatcher matcher(NORM_L2, true /*crossCheck*/);
OCL_TEST_CYCLE()
matcher.match(uquery, utrain, matches);
SANITY_CHECK_MATCHES(matches, 1e-3);
}
} // ocl
} // cvtest