当我运行Flann KNN匹配器时,在某些情况下,KNN匹配器仅返回一个点,这是因为依赖于该点的匹配器后面的哪个代码失败:

flann = cv2.FlannBasedMatcher(index_params, search_params)

matches = flann.knnMatch(descriptors1, descriptors2, k=2)

# Retrieve good matches
good_matches = []

# ratio test as per Lowe's paper
for i, (m, n) in enumerate(matches):
    if m.distance < 0.7*n.distance:
        good_matches.append((m, n))

引发此错误:
Traceback (most recent call last):
  File "main.py", line 161, in <module>
    main(vid, video_file)
 ...
  File "main.py", line 73, in consume_for_homography_error
    matches = flann_matcher(descriptors1, descriptors2)
  File "main.py", line 48, in flann_matcher
    for i, (m, n) in enumerate(matches):
ValueError: need more than 1 value to unpack

这里似乎是什么问题?

最佳答案

knnMatch函数返回的matchs集合为列表类型,其中每个元素再次是2个DMatch对象的列表(由于k = 2)。因此,将枚举数应用于匹配列表时,您将在每次迭代中获得索引值和一个列表对象。您的代码在每次迭代中都期望索引值和元组。那就是问题所在。请查看以下代码。

flann = cv2.FlannBasedMatcher(index_params, search_params)

matches = flann.knnMatch(descriptors1, descriptors2, k=2)

# Retrieve good matches
good_matches = []

# ratio test as per Lowe's paper
for m,n in matches:
    if m.distance < 0.7*n.distance:
    good_matches.append((m, n))

关于python - OpenCV中的Flann KNN Matcher仅返回一个点而不是一对,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/31291993/

10-13 06:50