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



我有两个图像的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,但根据这篇文章(什么是 openCV features2D 中的"查询"和"训练"(,我认为是这样。

我已经找到了解决方案。首先,根据drawMatchesKnn文档:

关键点1[i]

在关键点2[匹配[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]

最新更新