From 281452434c4a2e373c3179e621d41b646783a59a Mon Sep 17 00:00:00 2001 From: Nicolai Behmann Date: Wed, 11 Jul 2018 18:33:31 +0200 Subject: [PATCH] Added support for OpenCV 3 CUDA ORB feature extractor and descriptor (#296) * added cuda support for orb with OpenCV 3 * added support for OpenCV 3 CUDA ORB feature detector and descriptor --- .gitignore | 1 + corelib/src/Features2d.cpp | 36 ++++++++++++++++++++++++++++-------- 2 files changed, 29 insertions(+), 8 deletions(-) diff --git a/.gitignore b/.gitignore index e6683677..1bb57945 100644 --- a/.gitignore +++ b/.gitignore @@ -2,6 +2,7 @@ .DS_Store .settings/language.settings.xml .idea/ +.vscode cmake-build-debug/ app/android/.classpath app/android/.project diff --git a/corelib/src/Features2d.cpp b/corelib/src/Features2d.cpp index 0301f3f8..e88261df 100644 --- a/corelib/src/Features2d.cpp +++ b/corelib/src/Features2d.cpp @@ -923,11 +923,11 @@ void ORB::parseParameters(const ParametersMap & parameters) gpu_ = false; } #endif - if(gpu_) + if(gpu_ && cv::cuda::getCudaEnabledDeviceCount() == 0) { - UWARN("GPU version of ORB available but not implemented yet! Using CPU version instead..."); + UWARN("GPU version of ORB not available (no GPU found)! Using CPU version instead..."); + gpu_ = false; } - gpu_ = false; #endif if(gpu_) { @@ -940,7 +940,7 @@ void ORB::parseParameters(const ParametersMap & parameters) #endif #else #ifdef HAVE_OPENCV_CUDAFEATURES2D - UFATAL("not implemented"); + _gpuOrb = CV_ORB_GPU::create(this->getMaxFeatures(), scaleFactor_, nLevels_, edgeThreshold_, firstLevel_, WTA_K_, scoreType_, patchSize_, fastThreshold_); #endif #endif } @@ -977,7 +977,14 @@ std::vector ORB::generateKeypointsImpl(const cv::Mat & image, cons #endif #else #ifdef HAVE_OPENCV_CUDAFEATURES2D - UFATAL("not implemented"); + cv::cuda::GpuMat d_image(imgRoi); + cv::cuda::GpuMat d_mask(maskRoi); + try { + _gpuOrb->detectAndCompute(d_image, d_mask, keypoints, cv::cuda::GpuMat(), false); + } catch (cv::Exception& e) { + const char* err_msg = e.what(); + UWARN("OpenCV exception caught: %s", err_msg); + } #endif #endif } @@ -1018,10 +1025,23 @@ cv::Mat ORB::generateDescriptorsImpl(const cv::Mat & image, std::vectordetectAndCompute(d_image, cv::cuda::GpuMat(), keypoints, d_descriptors, true); + } catch (cv::Exception& e) { + const char* err_msg = e.what(); + UWARN("OpenCV exception caught: %s", err_msg); + } + // Download descriptors + if (d_descriptors.empty()) + descriptors = cv::Mat(); + else + { + UASSERT(d_descriptors.type() == CV_32F || d_descriptors.type() == CV_8U); + d_descriptors.download(descriptors); + } #endif #endif }