在 OpenCV 中计算特征匹配 (BFMatcher) 中的相似性度量

Compute similarity measure in feature matching (BFMatcher) in OpenCV

我正在比较图像,我已经使用 BFMatcher 进行特征匹配

我的实际代码是:

def get_similarity_from_desc(approach, query_desc, corp_desc):
    if approach == 'sift':
        # BFMatcher with euclidean distance
        bf = cv.BFMatcher()
    else:
        # BFMatcher with hamming distance
        bf = cv.BFMatcher(cv.NORM_HAMMING)
    
    matches = bf.knnMatch(query_desc,corp_desc,k=2)
    # Apply ratio test
    good = []
    for m,n in matches:
        if m.distance < 0.75*n.distance:
            good.append([m])
    similarity = ??
    return similarity

我想知道是否有可能在给定良好匹配列表 good 和两个图像的描述符 query_desc 的情况下计算 相似性度量 corp_desc

此刻我想到了:

similarity = len(good) / len(matches)

但我认为这不是确定两个图像之间相似性的正确方法

您知道计算此度量的更好方法吗?

我终于做到了,看起来效果不错:

def get_similarity_from_desc(approach, search_desc, idx_desc):
    if approach == 'sift' or approach == 'orb_sift':
        # BFMatcher with euclidean distance
        bf = cv.BFMatcher()
    else:
        # BFMatcher with hamming distance
        bf = cv.BFMatcher(cv.NORM_HAMMING)
    matches = bf.match(search_desc, idx_desc)
    # Distances between search and index features that match
    distances = [m.distance for m in matches]
    # Distance between search and index images
    distance = sum(distances) / len(distances)
    # If distance == 0 -> similarity = 1
    similarity = 1 / (1 + distance)
    return similarity

来源:https://linuxtut.com/en/c9497ffb5240622ede01/