【CV】opencv特征匹配算法

发布于:2024-04-24 ⋅ 阅读:(29) ⋅ 点赞:(0)

特征匹配是计算机视觉领域中的一项关键任务,它用于在不同图像中寻找相似的特征点,并将它们进行匹配。这些特征点可以是图像中的角点、边缘、斑点等,在不同的图像中可能因为旋转、缩放、光照变化等因素发生变化。

在OpenCV中,提供了多种特征匹配算法,其中包括ORB、SIFT、SURF、KAZE、AKAZE等。接下来,简要介绍这些算法,主要给出OpenCV示例。这里挖个坑,每个算法原理后续再填

ORB (Oriented FAST and Rotated BRIEF)

ORB是一种高效的特征提取和描述符算法,它结合了FAST关键点检测器和BRIEF描述符算法。ORB算法具有良好的旋转不变性和尺度不变性,并且计算速度较快,适用于实时应用场景。

#include <opencv2/opencv.hpp>

using namespace cv;

int main() {
    Mat img1 = imread("image1.jpg", IMREAD_GRAYSCALE);
    Mat img2 = imread("image2.jpg", IMREAD_GRAYSCALE);

    Ptr<ORB> orb = ORB::create();
    vector<KeyPoint> keypoints1, keypoints2;
    Mat descriptors1, descriptors2;

    orb->detectAndCompute(img1, Mat(), keypoints1, descriptors1);
    orb->detectAndCompute(img2, Mat(), keypoints2, descriptors2);

    // 进行特征点匹配
    BFMatcher matcher(NORM_HAMMING);
    vector<DMatch> matches;
    matcher.match(descriptors1, descriptors2, matches);

    // 绘制匹配结果
    Mat img_matches;
    drawMatches(img1, keypoints1, img2, keypoints2, matches, img_matches);
    
    imshow("Matches", img_matches);
    waitKey(0);

    return 0;
}

SIFT (Scale-Invariant Feature Transform)

SIFT是一种基于尺度空间的特征提取和描述符算法,具有良好的旋转和尺度不变性,但计算速度较慢。它是一种经典的特征匹配算法,在许多应用中仍然被广泛使用。

#include <opencv2/opencv.hpp>

using namespace cv;

int main() {
    Mat img1 = imread("image1.jpg", IMREAD_GRAYSCALE);
    Mat img2 = imread("image2.jpg", IMREAD_GRAYSCALE);

    Ptr<SIFT> sift = SIFT::create();
    vector<KeyPoint> keypoints1, keypoints2;
    Mat descriptors1, descriptors2;

    sift->detectAndCompute(img1, Mat(), keypoints1, descriptors1);
    sift->detectAndCompute(img2, Mat(), keypoints2, descriptors2);

    // 进行特征点匹配
    BFMatcher matcher;
    vector<DMatch> matches;
    matcher.match(descriptors1, descriptors2, matches);

    // 绘制匹配结果
    Mat img_matches;
    drawMatches(img1, keypoints1, img2, keypoints2, matches, img_matches);
    
    imshow("Matches", img_matches);
    waitKey(0);

    return 0;
}

SURF (Speeded-Up Robust Features)
SURF是一种基于快速Hessian矩阵检测的特征提取算法,它具有比SIFT更快的计算速度,但牺牲了一些旋转不变性。SURF适用于对速度要求较高的应用场景。

#include <opencv2/opencv.hpp>

using namespace cv;

int main() {
    Mat img1 = imread("image1.jpg", IMREAD_GRAYSCALE);
    Mat img2 = imread("image2.jpg", IMREAD_GRAYSCALE);

    Ptr<SURF> surf = SURF::create();
    vector<KeyPoint> keypoints1, keypoints2;
    Mat descriptors1, descriptors2;

    surf->detectAndCompute(img1, Mat(), keypoints1, descriptors1);
    surf->detectAndCompute(img2, Mat(), keypoints2, descriptors2);

    // 进行特征点匹配
    BFMatcher matcher;
    vector<DMatch> matches;
    matcher.match(descriptors1, descriptors2, matches);

    // 绘制匹配结果
    Mat img_matches;
    drawMatches(img1, keypoints1, img2, keypoints2, matches, img_matches);
    
    imshow("Matches", img_matches);
    waitKey(0);

    return 0;
}

KAZE (Accelerated-KAZE)

KAZE是一种快速的特征提取算法,它在保持较好旋转和尺度不变性的同时,具有更快的计算速度。KAZE适用于对计算资源要求较高的应用场景。

#include <opencv2/opencv.hpp>

using namespace cv;

int main() {
    Mat img1 = imread("image1.jpg", IMREAD_GRAYSCALE);
    Mat img2 = imread("image2.jpg", IMREAD_GRAYSCALE);

    Ptr<KAZE> kaze = KAZE::create();
    vector<KeyPoint> keypoints1, keypoints2;
    Mat descriptors1, descriptors2;

    kaze->detectAndCompute(img1, Mat(), keypoints1, descriptors1);
    kaze->detectAndCompute(img2, Mat(), keypoints2, descriptors2);

    // 进行特征点匹配
    BFMatcher matcher(NORM_L2);
    vector<DMatch> matches;
    matcher.match(descriptors1, descriptors2, matches);

    // 绘制匹配结果
    Mat img_matches;
    drawMatches(img1, keypoints1, img2, keypoints2, matches, img_matches);
    
    imshow("Matches", img_matches);
    waitKey(0);

    return 0;
}

AKAZE (Accelerated-KAZE)

AKAZE是KAZE的改进版本,它在保持计算速度的同时,进一步提升了匹配的性能。AKAZE适用于对性能要求较高的实时应用场景。

#include <opencv2/opencv.hpp>

using namespace cv;

int main() {
    // 读取两张图像
    Mat image1 = imread("image1.jpg");
    Mat image2 = imread("image2.jpg");

    // 检查图像是否成功读取
    if (image1.empty() || image2.empty()) {
        std::cerr << "Error: Unable to load images." << std::endl;
        return -1;
    }

    // 创建 AKAZE 特征检测器
    Ptr<AKAZE> akaze = AKAZE::create();

    // 检测特征点和描述符
    std::vector<KeyPoint> keypoints1, keypoints2;
    Mat descriptors1, descriptors2;
    akaze->detectAndCompute(image1, noArray(), keypoints1, descriptors1);
    akaze->detectAndCompute(image2, noArray(), keypoints2, descriptors2);

    // 创建 BFMatcher
    BFMatcher matcher(NORM_HAMMING);

    // 在第一张图像中的每个特征点上寻找最佳匹配
    std::vector<DMatch> matches;
    matcher.match(descriptors1, descriptors2, matches);

    // 绘制匹配结果
    Mat img_matches;
    drawMatches(image1, keypoints1, image2, keypoints2, matches, img_matches);

    // 显示匹配结果
    imshow("Matches", img_matches);
    waitKey(0);

    return 0;
}

python 调用opencv 方法类似