使用 OpenCV + Python 按 y 坐标过滤 SIFT 点

Filtering SIFT points by y-coordinate with OpenCV + Python

我有两个图像的 SIFT 关键点(用 Python + OpenCV 3 计算)。 我想通过他们的 y 坐标过滤它们。

具体来说,我想去掉y坐标差值大于图像高度除以10的所有匹配点,例如: 如果两个匹配点是A(x1, y1)和B(x2, y2):

if abs(y2 - y1) > imageHeight / 10 then remove that maching points.

我测试了什么

这是我测试过的代码。我正在删除关键点,但不是我想删除的。

# Load the two images
img1 = cv2.imread(PATH + "image1.jpg", -1)
img2 = cv2.imread(PATH + "image2.jpg", -1)


# Get their dimensions
height, width = img1.shape[:2]

# Resize them (they are too big)
img1 = cv2.resize(img1, (width / 4, height / 4))
img2 = cv2.resize(img2, (width / 4, height / 4))

# Get the resized image's dimensions
height, width = img1.shape[:2]

# Initiate SIFT detector
sift = X2D.SIFT_create()

# find the keypoints and descriptors with SIFT
kp1, des1 = sift.detectAndCompute(img1,None)
kp2, des2 = sift.detectAndCompute(img2,None)

# BFMatcher with default params
bf = cv2.BFMatcher()
matches = bf.knnMatch(des1,des2, k=2)

### Here the filtering attempt ###
# Alloc a new vector for filtered matches
filteredMatches = [None] * len(matches)
# Counter that will count how many matches I have at the end
counter = 0

# for each match
for i in range(len(matches)):

    # Get the "img1" heypoint
    leftPoint = kp1[ matches[i][0].queryIdx ].pt   #'left' image
    # Get the "img2" keypoint
    rightPoint = kp2[ matches[i][0].trainIdx ].pt  #'right' image

    # substract the y-coordinate of both points and compare
    # with height / 10
    if( abs(leftPoint[1] - rightPoint[1]) < height / 10):
        # if the difference is lower than higher / 10, add it
        # to the new list and increment the counter:
        filteredMatches[counter] = matches[i]
        counter += 1

# fix the filtered list size
matches = matches[:counter]

我不确定我是否正确使用了 queryIdxtrainIdx,但根据这个 post (What is `query` and `train` in openCV features2D) 我认为是这样。

我找到了解决办法。首先,根据 drawMatchesKnn 文档:

keypoints1[i]在keypoints2[matches[i]]有对应点

在我的代码中,'keypoints1' 是 kp1,'keypoints2' 是 kp2,'matches' 是 matches

kp1kp2的对应关系是:kp1[i] matches with kp2[ matches[i].trailIdx ].

这里是过滤关键点的 finally 函数,删除所有 y 坐标高于 图像高度 * n 的关键点,其中 n 是给定的数字(介于 0 和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]