简体   繁体   English

如何从 OpenCV Python 中的特征匹配中获取像素坐标

[英]How to get pixel coordinates from Feature Matching in OpenCV Python

I need to get the list of the x and y coordinates of the pixels that the feature matcher selects in the code provided.我需要获取特征匹配器在提供的代码中选择的像素的xy坐标列表。 I'm using Python and OpenCV.我正在使用 Python 和 OpenCV。 Can anyone help me?谁能帮我?

img1=cv2.imread('DSC_0216.jpg',0)
img2=cv2.imread('DSC_0217.jpg',0)

orb=cv2.ORB(nfeatures=100000)
kp1,des1=orb.detectAndCompute(img1,None)
kp2,des2=orb.detectAndCompute(img2,None)

img1kp=cv2.drawKeypoints(img1,kp1,color=(0,255,0),flags=0)
img2kp=cv2.drawKeypoints(img2,kp2,color=(0,255,0),flags=0)
cv2.imwrite('m_img1.jpg',img1kp)
cv2.imwrite('m_img2.jpg',img2kp)

bf=cv2.BFMatcher(cv2.NORM_HAMMING, crossCheck=True)
matches=bf.match(des1,des2)
matches=sorted(matches, key= lambda x:x.distance)

We know that your keypoints are stored in kp1 and kp2 where they are lists of feature matches for the first and second image respectively.我们知道您的关键点存储在kp1kp2 ,它们分别是第一张和第二张图像的特征匹配列表。 In the cv2.ORB perspective, the feature descriptors are 2D matrices where each row is a keypoint that is detected in the first and second image.cv2.ORB视角中,特征描述符是二维矩阵,其中每一行都是在第一幅图像和第二cv2.ORB图像中检测到的关键点。

In your case because you are using cv2.BFMatch , matches returns a list of cv2.DMatch objects where each object contains several members and among them are two important members:在您的情况下,因为您使用的是cv2.BFMatchmatches返回一个cv2.DMatch对象列表,其中每个对象包含多个成员,其中有两个重要成员:

  • queryIdx - The index or row of the kp1 interest point matrix that matches queryIdx - 匹配的kp1兴趣点矩阵的索引或
  • trainIdx - The index or row of the kp2 interest point matrix that matches trainIdx - 匹配的kp2兴趣点矩阵的索引或

Therefore, queryIdx and trainIdx tell you which ORB features match between the first and second image.因此, queryIdxtrainIdx告诉您第一幅图像和第二trainIdx图像之间哪些 ORB 特征匹配。 You'd use these to index into kp1 and kp2 and obtain the pt member, which is a tuple of (x,y) coordinates that determine the actual spatial coordinates of the matches.您将使用它们来索引kp1kp2并获得pt成员,它是(x,y)坐标的元组,用于确定匹配项的实际空间坐标。

All you have to do is iterate through each cv2.DMatch object in matches , append to a list of coordinates for both kp1 and kp2 and you're done.您所要做的就是遍历matches每个cv2.DMatch对象,附加到kp1kp2的坐标列表,然后就完成了。

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
    list_kp1.append((x1, y1))
    list_kp2.append((x2, y2))

Note that I could have just done list_kp1.append(kp1[img1_idx].pt) and the same for list_kp2 , but I wanted to make it very clear on how to interpret the spatial coordinates.请注意,我刚才做list_kp1.append(kp1[img1_idx].pt)与同为list_kp2 ,但我想让它如何解释的空间坐标非常清晰。 You could also go one step further and do a list comprehension:你也可以更进一步,做一个列表理解:

list_kp1 = [kp1[mat.queryIdx].pt for mat in matches] 
list_kp2 = [kp2[mat.trainIdx].pt for mat in matches]

list_kp1 will contain the spatial coordinates of a feature point that matched with the corresponding position in list_kp2 . list_kp1将包含与list_kp2相应位置匹配的特征点的空间坐标。 In other words, element i of list_kp1 contains the spatial coordinates of the feature point from img1 that matched with the corresponding feature point from img2 in list_kp2 whose spatial coordinates are in element i .换句话说, list_kp1元素i包含来自img1的特征点的空间坐标,该特征点与来自list_kp2img2的相应特征点匹配,其空间坐标在元素i


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.作为一个次要的旁注,我在为drawMatches编写解决方法时使用了这个概念,因为对于 OpenCV 2.4.x,C++ 函数的 Python 包装器不存在,所以我利用了上述概念来定位匹配的空间坐标两个图像之间的功能来编写我自己的实现。

Check it out if you like!喜欢就去看看吧!

module' object has no attribute 'drawMatches' opencv python 模块'对象没有属性'drawMatches' opencv python

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM