OpenCV特征匹配总结

发布于:2024-05-13 ⋅ 阅读:(68) ⋅ 点赞:(0)

1.概述

在深度学习出现之前,图像中的特征匹配方法主要有

2.理论对比

3.代码实现

#include <iostream>
#include <opencv2/opencv.hpp>

int main(int argc, char** argv) {
    if(argc != 3) {
        std::cerr << "Usage: " << argv[0] << " <image1_path> <image2_path>" << std::endl;
        return -1;
    }

    // Load the images
    cv::Mat img1 = cv::imread(argv[1], cv::IMREAD_GRAYSCALE);
    cv::Mat img2 = cv::imread(argv[2], cv::IMREAD_GRAYSCALE);
    if(img1.empty() || img2.empty()) {
        std::cerr << "Error: Couldn't read one or both images. Check the paths and try again." << std::endl;
        return -1;
    }

    // Detect ORB keypoints and descriptors
    cv::Ptr<cv::ORB> orb = cv::ORB::create();
    std::vector<cv::KeyPoint> keypoints1, keypoints2;
    cv::Mat descriptors1, descriptors2;
    orb->detectAndCompute(img1, cv::noArray(), keypoints1, descriptors1);
    orb->detectAndCompute(img2, cv::noArray(), keypoints2, descriptors2);

    // Use BFMatcher to match the ORB descriptors
    cv::BFMatcher matcher(cv::NORM_HAMMING);
    std::vector<cv::DMatch> matches;
    matcher.match(descriptors1, descriptors2, matches);

    // Draw the matches
    cv::Mat imgMatches;
    cv::drawMatches(img1, keypoints1, img2, keypoints2, matches, imgMatches);
    cv::imshow("ORB Feature Matches", imgMatches);

    // Wait for a key press and then close
    cv::waitKey(0);

    return 0;
}