Filtering SIFT points by y-coordinate with OpenCV + Python
I have the SIFT keypoints of two images (calculated with Python + OpenCV 3). I want to filter them by their y-coordinate.
Specifically, I want to remove all matching points whose difference of y-coordinate is higher than the image height divided by 10, for example: If two matching points are A(x1, y1) and B(x2, y2):
if abs(y2 - y1) > imageHeight / 10 then remove that maching points
.
What I have test
Here is the code I have tested. I'm removing keypoints, but not what I want to remove.
# 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]
I'm not sure if I'm using queryIdx
and trainIdx
correctly, but according with this post (http://stackoverflow.com/questions/10...) I think so.