# BFMatcher with default params
bf = cv2.BFMatcher()
matches = bf.knnMatch(des1,des2, k=2)
# Apply ratio test
good = []
for m,n in matches:
if m.distance < 0.75*n.distance:
good.append([m])
由于不是所有的des1都能找到2个匹配的des2,这里可能返回0或1个match,所以上面的例子是会报错的。可以作下面的修改:
for i, pair in enumerate(matches):
try:
m, n = pair
if m.distance < 0.7*n.distance:
good.append(m)
except ValueError:
pass