代码之家  ›  专栏  ›  技术社区  ›  Simplicity

以MSER作为关键点检测器,SIFT作为描述器

  •  0
  • Simplicity  · 技术社区  · 7 年前

    import numpy as np
    import cv2
    from matplotlib import pyplot as plt
    
    img1 = cv2.imread('1.jpg')
    img2 = cv2.imread('2.jpg') 
    
    mser = cv2.MSER_create()
    
    kp1 = mser.detect(img1)
    kp2 = mser.detect(img2)
    
    sift = cv2.xfeatures2d.SIFT_create()
    
    kp1, des1 = sift.detectAndCompute(img1, kp1)
    kp2, des2 = sift.detectAndCompute(img2, kp2)
    
    bf = cv2.BFMatcher()
    matches = bf.knnMatch(des1,des2, k=2)
    
    good = []
    for m, n in matches:
        if m.distance < 0.8 * n.distance:
            good.append(m)
    
    good = sorted(good, key=lambda x: x.distance)
    print len(good)
    
    matching_result = cv2.drawMatches(img1, kp1, img2, kp2, good, None, flags=2)
    cv2.imwrite('result.jpg', matching_result)
    

    但是,得到以下错误:

    kp1, des1 = sift.detectAndCompute(img1, kp1)
    TypeError: mask is not a numpy array, neither a scalar
    

    如何解决此问题?我使用探测器和描述符的方式对吗?

    1 回复  |  直到 7 年前
        1
  •  0
  •   Howard GENG    7 年前

    您可以基于现有的关键点来计算描述符。一个快速解决方案是从代码中更改以下行:

    kp1, des1 = sift.detectAndCompute(img1, kp1)
    kp2, des2 = sift.detectAndCompute(img2, kp2)
    

    相反,使用compute()函数:

    kp1, des1 = sift.compute(img1, kp1)
    kp2, des2 = sift.compute(img2, kp2)