mirror of
https://github.com/opencv/opencv.git
synced 2026-07-25 21:33:04 +04:00
60c705e93b
Feat: add OpenCL SIFT detector and descriptor #29450 Add OpenCL implementation of SIFT detector and descriptor extractor, triggered via T-API when the caller passes a UMat. Targets the features module (5.x directory layout). Implementation: - 4 OpenCL kernels in sift.cl: gaussian_blur_h, gaussian_blur_v, detect_and_orient, and compute_descriptor. - Host-side dispatch in sift.dispatch.cpp using CV_OCL_RUN_ macro, pure additions to the existing CPU path. - T-API entry: detectAndCompute, detect, compute. Gating and fallback: - 64-bit only. 32-bit falls back to CPU via sizeof(void*) > 4 guard. - OpenCL 1.1 or later required. Devices below 1.1 fall back to CPU. - No-OpenCL builds fall back to CPU. Verified with WITH_OPENCL=OFF: 79 tests pass, same 3 pre-existing failures (DISK/AFFINE_FEATURE missing .npy data files). Correctness: - nOctaveLayers > 3 OOB fix: gk_coeffs and gk_radius resized to nOctaveLayers+2 via std::vector (were fixed size 5). - Regression test for nOctaveLayers > 3. - 10 OCL SIFT tests pass, 60 OCL tests pass, 139 features tests pass (3 pre-existing failures unrelated to SIFT). Performance optimizations: - native_exp, native_sqrt, native_recip for hardware approximations (OpenCL 1.1 builtins). - Eliminate intermediate rawDst[128] buffer; normalize in-place from hist[]. - Non-blocking kernel launches with single ocl::finish() before host reads keypoint count. - Custom separable Gaussian blur for init image bypassing T-API GaussianBlur dispatch overhead. - exp2() replaces pow(2.0f, x); scl_octv reused. - Hoist UMat tmp allocation out of pyramid loop. - Interleaved keypoint output buffer (6 arrays to 1) for coalesced writes and fewer copies. - Consolidated descriptor keypoint copies (2N to 2 host-to-device transfers). - std::map replaced with flat vector indexed by pyramid level (O(1) vs O(log n)). - DoG computed on-the-fly in detect kernel via READ_DOG macro, eliminating 45 subtract() calls and dog_pack allocation. - Shared gauss_packs between detect and descriptor, eliminating duplicate copyTo. - Cross-level packing per octave: one descriptor kernel launch per octave instead of per level. Keypoint buffer expanded to 5 floats (added layer index). Levels packed into single UMat. Reduces kernel launch overhead and improves GPU utilization for octaves with few keypoints. - __local memory tiling for Gaussian blur kernels with 16x16 workgroups and cooperative halo loading. Reduces global memory traffic. - Direct uchar descriptor output for CV_8U: kernel writes convert_uchar_sat_rte directly instead of float buffer + host convertTo pass. Eliminates temp allocation and extra kernel launch. Performance result (stitching/s2.jpg resized): - DetectAndCompute: OCL wins at >=960x540 (1.32x at 960x540, 1.80x at 1280x720, 1.87x at 1920x1080). - Compute-only: OCL wins at >=1280x720 (1.60x at 1280x720, 1.78x at 1920x1080). Tests: - modules/features/test/ocl/test_feature2d.cpp with OCL SIFT correctness tests on real images (leuven img1.png, a3.png, s2.jpg). - DescriptorType, Regression_26139, and Batch tests mirror CPU coverage (CV_8U descriptor type, single-keypoint edge case, 6-image detect+compute). - modules/features/perf/opencl/perf_sift.cpp with real image and 5-size sweep. - CPU SIFT_Scaled fixture added to perf_feature2d.cpp for aligned OCL vs CPU comparison. ### Pull Request Readiness Checklist See details at https://github.com/opencv/opencv/wiki/How_to_contribute#making-a-good-pull-request - [x] I agree to contribute to the project under Apache 2 License. - [x] To the best of my knowledge, the proposed patch is not based on a code under GPL or another license that is incompatible with OpenCV - [x] The PR is proposed to the proper branch - [ ] There is a reference to the original bug report and related work - [x] There is accuracy test, performance test and test data in opencv_extra repository, if applicable Patch to opencv_extra has the same branch name. - [x] The feature is well documented and sample code can be built with the project CMake
192 lines
6.2 KiB
C++
192 lines
6.2 KiB
C++
// This file is part of OpenCV project.
|
|
// It is subject to the license terms in the LICENSE file found in the top-level directory
|
|
// of this distribution and at http://opencv.org/license.html
|
|
|
|
#include "../test_precomp.hpp"
|
|
#include "opencv2/ts/ocl_test.hpp"
|
|
#include <functional>
|
|
|
|
#ifdef HAVE_OPENCL
|
|
|
|
namespace opencv_test {
|
|
namespace ocl {
|
|
|
|
#define TEST_IMAGES testing::Values(\
|
|
"detectors_descriptors_evaluation/images_datasets/leuven/img1.png",\
|
|
"../stitching/a3.png", \
|
|
"../stitching/s2.jpg")
|
|
|
|
PARAM_TEST_CASE(Feature2DFixture, std::function<Ptr<Feature2D>()>, std::string, double)
|
|
{
|
|
std::string filename;
|
|
double desc_eps;
|
|
Mat image, descriptors;
|
|
vector<KeyPoint> keypoints;
|
|
UMat uimage, udescriptors;
|
|
vector<KeyPoint> ukeypoints;
|
|
Ptr<Feature2D> feature;
|
|
|
|
virtual void SetUp()
|
|
{
|
|
feature = GET_PARAM(0)();
|
|
filename = GET_PARAM(1);
|
|
desc_eps = GET_PARAM(2);
|
|
|
|
image = readImage(filename);
|
|
ASSERT_FALSE(image.empty());
|
|
image.copyTo(uimage);
|
|
|
|
OCL_OFF(feature->detect(image, keypoints));
|
|
OCL_ON(feature->detect(uimage, ukeypoints));
|
|
OCL_OFF(feature->compute(image, keypoints, descriptors));
|
|
OCL_ON(feature->compute(uimage, keypoints, udescriptors));
|
|
}
|
|
};
|
|
|
|
OCL_TEST_P(Feature2DFixture, KeypointsSame)
|
|
{
|
|
size_t count_diff = (keypoints.size() > ukeypoints.size()) ?
|
|
keypoints.size() - ukeypoints.size() : ukeypoints.size() - keypoints.size();
|
|
EXPECT_LE(count_diff, (size_t)(std::min(keypoints.size(), ukeypoints.size()) * 0.20 + 1));
|
|
|
|
std::vector<KeyPoint> cpu_sorted = keypoints, ocl_sorted = ukeypoints;
|
|
std::sort(cpu_sorted.begin(), cpu_sorted.end(), [](const KeyPoint& a, const KeyPoint& b) {
|
|
return a.pt.x < b.pt.x || (a.pt.x == b.pt.x && a.pt.y < b.pt.y);
|
|
});
|
|
std::sort(ocl_sorted.begin(), ocl_sorted.end(), [](const KeyPoint& a, const KeyPoint& b) {
|
|
return a.pt.x < b.pt.x || (a.pt.x == b.pt.x && a.pt.y < b.pt.y);
|
|
});
|
|
|
|
int matched = 0;
|
|
size_t j = 0;
|
|
for (size_t i = 0; i < cpu_sorted.size(); i++)
|
|
{
|
|
while (j < ocl_sorted.size() && ocl_sorted[j].pt.x < cpu_sorted[i].pt.x - 2.0f)
|
|
j++;
|
|
for (size_t k = j; k < ocl_sorted.size() && ocl_sorted[k].pt.x <= cpu_sorted[i].pt.x + 2.0f; k++)
|
|
{
|
|
if (std::abs(ocl_sorted[k].pt.y - cpu_sorted[i].pt.y) < 2.0f)
|
|
{
|
|
matched++;
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
size_t n = std::min(cpu_sorted.size(), ocl_sorted.size());
|
|
EXPECT_GE(matched, (int)(n * 0.70));
|
|
}
|
|
|
|
OCL_TEST_P(Feature2DFixture, DescriptorsSame)
|
|
{
|
|
EXPECT_EQ(descriptors.size(), udescriptors.size());
|
|
ASSERT_EQ(descriptors.type(), udescriptors.type());
|
|
Mat udesc = udescriptors.getMat(ACCESS_READ);
|
|
double max_diff = 0.0;
|
|
for (int r = 0; r < descriptors.rows; r++)
|
|
{
|
|
for (int c = 0; c < descriptors.cols; c++)
|
|
{
|
|
double d;
|
|
if (descriptors.type() == CV_8U)
|
|
d = std::abs((double)descriptors.at<uchar>(r, c) - (double)udesc.at<uchar>(r, c));
|
|
else
|
|
d = std::abs((double)descriptors.at<float>(r, c) - (double)udesc.at<float>(r, c));
|
|
max_diff = std::max(max_diff, d);
|
|
}
|
|
}
|
|
EXPECT_LE(max_diff, desc_eps);
|
|
}
|
|
|
|
OCL_INSTANTIATE_TEST_CASE_P(SIFT, Feature2DFixture,
|
|
testing::Combine(testing::Values([]() { return SIFT::create(); }), TEST_IMAGES, testing::Values(2.0)));
|
|
|
|
OCL_TEST(SIFT, NonDefaultNOctaveLayers)
|
|
{
|
|
Mat image = TestUtils::readImage("../stitching/s2.jpg", IMREAD_GRAYSCALE);
|
|
ASSERT_FALSE(image.empty());
|
|
UMat uimage;
|
|
image.copyTo(uimage);
|
|
|
|
Ptr<SIFT> sift = SIFT::create(0, 4);
|
|
vector<KeyPoint> keypoints;
|
|
UMat descriptors;
|
|
ASSERT_NO_THROW(sift->detectAndCompute(uimage, noArray(), keypoints, descriptors, false));
|
|
EXPECT_GT(keypoints.size(), 20u);
|
|
EXPECT_EQ((size_t)descriptors.rows, keypoints.size());
|
|
}
|
|
|
|
OCL_TEST(SIFT, DescriptorType)
|
|
{
|
|
Mat image = imread(cvtest::findDataFile("features2d/tsukuba.png"), IMREAD_GRAYSCALE);
|
|
ASSERT_FALSE(image.empty());
|
|
UMat uimage;
|
|
image.copyTo(uimage);
|
|
|
|
vector<KeyPoint> keypoints;
|
|
UMat descriptorsFloat, descriptorsUchar;
|
|
|
|
Ptr<SIFT> siftFloat = SIFT::create(0, 3, 0.04, 10, 1.6, CV_32F);
|
|
siftFloat->detectAndCompute(uimage, noArray(), keypoints, descriptorsFloat, false);
|
|
ASSERT_EQ(descriptorsFloat.type(), CV_32F) << "type mismatch";
|
|
|
|
Ptr<SIFT> siftUchar = SIFT::create(0, 3, 0.04, 10, 1.6, CV_8U);
|
|
siftUchar->detectAndCompute(uimage, noArray(), keypoints, descriptorsUchar, false);
|
|
ASSERT_EQ(descriptorsUchar.type(), CV_8U) << "type mismatch";
|
|
|
|
Mat df = descriptorsFloat.getMat(ACCESS_READ);
|
|
Mat du = descriptorsUchar.getMat(ACCESS_READ);
|
|
Mat descriptorsFloat2;
|
|
du.assignTo(descriptorsFloat2, CV_32F);
|
|
Mat diff = df != descriptorsFloat2;
|
|
EXPECT_EQ(countNonZero(diff), 0) << "descriptors are not identical";
|
|
}
|
|
|
|
OCL_TEST(SIFT, Regression_26139)
|
|
{
|
|
UMat uimage(Size(300, 300), CV_8UC1, Scalar::all(0));
|
|
std::vector<KeyPoint> kps {
|
|
KeyPoint(154.076813f, 136.160904f, 111.078636f, 216.195618f, 0.00000899323549f, 7)
|
|
};
|
|
Ptr<SIFT> extractor = SIFT::create();
|
|
UMat descriptors;
|
|
extractor->compute(uimage, kps, descriptors);
|
|
ASSERT_EQ(descriptors.size(), Size(128, 1));
|
|
}
|
|
|
|
OCL_TEST(SIFT, Batch)
|
|
{
|
|
string path = cvtest::TS::ptr()->get_data_path() + "detectors_descriptors_evaluation/images_datasets/graf";
|
|
vector<UMat> imgs;
|
|
vector<UMat> descriptors;
|
|
vector<vector<KeyPoint> > keypoints;
|
|
int n = 6;
|
|
Ptr<SIFT> sift = SIFT::create();
|
|
|
|
for (int i = 0; i < n; i++)
|
|
{
|
|
string imgname = format("%s/img%d.png", path.c_str(), i + 1);
|
|
Mat img = imread(imgname, IMREAD_GRAYSCALE);
|
|
ASSERT_FALSE(img.empty()) << "Failed to load " << imgname;
|
|
UMat uimg;
|
|
img.copyTo(uimg);
|
|
imgs.push_back(uimg);
|
|
}
|
|
|
|
sift->detect(imgs, keypoints);
|
|
sift->compute(imgs, keypoints, descriptors);
|
|
|
|
ASSERT_EQ((int)keypoints.size(), n);
|
|
ASSERT_EQ((int)descriptors.size(), n);
|
|
|
|
for (int i = 0; i < n; i++)
|
|
{
|
|
EXPECT_GT((int)keypoints[i].size(), 100);
|
|
EXPECT_GT(descriptors[i].rows, 100);
|
|
}
|
|
}
|
|
|
|
}//ocl
|
|
}//opencv_test
|
|
|
|
#endif //HAVE_OPENCL
|