【发布时间】:2022-06-18 03:48:34
【问题描述】:
我正在尝试使用 open cv 匹配关键点。 具体来说,我使用的是“sift”检测器和“flann”匹配器。我的代码基于cv2's documentation:
detector = cv2.SIFT_create()
matcher = cv2.FlannBasedMatcher(indexParams=dict(algorithm=0, trees=5), searchParams=dict(checks=50))
kps1, desc1 = detector.detectAndCompute(img1, None)
kps2, desc2 = detector.detectAndCompute(img2, None)
all_matches = matcher.knnMatch(desc1, desc2, 2)
ratio = 0.7
good_matches = []
for m, n in all_matches:
if m.distance <= ratio * n.distance:
good_matches.append(m)
我注意到,即使在 good_matches 列表中,我也有一些关键点有多个匹配项:
extra_matches = dict()
for match in good_matches:
t_idx = match.trainIdx
reps = [mch for mch in good_matches if mch.trainIdx == t_idx]
if len(reps) > 1 and t_idx not in extra_matches.dict():
extra_matches[t_idx] = reps
print(len(extra_matches)) # not 0
我觉得这很奇怪,因为我认为 knnMatch 已经产生了 2 个最佳匹配项。为什么在对匹配进行比率修剪后,每个关键点会有多个匹配?
【问题讨论】: