【问题标题】:How to use surf and sift detector in OpenCV for Python如何在 OpenCV for Python 中使用 surf 和 sift 检测器
【发布时间】:2016-02-13 16:55:40
【问题描述】:

我正在尝试使用函数 SURF() 进行特征匹配的代码。执行时会出现错误提示“AttributeError: 'module' object has no attribute 'SURF'”。

如何下​​载 Python (Windows) 的此模块并修复此错误?

【问题讨论】:

    标签: python opencv surf


    【解决方案1】:

    您可以在 open cv 中尝试 ORB(定向 FAST 和 Rotated Brief)作为 SURF 的替代品。它几乎和 SURF 和 SIFT 一样好用,而且它是 免费 不像 SIFTSURF专利 并且可以不得用于商业用途。
    您可以在 opencv-python 文档中了解更多信息 here

    这里是示例代码,方便您使用

    import cv2
    from matplotlib import pyplot as plt
    
    img1 = cv2.imread('text.png',cv2.COLOR_BGR2GRAY) # queryImage
    img2 = cv2.imread('original.png',cv2.COLOR_BGR2GRAY) # trainImage
    # Initiate SIFT detector
    orb = cv2.ORB_create()
    
    # find the keypoints and descriptors with SIFT
    kp1, des1 = orb.detectAndCompute(img1,None)
    kp2, des2 = orb.detectAndCompute(img2,None)
    # create BFMatcher object
    bf = cv2.BFMatcher(cv2.NORM_HAMMING, crossCheck=True)
    # Match descriptors.
    matches = bf.match(des1,des2)
    
    # Sort them in the order of their distance.
    matches = sorted(matches, key = lambda x:x.distance) 
    # Draw first 10 matches.
    img3 = cv2.drawMatches(img1,kp1,img2,kp2,matches[:10],None, flags=2)
    
    plt.imshow(img3),plt.show()
    

    【讨论】:

    • 它在 opencv 2.7 或 2.4 中有效吗?因为我在 ORB 上遇到了同样的错误。
    • 我在 open cv 3.0 中使用过
    • 与此同时,几年后,openCV 的天才们想出了另一个解决方案。 2 实际上,KAZE 和 AKAZE。值得一看。
    【解决方案2】:

    一开始

    pip install opencv-contrib-python
    

    然后使用这个 hack 来创建 sift 对象

    sift = cv2.xfeatures2d.SIFT_create()
    

    【讨论】:

    • 我不知道...我去测试一下!
    • SIFT 和 SURF 不再包含在 OpenCV Version >= 3 中。如果你想使用它们,你必须手动编译 OpenCV 并打开 contrib-modules 和 OPENCV_ENABLE_NONFREE CMake 标志。
    【解决方案3】:
    import numpy as np
    import cv2 as cv
    from matplotlib import pyplot as plt
    img = cv.imread('simple.jpg',0)
    # Initiate FAST object with default values
    fast = cv.FastFeatureDetector_create()
    # find and draw the keypoints
    kp = fast.detect(img,None)
    img2 = cv.drawKeypoints(img, kp, None, color=(255,0,0))
    # Print all default params
    print( "Threshold: {}".format(fast.getThreshold()) )
    print( "nonmaxSuppression:{}".format(fast.getNonmaxSuppression()) )
    print( "neighborhood: {}".format(fast.getType()) )
    print( "Total Keypoints with nonmaxSuppression: {}".format(len(kp)) )
    cv.imwrite('fast_true.png',img2)
    #Disable nonmaxSuppression
    fast.setNonmaxSuppression(0)
    kp = fast.detect(img,None)
    print( "Total Keypoints without nonmaxSuppression: {}".format(len(kp)) )
    img3 = cv.drawKeypoints(img, kp, None, color=(255,0,0))
    cv.imwrite('fast_false.png',img3)
    

    【讨论】:

      猜你喜欢
      • 2023-03-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-07-21
      • 1970-01-01
      • 2011-11-26
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多