Ask Your Question

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

asked 2018-05-31 08:50:57 -0500

prinsherbert gravatar image

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.

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted

answered 2018-05-31 09:25:30 -0500

berak gravatar image

updated 2018-05-31 09:34:04 -0500

if you use knnMatch, it will return a list of (the best) K matches instead of a single DMatch. (K=2 was chosen in your example)

using match instead will return a single list of matches. (that's probably, what you expected)

edit flag offensive delete link more



In the first link of the OP, they test whether the best test is at least 1.0/0.7 times better than the second best, and that is why they use knnMatch instead of match (to also get the second bests)?

prinsherbert gravatar imageprinsherbert ( 2018-05-31 09:57:46 -0500 )edit

Yes, you are right.

vladvin gravatar imagevladvin ( 2020-01-27 06:19:49 -0500 )edit

Question Tools

1 follower


Asked: 2018-05-31 08:43:23 -0500

Seen: 4,109 times

Last updated: May 31 '18