如何从OpenCV Python中的特征匹配中获取像素坐标 [英] How to get pixel coordinates from Feature Matching in OpenCV Python

查看:5037
本文介绍了如何从OpenCV Python中的特征匹配中获取像素坐标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要获取功能匹配器选择的像素的 x y 坐标列表提供的代码。我正在使用Python和OpenCV。任何人都可以帮助我吗?

I need to get the list of the x and y coordinates of the pixels that the feature matcher selects in the code provided. I'm using Python and 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)


推荐答案

我们知道您的关键点已存储在 kp1 kp2 中,它们分别是第一个和第二个图像的功能匹配。在 cv2.ORB 透视图中,这些是2D矩阵,其中每一行都是在第一张图像中检测到的关键点, kp1 和第二张图片, kp2

We know that your keypoints are stored in kp1 and kp2 where they are the features matches for the first and second image respectively. In the cv2.ORB perspective, these are 2D matrices where each row is a keypoint that is detected in first image, kp1 and the second image, kp2.

在您的情况下,因为您使用 cv2.BFMatch 匹配返回 cv2.DMatch 对象的列表,其中每个对象包含多个成员....其中有两个重要成员:

In your case because you are using cv2.BFMatch, matches returns a list of cv2.DMatch objects where each object contains several members.... among them are two important members:


  • queryIdx - kp1的索引或 匹配的兴趣点矩阵

  • trainIdx - <的索引或 code> kp2 匹配的兴趣点矩阵

  • queryIdx - The index or row of the kp1 interest point matrix that matches
  • trainIdx - The index or row of the kp2 interest point matrix that matches

因此, queryIdx trainIdx 告诉您哪些ORB功能在 kp1 之间匹配KP2 。因此,您可以使用这些索引到 kp1 kp2 并获取 pt member,它是(x,y)元组的元组,用于确定匹配项的实际空间坐标。

Therefore, queryIdx and trainIdx tell you which ORB features match between kp1 and kp2. Therefore, 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.

您所要做的就是遍历匹配中的每个 cv2.DMatch 对象,追加到 kp1 kp2 的坐标列表,你就完成了。

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.

这样的事情:

# 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))

请注意,我刚刚完成 list_kp1.append(kp1 [img1_idx] .pt) list_kp2 相同,但我想说清楚如何解释空间坐标。您还可以更进一步做一个列表理解:

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. 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 将包含与 list_kp2 中相应位置匹配的要素点的空间坐标。换句话说,元素 i list_kp1 包含来自 img1的特征点的空间坐标 img2 中<* c $ c> list_kp2 的相应要素点匹配,其空间坐标位于元素中

list_kp1 will contain the spatial coordinates of a feature point that matched with the corresponding position in 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.

作为次要的旁注,当我为 drawMatches 因为对于OpenCV 2.4.x, C ++函数的Python包装器不存在,所以我利用上面的概念来定位两个图像之间匹配特征的空间坐标来编写我自己的实现。

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!

模块'对象没有属性'drawMatches'opencv python

这篇关于如何从OpenCV Python中的特征匹配中获取像素坐标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆