OPENCV例子opencv-4.5.5\samples\gpu\surf_keypoint_matcher.cpp的代码分析

该程序演示了使用 SURF_CUDA 特征检测器、描述符提取器和 BruteForceMatcher_CUDA。

示例main函数调用情况如下:

 

示例main函数流程图情况如下:

 

示例main函数UML逻辑图情况如下:

 

示例源代码如下:

#include <iostream>

#include "opencv2/opencv_modules.hpp"

#include "opencv2/core.hpp"

#include "opencv2/features2d.hpp"

#include "opencv2/highgui.hpp"

#include "opencv2/cudafeatures2d.hpp"

#include "opencv2/xfeatures2d/cuda.hpp"

using namespace std;

using namespace cv;

using namespace cv::cuda;

static void help()

{

    cout << "\nThis program demonstrates using SURF_CUDA features detector, descriptor extractor and BruteForceMatcher_CUDA" << endl;

    cout << "\nUsage:\n\tsurf_keypoint_matcher --left <image1> --right <image2>" << endl;

}

int main(int argc, char* argv[])

{

    if (argc != 5)

    {

        help();

        return -1;

    }

    GpuMat img1, img2;

    for (int i = 1; i < argc; ++i)

    {

        if (string(argv[i]) == "--left")

        {

            img1.upload(imread(argv[++i], IMREAD_GRAYSCALE));

            CV_Assert(!img1.empty());

        }

        else if (string(argv[i]) == "--right")

        {

            img2.upload(imread(argv[++i], IMREAD_GRAYSCALE));

            CV_Assert(!img2.empty());

        }

        else if (string(argv[i]) == "--help")

        {

            help();

            return -1;

        }

    }

    cv::cuda::printShortCudaDeviceInfo(cv::cuda::getDevice());

    SURF_CUDA surf;

    // detecting keypoints & computing descriptors检测关键点和计算描述符

    GpuMat keypoints1GPU, keypoints2GPU;

    GpuMat descriptors1GPU, descriptors2GPU;

    surf(img1, GpuMat(), keypoints1GPU, descriptors1GPU);

    surf(img2, GpuMat(), keypoints2GPU, descriptors2GPU);

    cout << "FOUND " << keypoints1GPU.cols << " keypoints on first image" << endl;

    cout << "FOUND " << keypoints2GPU.cols << " keypoints on second image" << endl;

    // matching descriptors匹配描述符

    Ptr<cv::cuda::DescriptorMatcher> matcher = cv::cuda::DescriptorMatcher::createBFMatcher(surf.defaultNorm());

    vector<DMatch> matches;

    matcher->match(descriptors1GPU, descriptors2GPU, matches);

    // downloading results下载结果

    vector<KeyPoint> keypoints1, keypoints2;

    vector<float> descriptors1, descriptors2;

    surf.downloadKeypoints(keypoints1GPU, keypoints1);

    surf.downloadKeypoints(keypoints2GPU, keypoints2);

    surf.downloadDescriptors(descriptors1GPU, descriptors1);

    surf.downloadDescriptors(descriptors2GPU, descriptors2);

    // drawing the results绘制结果

    Mat img_matches;

    drawMatches(Mat(img1), keypoints1, Mat(img2), keypoints2, matches, img_matches);

    namedWindow("matches", 0);

    imshow("matches", img_matches);

    waitKey(0);

    return 0;

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

qqq9668

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值