knnMatch使用報錯“ValueError: not enough values to unpack (expected 2, got 0)”

# 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

參考:https://stackoverflow.com/questions/55612455/when-receiving-valueerror-not-enough-values-to-unpack-expected-2-got-1-ho

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章