计算机视觉攻略 笔记11 (用二值描述子匹配关键点)

用二值描述子匹配关键点

为了减少内存使用、降低计算量,人们引入了将一组比特位(0 和 1)组合成二值描述子的概念。

示例程序

#include <iostream>
#include <algorithm>
#include <vector>
#include <opencv2/core/core.hpp>
#include </home/jlm/3rdparty/opencv/opencv_contrib/modules/xfeatures2d/include/opencv2/xfeatures2d.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>

using namespace std;

int main(int argc, char** argv) {
    
    
    if(argc != 3)
    {
    
    
        cerr << "don't get the right numbers of image" << endl;
        return -1;
    }
    cv::Mat image1 = cv::imread(argv[1],cv::IMREAD_GRAYSCALE);
    cv::Mat image2 = cv::imread(argv[2],cv::IMREAD_GRAYSCALE);
    if(image1.empty() || image2.empty())
    {
    
    
        cout << "don't get the data of the argv[1]" << endl;
        return -1;
    }
    cv::imshow("Original1", image1);
    cv::imshow("Original2", image2);
    cv::waitKey(0);
    // 定义关键点的容器
    vector<cv::KeyPoint> keypoints1;
    vector<cv::KeyPoint> keypoints2;

    // 提取描述子
    cv::Mat descriptors1;
    cv::Mat descriptors2;

    // 定义特征检测器
    //对于 SIFT,调用 cv::SIFT::create 函数即可
    cv::Ptr<cv::Feature2D> ptrFeature2D =
            cv::ORB::create(60); // 大约60个特征点
    // 检测并描述关键点
    // 检测ORB特征
    ptrFeature2D -> detectAndCompute(image1, cv::noArray(),
                                     keypoints1, descriptors1);
    ptrFeature2D -> detectAndCompute(image2, cv::noArray(),
                                     keypoints2, descriptors2);

//    ptrFeature2D -> detect(image1, keypoints1);
//    ptrFeature2D -> detect(image2, keypoints2);
//
//    ptrFeature2D -> compute(image1, keypoints1, descriptors1);
//    ptrFeature2D -> compute(image2, keypoints2, descriptors2);
    // 构建匹配器
    cv::BFMatcher matcher(cv::NORM_HAMMING);    // 二值描述子一律使用Hamming规范
    // 匹配两幅图像的描述子
    vector<cv::DMatch> matches;
    matcher.match(descriptors1, descriptors2, matches);

    // 输出结果
    cv::Mat outImage;
    cv::drawMatches(image1, keypoints1,
                    image2, keypoints2,
                    matches,
                    outImage,
                    cv::Scalar(255, 255, 255),
                    cv::Scalar(255, 255, 255));
    cv::imshow("OUT IMAGE", outImage);
    cout << "the keypoints number of image1 : " << keypoints1.size() << endl;
    cout << "the keypoints number of image2 : " << keypoints2.size() << endl;
    cout << "the number of newMatches : " << matches.size() << endl;
    cv::waitKey(0);
    return 0;
}

猜你喜欢

转载自blog.csdn.net/jlm7689235/article/details/108093113