import cv2

#
def match_snn(desc1, desc2, snn_th = 0.8):
    index_params = dict(algorithm=1, trees=4)
    search_params = dict(checks=128)  # or pass empty dictionary
    matcher = cv2.FlannBasedMatcher(index_params, search_params)
    matches = matcher.knnMatch(desc1.astype(np.float32), desc2.astype(np.float32), k=2)
    
    #ratio test
    good_matches = []
    for m,n in matches:
        if m.distance < snn_th * n.distance:
            good_matches.append(m)
    return good_matches

取图像1中的某个关键点,并找出其与图像2中距离最近的前两个关键点,在这两个关键点中,若最近的距离除以次近的距离小于某个阈值,则接受这一对匹配点.

由于特征空间的高维性,相近的距离可能有大量的错误匹配,从而它的ratio值比较高. Lowe推荐ratio的取值为0.8,但通过对大量存在尺度、旋转和亮度变化的两幅图片进行匹配,实验结果表明ratio取值在0. 4~0. 6为最佳,小于0. 4会造成非常少的匹配点,大于0. 6会造成大量的错误匹配点.

Last modification:March 1st, 2021 at 11:22 am