OpenCV CUDA 模块特征检测与描述------在GPU上执行特征描述符匹配的类cv::cuda::DescriptorMatcher

  • 操作系统:ubuntu22.04
  • OpenCV版本:OpenCV4.9
  • IDE:Visual Studio Code
  • 编程语言:C++11

算法描述

cv::cuda::DescriptorMatcher 是 OpenCV 的 CUDA 模块中用于在 GPU 上执行特征描述符匹配的类。它允许你利用 NVIDIA GPU 的并行计算能力来加速特征匹配过程,这对于需要实时处理或处理大规模数据集的应用来说非常有用。

主要功能

  • 特征描述符匹配:可以在 GPU 上高效地匹配两组特征描述符(如 SIFT、SURF 等)。
  • 多种匹配策略:支持 K-最近邻匹配(KNN)、基于半径的匹配等。
  • 跨平台兼容性:能够在任何支持 CUDA 的平台上运行。

类概述

以下是 cv::cuda::DescriptorMatcher 的一些关键成员函数和说明:

构造函数

DescriptorMatcher(const Ptr& matcher):创建一个 DescriptorMatcher 对象,通常使用其派生类(例如 BruteForceMatcher 或 FlannBasedMatcher)进行实例化。

匹配方法

  • void match(Ptr& queryDescriptors, std::vector& matches, const GpuMat& trainDescriptors = GpuMat()):在两组描述符之间找到最佳匹配项。
  • void knnMatch(Ptr& queryDescriptors, std::vector<std::vector>& matches, int k, const GpuMat& trainDescriptors = GpuMat(), bool compactResult = false):为每一条查询描述符找到其前 k 个最佳匹配。
  • void radiusMatch(Ptr& queryDescriptors, std::vector<std::vector>& matches, float maxDistance, const GpuMat& trainDescriptors = GpuMat(), bool compactResult = false):找到所有距离小于指定最大值的匹配。

添加训练描述符

  • void add(const std::vector& descriptors):添加一组训练描述符。
  • void clear():清除所有的训练描述符。

获取训练描述符

  • std::vector getTrainDescriptors() const:返回当前所有的训练描述符。
  • bool isMaskSupported() const:检查是否支持掩码。

示例代码

下面是一个简单的示例,演示如何使用 cv::cuda::DescriptorMatcher 进行特征匹配:

cpp 复制代码
#include <opencv2/cudafeatures2d.hpp>
#include <opencv2/cudaimgproc.hpp>
#include <opencv2/opencv.hpp>

int main()
{
    // 加载图像
    cv::Mat img1 = cv::imread( "/media/dingxin/data/study/OpenCV/sources/images/left.jpg", cv::IMREAD_GRAYSCALE );
    cv::Mat img2 = cv::imread( "/media/dingxin/data/study/OpenCV/sources/images/right.jpg", cv::IMREAD_GRAYSCALE );

    if ( img1.empty() || img2.empty() )
    {
        std::cerr << "无法加载图像" << std::endl;
        return -1;
    }

    // 转换到 GPU 内存
    cv::cuda::GpuMat d_img1( img1 ), d_img2( img2 );

    // 创建 CUDA ORB 检测器
    cv::Ptr< cv::cuda::ORB > orb = cv::cuda::ORB::create();

    // 存储结果:关键点是 CPU 上的 vector,描述符是 GPU 上的 GpuMat
    std::vector< cv::KeyPoint > keypoints1, keypoints2;
    cv::cuda::GpuMat descriptors1, descriptors2;

    // 提取关键点和描述符
    orb->detectAndCompute( d_img1, cv::cuda::GpuMat(), keypoints1, descriptors1 );
    orb->detectAndCompute( d_img2, cv::cuda::GpuMat(), keypoints2, descriptors2 );

    // 创建匹配器
    cv::Ptr< cv::cuda::DescriptorMatcher > matcher = cv::cuda::DescriptorMatcher::createBFMatcher( cv::NORM_HAMMING );

    // 匹配描述符
    std::vector< cv::DMatch > matches;
    matcher->match( descriptors1, descriptors2, matches );

    // 将 GPU 描述符下载回 CPU(如果需要可视化)
    cv::Mat descriptors1_cpu, descriptors2_cpu;
    descriptors1.download( descriptors1_cpu );
    descriptors2.download( descriptors2_cpu );

    // 绘制匹配结果
    cv::Mat img_matches;
    cv::drawMatches( img1, keypoints1, img2, keypoints2, matches, img_matches );

    cv::imshow( "Matches", img_matches );
    cv::waitKey( 0 );

    return 0;
}

运行结果

相关推荐
长空任鸟飞_阿康7 分钟前
在 Vue 3.5 中优雅地集成 wangEditor,并定制“AI 工具”下拉菜单(总结/润色/翻译)
前端·vue.js·人工智能
滑水滑成滑头13 分钟前
**发散创新:多智能体系统的探索与实践**随着人工智能技术的飞速发展,多智能体系统作为当今研究的热点领域,正受到越来越多关注
java·网络·人工智能·python
云布道师18 分钟前
阿里云 OSS MetaQuery 全面升级——新增内容和语义的检索能力,助力 AI 应用快速落地
人工智能·阿里云·云计算
m0_6501082431 分钟前
【论文精读】FlowVid:驯服不完美的光流,实现一致的视频到视频合成
人工智能·计算机视觉·扩散模型·视频编辑·视频生成·论文精读·不完美光流
radient43 分钟前
属于Agent的课本 - RAG
人工智能·后端·程序员
第七序章44 分钟前
【C + +】红黑树:全面剖析与深度学习
c语言·开发语言·数据结构·c++·人工智能
渡我白衣1 小时前
未来的 AI 操作系统(三)——智能的中枢:从模型到系统的统一
人工智能·深度学习·ui·语言模型·人机交互
Blossom.1181 小时前
把 AI“缝”进布里:生成式编织神经网络让布料自带摄像头
人工智能·python·单片机·深度学习·神经网络·目标检测·机器学习
曾经的三心草1 小时前
深度学习1-简介-简单实现-手写数字识别
人工智能·深度学习