Ask Your Question

Revision history [back]

Why does knnMatch return a list of tuples instead of a list of DMatch?

This stitching example

pipes the output of flann.knnMatch to cv2.drawMatchesKnn. Since the reference pages do not come close to properly documenting the output of knnMatch:


I decided to look at the implementation of drawMatchesKnn, but found something strange. In the python-code, the output of knnMatch is a list of lists of two DMatch objects:

matches[:3] == (
[[<DMatch 0x7ff57f981710>, <DMatch 0x7ff57f981130>],
 [<DMatch 0x7ff57f981330>, <DMatch 0x7ff57f981290>],
 [<DMatch 0x7ff57f981310>, <DMatch 0x7ff57f9816f0>]]

However drawMatchesKnn, to which matches is passed, these two line suggest it is a list of DMatches, instead of a list of lists of DMatches, since there is only one index [m] not two [m][n],

Moreover, this loop for i,(m,n) in enumerate(matches): in the first link also seems to ignore the second DMatch n in each sublist.

What is this second DMatch in each item of matches? Why is matches a list of tuples of DMatches, and not a list of 'DMatch`es?

It seems as if I can ignore the second DMatch, yet I don't feel comfortable blindly doing so.