I need to get the list of the x and y coordinates of the pixels that feature matcher selects in the code provided. I'm using Python and OpenCV. Can anyone help me?
matches=sorted(matches, key= lambda x:x.distance)
Best How To :
matches returns a list of structures where each structure contains several fields... among them are two important fields:
queryIdx - The index of the feature into
kp1 that matches
trainIdx - The index of the feature into
kp2 that matches
You'd use these to index into
kp2 and obtain the
pt field to obtain the
(x,y) coordinates of the matches.
All you have to do is iterate through each structure in
matches, append to a list of coordinates for both
kp2 and you're done.
Something like this:
# Initialize lists
list_kp1 = 
list_kp2 = 
# For each match...
for mat in matches:
# Get the matching keypoints for each of the images
img1_idx = mat.queryIdx
img2_idx = mat.trainIdx
# x - columns
# y - rows
# Get the coordinates
(x1,y1) = kp1[img1_idx].pt
(x2,y2) = kp2[img2_idx].pt
# Append to each list
Note that I could have just done
list_kp1.append(kp1[img1_idx].pt), as well as the same for
list_kp2, but I wanted to make very clear on how to interpret the spatial coordinates.
list_kp1 will contain the spatial coordinates of a feature point that matched with the corresponding position in
list_kp2. In other words, element
list_kp1 contains the spatial coordinates of the feature point from
img1 that matched with the corresponding feature point from
list_kp2 whose spatial coordinates are in element
As a minor sidenote, I used this concept when I wrote a workaround for
drawMatches because for OpenCV 2.4.x, the Python wrapper to the C++ function does not exist, so I made use of the above concept in locating the spatial coordinates of the matching features between the two images to write my own implementation of it.
Check it out if you like!
module' object has no attribute 'drawMatches' opencv python