【问题标题】:OpenCV draw non-matching pointsOpenCV 绘制非匹配点
【发布时间】:2017-05-04 06:52:39
【问题描述】:

我已关注OpenCV Feature Detection and Description tutorial,并在 OpenCV 中使用 SIFT 和其他算法来查找 2 张图像之间的匹配特征点。据我了解,这些算法可以找到 2 张图像之间的相似区域。但我有兴趣识别不同或不相似的区域。

如何在两张图像上绘制所有不匹配的特征点?此外,我可以在这些不匹配点周围绘制边界,以便能够显示 2 张图像中的哪些区域不同?

我在 Windows 7 上使用 Python 代码并从最新的 OpenCV 源代码构建。

【问题讨论】:

  • 你能提供一些你的代码吗(特别是你为两个图像生成关键点和描述符的部分以及你在哪里执行match)?
  • 我可以解决它..我在下面发布了我的答案
  • 你的问题的第二部分还需要一些提示吗?
  • @ElouarnLaine 是的 plzz..第二部分需要帮助
  • @ElouarnLaine 谢谢...我有一个后续问题here..看看你有什么建议

标签: python opencv sift


【解决方案1】:
  1. 在两张图片上绘制所有不匹配的特征点:

一旦您知道由两个描述符 (matches = bf.match(des1,des2)) 的 匹配 产生的 Matcher objects 的结构,这项任务就非常简单了。与此问题相关的两个 Matcher 对象的属性如下:

  • DMatch.trainIdx:列车描述符中的描述符索引(或来自列车图像的关键点
  • DMatch.queryIdx:查询描述符中的描述符索引(或查询图像中的关键点

然后,知道这些信息,正如@uzair_syed 所说,这只是simple list operations task


  1. 围绕不匹配点画边界:

要实现这一点,我会这样做:

  • 为每个不匹配的点创建一个带有白色像素的黑色蒙版
  • 根据非匹配点簇的密度扩张具有大内核(即 15 x 15 像素)的遮罩。
  • 腐蚀具有相同内核大小的掩码。
  • 最后,在掩码上应用findContours 函数来获取不匹配点的边界。

更多信息,你可以看看这个question and its answer

希望它能让你走上正轨!

【讨论】:

    【解决方案2】:

    原来是简单的列表操作任务。这是我的 Python 代码

    # code copied from
    # http://docs.opencv.org/3.0-beta/doc/py_tutorials/py_feature2d/py_feature_homography/py_feature_homography.html
    
    import numpy as np
    import cv2
    from matplotlib import pyplot as plt
    from scipy.spatial.distance import euclidean
    
    MIN_MATCH_COUNT = 10
    
    img1 = cv2.imread('Src.png',0)  # queryImage
    img2 = cv2.imread('Dest.png',0) # trainImage
    
    # Initiate SIFT detector
    sift = cv2.xfeatures2d.SIFT_create()
    
    # find the keypoints and descriptors with SIFT
    kp1, des1 = sift.detectAndCompute(img1,None)
    kp2, des2 = sift.detectAndCompute(img2,None)
    
    FLANN_INDEX_KDTREE = 0
    index_params = dict(algorithm = FLANN_INDEX_KDTREE, trees = 5)
    search_params = dict(checks = 50)
    
    flann = cv2.FlannBasedMatcher(index_params, search_params)
    
    matches = flann.knnMatch(des1,des2,k=2)
    
    # store all the good matches as per Lowe's ratio test.
    good = []
    for m,n in matches:
        if m.distance < 0.7*n.distance:
            good.append(m)
    
    if len(good)>MIN_MATCH_COUNT:
        src_pts = np.float32([ kp1[m.queryIdx].pt for m in good ]).reshape(-1,1,2)
        dst_pts = np.float32([ kp2[m.trainIdx].pt for m in good ]).reshape(-1,1,2)
    
        kp1_matched=([ kp1[m.queryIdx] for m in good ])
        kp2_matched=([ kp2[m.trainIdx] for m in good ])
    
        kp1_miss_matched=[kp for kp in kp1 if kp not in kp1_matched]
        kp2_miss_matched=[kp for kp in kp2 if kp not in kp2_matched]
    
        # draw only miss matched or not matched keypoints location
        img1_miss_matched_kp = cv2.drawKeypoints(img1,kp1_miss_matched, None,color=(255,0,0), flags=0)
        plt.imshow(img1_miss_matched_kp),plt.show()
    
        img2_miss_matched_kp = cv2.drawKeypoints(img2,kp2_miss_matched, None,color=(255,0,0), flags=0)
        plt.imshow(img2_miss_matched_kp),plt.show()
    
        M, mask = cv2.findHomography(src_pts, dst_pts, cv2.RANSAC,5.0)
        matchesMask = mask.ravel().tolist()
    
        h,w = img1.shape
        pts = np.float32([ [0,0],[0,h-1],[w-1,h-1],[w-1,0] ]).reshape(-1,1,2)
        dst = cv2.perspectiveTransform(pts,M)
    
    else:
        print "Not enough matches are found - %d/%d" % (len(good),MIN_MATCH_COUNT)
        matchesMask = None
    

    【讨论】:

      猜你喜欢
      • 2016-10-26
      • 2017-08-28
      • 2013-03-25
      • 2013-12-19
      • 2021-10-09
      • 2015-05-26
      • 2019-02-01
      • 1970-01-01
      • 2012-10-11
      相关资源
      最近更新 更多