繁体   English   中英

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

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

我需要获取特征匹配器在提供的代码中选择的像素的xy坐标列表。 我正在使用 Python 和 OpenCV。 谁能帮我?

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)

我们知道您的关键点存储在kp1kp2 ,它们分别是第一张和第二张图像的特征匹配列表。 cv2.ORB视角中,特征描述符是二维矩阵,其中每一行都是在第一幅图像和第二cv2.ORB图像中检测到的关键点。

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

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

因此, queryIdxtrainIdx告诉您第一幅图像和第二trainIdx图像之间哪些 ORB 特征匹配。 您将使用它们来索引kp1kp2并获得pt成员,它是(x,y)坐标的元组,用于确定匹配项的实际空间坐标。

您所要做的就是遍历matches每个cv2.DMatch对象,附加到kp1kp2的坐标列表,然后就完成了。

像这样的东西:

# 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 ,但我想让它如何解释的空间坐标非常清晰。 你也可以更进一步,做一个列表理解:

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

list_kp1将包含与list_kp2相应位置匹配的特征点的空间坐标。 换句话说, list_kp1元素i包含来自img1的特征点的空间坐标,该特征点与来自list_kp2img2的相应特征点匹配,其空间坐标在元素i


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

喜欢就去看看吧!

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

暂无
暂无

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

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