【CV】opencv特征匹配算法

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

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

ORB (Oriented FAST and Rotated BRIEF)

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

cpp 复制代码
#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是一种基于尺度空间的特征提取和描述符算法,具有良好的旋转和尺度不变性,但计算速度较慢。它是一种经典的特征匹配算法,在许多应用中仍然被广泛使用。

cpp 复制代码
#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适用于对速度要求较高的应用场景。

cpp 复制代码
#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适用于对计算资源要求较高的应用场景。

cpp 复制代码
#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适用于对性能要求较高的实时应用场景。

cpp 复制代码
#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 方法类似

相关推荐
XFF不秃头40 分钟前
力扣刷题笔记-旋转图像
c++·笔记·算法·leetcode
Coding茶水间40 分钟前
基于深度学习的学生上课行为检测系统演示与介绍(YOLOv12/v11/v8/v5模型+Pyqt5界面+训练代码+数据集)
图像处理·人工智能·深度学习·yolo·目标检测·机器学习·计算机视觉
王老师青少年编程1 小时前
csp信奥赛C++标准模板库STL案例应用3
c++·算法·stl·csp·信奥赛·lower_bound·标准模版库
有为少年2 小时前
Welford 算法 | 优雅地计算海量数据的均值与方差
人工智能·深度学习·神经网络·学习·算法·机器学习·均值算法
Ven%2 小时前
从单轮问答到连贯对话:RAG多轮对话技术详解
人工智能·python·深度学习·神经网络·算法
山楂树の2 小时前
爬楼梯(动态规划)
算法·动态规划
谈笑也风生2 小时前
经典算法题型之复数乘法(二)
开发语言·python·算法
智算菩萨2 小时前
强化学习从单代理到多代理系统的理论与算法架构综述
人工智能·算法·强化学习
lhn2 小时前
大模型强化学习总结
算法
AI小怪兽2 小时前
YOLO11-4K:面向4K全景图像实时小目标检测的高效架构
人工智能·目标检测·计算机视觉·目标跟踪·架构