OpenCV图像拼接(3)图像拼接的类cv::detail::BestOf2NearestMatcher

  • 操作系统:ubuntu22.04
  • OpenCV版本:OpenCV4.9
  • IDE:Visual Studio Code
  • 编程语言:C++11

算法描述

特征匹配器,为每个特征找到两个最佳匹配,并且仅在描述符距离之间的比率大于阈值 match_conf 时保留最佳的一个。

cv::detail::BestOf2NearestMatcher 是 OpenCV 库中用于图像拼接的一个类,特别适用于全景图生成过程中特征匹配的环节。它通过寻找最佳的两个最近邻匹配来提高匹配准确性,从而确定不同图像之间的对应关系。下面是对这个类的一些基本介绍和使用说明。

主要功能

  • 两近邻匹配:该类实现了基于最佳两近邻距离比测试的方法来进行特征匹配。这种方法假设正确的匹配通常是第一个最邻近且显著不同于第二个最邻近的点。

常用成员函数

  • match():执行特征点描述符之间的匹配。注意,这个方法在较新的OpenCV版本中可能是保护成员,因此需要通过实例化对象后直接调用(例如使用 operator())或者继承此类来访问。
  • collectGarbage():清理内部存储以释放内存。

代码示例

cpp 复制代码
#include <opencv2/opencv.hpp>
#include <opencv2/stitching/detail/matchers.hpp>

using namespace cv;
using namespace cv::detail;

void drawMatchingResult(const Mat& img1, const Mat& img2, const std::vector<KeyPoint>& keypoints1,
                        const std::vector<KeyPoint>& keypoints2, const std::vector<DMatch>& matches)
{
    // 创建一个输出图像来展示两张图片和它们之间的匹配
    Mat outputImg;
    drawMatches(img1, keypoints1, img2, keypoints2, matches, outputImg,
                Scalar::all(-1), Scalar::all(-1), std::vector<char>(),
                DrawMatchesFlags::NOT_DRAW_SINGLE_POINTS);

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

int main()
{
    // 读取两张待匹配的图片
    Mat img1 = imread("/media/dingxin/data/study/OpenCV/sources/images/stich1.png");
    Mat img2 = imread("/media/dingxin/data/study/OpenCV/sources/images/stich2.png");

    if (img1.empty() || img2.empty())
    {
        std::cerr << "Could not open or find the images!" << std::endl;
        return -1;
    }

    // 初始化ORB特征检测器
    Ptr<Feature2D> feature_detector = ORB::create();

    // 检测特征点并计算描述符
    std::vector<KeyPoint> keypoints1, keypoints2;
    Mat descriptors1, descriptors2;
    feature_detector->detectAndCompute(img1, noArray(), keypoints1, descriptors1);
    feature_detector->detectAndCompute(img2, noArray(), keypoints2, descriptors2);

    // 创建ImageFeatures对象并将特征点和描述符填入
    ImageFeatures features1, features2;
    features1.img_idx = 0; // 图片索引
    features1.keypoints = keypoints1;
    features1.descriptors = descriptors1.getUMat(ACCESS_READ); // 使用getUMat方法转换

    features2.img_idx = 1; // 图片索引
    features2.keypoints = keypoints2;
    features2.descriptors = descriptors2.getUMat(ACCESS_READ); // 使用getUMat方法转换

    // 创建BestOf2NearestMatcher实例
    Ptr<BestOf2NearestMatcher> matcher = BestOf2NearestMatcher::create();

    // 使用()运算符来进行匹配
    MatchesInfo matches_info;
    (*matcher)(features1, features2, matches_info);

    // 输出匹配结果的数量
    std::cout << "Found " << matches_info.matches.size() << " matches" << std::endl;

    // 将MatchesInfo中的matches转换为std::vector<DMatch>类型,以便于绘制
    std::vector<DMatch> matches_vector(matches_info.matches.begin(), matches_info.matches.end());

    // 绘制匹配结果
    drawMatchingResult(img1, img2, keypoints1, keypoints2, matches_vector);

    return 0;
}

运行结果

相关推荐
Raink老师1 分钟前
【AI面试临阵磨枪-29】什么是 Function Calling?与手动解析 LLM 输出的区别?
人工智能·ai 面试
ai大模型中转api测评2 分钟前
构建生产级 AI 应用:GPT-5.5 与 Claude 4.7 的 Token 成本管理与工程化实战
大数据·人工智能·gpt·自动化
wxl7812273 分钟前
Hermes+Qwen3.6-35B本地离线全链路全自动开发React项目,完成cognee-ui从零开发+自动测试+自动修Bug闭环
人工智能·经验分享·自我提升·hermes agent
jkyy20144 分钟前
数智赋能药品零售:从卖药到健康服务,重构慢病管理新生态
人工智能·重构·健康医疗·零售
DO_Community6 分钟前
DigitalOcean 打造 AI 原生云,帮助 AI 应用大幅降低成本与运维复杂度
运维·人工智能·agent·claude
汽车仪器仪表相关领域7 分钟前
Kvaser Memorator R SemiPro:双通道CAN总线记录仪,汽车与工业测试的高性价比之选
大数据·网络·人工智能·功能测试·汽车·安全性测试
天天爱吃肉82188 分钟前
空间智能上车:新能源OEM决胜「第三空间」的底层技术革命|研发工程师深度解析
大数据·人工智能·嵌入式硬件·汽车
初圣魔门首席弟子8 分钟前
深度学习 欠拟合、过拟合讲透
人工智能
开开心心就好9 分钟前
支持批量添加水印的实用工具推荐
人工智能·游戏·ci/cd·docker·音视频·语音识别·媒体
毕胜客源码10 分钟前
卷积神经网络的手势识别系统(有技术文档)深度学习 图像识别 卷积神经网络 Django python 人工智能
人工智能·python·深度学习·cnn·django