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

相关推荐
a18792721831几秒前
【算法】动态规划第三篇:背包第一课——贪心之死与正序倒序开关
算法·leetcode·动态规划·贪心·dp·暴力·算法讲解
黄金龙PLUS10 分钟前
Xoodoo置换算法的优缺点
算法·网络安全·密码学·哈希算法·同态加密
clorinda19 分钟前
OpenCV 学习实践:从人脸检测到人脸识别
人工智能·opencv·学习
YOLO数据集集合30 分钟前
无花果目标检测数据集 |无花果检测 智慧农业 果实识别 目标检测 YOLO格式 深度学习数据集 计算机视觉9049期
yolo·目标检测·计算机视觉·农业·水果识别·无花果识别·无花果检测
YOLO数据集集合37 分钟前
遥感影像树木检测数据集 | 遥感树木 目标检测 城市绿化 森林监测 YOLO格式9052期
人工智能·yolo·目标检测·计算机视觉·目标跟踪·树木检测·遥感树影
LayZhangStrive42 分钟前
数据结构与算法 - 堆排序
java·数据结构·算法·排序算法·堆排序·大根堆
AndrewHZ1 小时前
图像处理入门027 | 查找表(LUT)加速——把像素级操作从“每次算一遍“变成“只算 256 次“
图像处理·opencv·numpy·滤镜·查找表·lut
residual_fan1 小时前
深度渐进收缩学习(DPSL):面向强噪声与类内分散的机械复合故障诊断方法
人工智能·算法·数据挖掘·数据分析
陈年老古董1 小时前
OpenCV 人脸识别学习笔记:从Haar检测到三种特征脸算法实战
笔记·opencv·学习·人脸识别
a187927218312 小时前
【算法】动态规划第五篇:区间 DP——从“看什么都像背包“到“最后戳谁“
算法·leetcode·动态规划·dp·区间·区间dp·算法讲解