【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 方法类似

相关推荐
程序员Xu1 小时前
【LeetCode热题100道笔记】二叉树的右视图
笔记·算法·leetcode
笑脸惹桃花1 小时前
50系显卡训练深度学习YOLO等算法报错的解决方法
深度学习·算法·yolo·torch·cuda
阿维的博客日记2 小时前
LeetCode 48 - 旋转图像算法详解(全网最优雅的Java算法
算法·leetcode
GEO_YScsn2 小时前
Rust 的生命周期与借用检查:安全性深度保障的基石
网络·算法
程序员Xu2 小时前
【LeetCode热题100道笔记】二叉搜索树中第 K 小的元素
笔记·算法·leetcode
youcans_2 小时前
【医学影像 AI】YoloCurvSeg:仅需标注一个带噪骨架即可实现血管状曲线结构分割
人工智能·yolo·计算机视觉·分割·医学影像
THMAIL3 小时前
机器学习从入门到精通 - 数据预处理实战秘籍:清洗、转换与特征工程入门
人工智能·python·算法·机器学习·数据挖掘·逻辑回归
Kevinhbr4 小时前
CSP-J/S IS COMING
数据结构·c++·算法
蕓晨4 小时前
set的插入和pair的用法
c++·算法
THMAIL4 小时前
深度学习从入门到精通 - AutoML与神经网络搜索(NAS):自动化模型设计未来
人工智能·python·深度学习·神经网络·算法·机器学习·逻辑回归