Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

I have found the solution. First of all, according to drawMatchesKnn documentation:

keypoints1[i] has a corresponding point in keypoints2[matches[i]]

In my code, 'keypoints1' is kp1, 'keypoints2' is kp2 and 'matches' is matches.

The correspondency between kp1 and kp2 is: kp1[i] matches with kp2[ matches[i].trailIdx ].

Here the finally function which filter the keypoints removing all of them which y-coordinate is higher than the image's height * n, where n is a given number (between 0 and 1):

def filterMatches(kp1, kp2, matches, imgHeight, thresFactor = 0.4):
"""
Removes the matches that correspond to a pair of keypoints (kp1, kp2)
which y-coordinate difference is lower than imgHeight * thresFactor.

Args:
    kp1 (array of cv2.KeyPoint): Key Points.

    kp2 (array of cv2.KeyPoint): Key Points.

    matches (array of cv2.DMATCH): Matches between kp1 and kp2.

    imgHeight (Integer): height of the image that has produced kp1 or kp2.

    thresFactor (Float): Use to calculate the threshold. Threshold is 
        imgHeight * thresFactor.

Returns:
    array of cv2.DMATCH: filtered matches.

"""
filteredMatches = [None]*len(matches)
counter = 0
threshold = imgHeight * thresFactor
for i in range(len(kp1)):
    srcPoint = kp1[ matches[i][0].queryIdx ].pt
    dstPoint = kp2[ matches[i][0].trainIdx ].pt
    diff = abs(srcPoint[1] - dstPoint[1])
    if( diff < threshold):
        filteredMatches[counter] = matches[i]
        counter += 1

return filteredMatches[:counter]