• OpenCV Python – 使用SIFT算法实现两张图片的特征匹配


    OpenCV Python – 使用SIFT算法实现两张图片的特征匹配

    1.要实现在大图中找到任意旋转、缩放等情况下的小图位置,可以使用特征匹配算法,如 SIFT (尺度不变特征变换) 或 SURF (加速稳健特征)。这些算法可以在不同尺度和旋转情况下寻找匹配的特征点

    import cv2
    import numpy as np
    
    def find_template(template_path, image_path):
        # 加载图像
        template = cv2.imread(template_path, 0)
        image = cv2.imread(image_path, 0)
    
        # 初始化 SIFT 探测器
        sift = cv2.xfeatures2d.SIFT_create()
    
        # 在模板和大图中检测特征点和特征描述符
        keypoints1, descriptors1 = sift.detectAndCompute(template, None)
        keypoints2, descriptors2 = sift.detectAndCompute(image, None)
    
        # 初始化暴力匹配器
        matcher = cv2.DescriptorMatcher_create(cv2.DescriptorMatcher_BRUTEFORCE)
    
        # 寻找最佳匹配
        matches = matcher.match(descriptors1, descriptors2)
    
        # 根据匹配度排序
        matches = sorted(matches, key=lambda x: x.distance)
    
        # 提取匹配结果
        num_good_matches = int(len(matches) * 0.15)  # 根据匹配结果数自行调整,这里取前 15% 的匹配结果
        good_matches = matches[:num_good_matches]
    
        # 提取匹配结果的对应关系
        src_pts = np.float32([keypoints1[m.queryIdx].pt for m in good_matches]).reshape(-1, 1, 2)
        dst_pts = np.float32([keypoints2[m.trainIdx].pt for m in good_matches]).reshape(-1, 1, 2)
    
        # 计算透视变换矩阵
        M, mask = cv2.findHomography(src_pts, dst_pts, cv2.RANSAC, 5.0)
    
        # 获取模板图像的宽高
        h, w = template.shape
    
        # 在大图中查找模板位置
        matches_mask = mask.ravel().tolist()
        if sum(matches_mask) > 10:
            pts = np.float32([[0, 0], [0, h - 1], [w - 1, h - 1], [w - 1, 0]]).reshape(-1, 1, 2)
            dst = cv2.perspectiveTransform(pts, M)
            return dst.reshape(4, 2)
        else:
            return None
    
    # 示例用法
    template_path = 'path_to_template_image.png'
    image_path = 'path_to_large_image.png'
    result = find_template(template_path, image_path)
    
    if result is not None:
        print("找到了模板图像的位置:")
        for pt in result:
            print("坐标:", pt)
    else:
        print("未找到模板图像")
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58

    2.我们使用了 SIFT 算法检测和匹配特征点,然后使用 RANSAC 算法计算透视变换矩阵,从而得到模板图像在大图中的位置。根据你的需求,你可以根据实际情况调整代码中的阈值以及匹配结果的筛选条件。

    请注意,使用 SIFT 算法需要安装额外的 OpenCV 扩展库,可以通过 pip 安装:pip install opencv-contrib-python。如果你使用的是不带 SIFT 的 OpenCV 版本,你可以尝试 SURF 算法,或者使用其他特征提取和匹配算法来适应不同的图像变换情况。

  • 相关阅读:
    postman接口测试
    如何使用组件
    微信扫码授权登录手游(你使用的浏览器暂不支持微信登录)
    TPC-C 、TPC-H、TPC-DS和SSB测试基准(Benchmark)介绍
    Spring Cloud Alibaba微服务第7章之负载均衡Ribbon
    手把手教你如何使用YOLOV5训练自己的数据集
    开源项目有哪些机遇与挑战?
    MultiBank Group宣布在阿联酋和新加坡取得两项新牌照
    1018 锤子剪刀布
    什么是容器
  • 原文地址:https://blog.csdn.net/huage926/article/details/133707020