Ask Your Question

Revision history [back]

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

This stitching example

https://docs.opencv.org/3.0-beta/doc/py_tutorials/py_feature2d/py_matcher/py_matcher.html

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

  • https://docs.opencv.org/3.1.0/dc/de2/classcv_1_1FlannBasedMatcher.html
  • https://docs.opencv.org/3.1.0/d4/de0/classcv_1_1DMatch.html

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],

https://github.com/opencv/opencv/blob/05b15943d6a42c99e5f921b7dbaa8323f3c042c6/modules/features2d/src/draw.cpp#L209

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.