【问题标题】:Python - Distance between Feature Matching Keypoints with OpenCVPython - 与 OpenCV 的特征匹配关键点之间的距离
【发布时间】:2016-05-03 11:43:58
【问题描述】:

我正在尝试实现一个程序,该程序将输入两个立体图像并找到具有特征匹配的关键点之间的距离。有什么办法吗?我正在使用 SIFT/BFMatcher,我的代码如下:

import numpy as np
import cv2
from matplotlib import pyplot as plt

img1 = dst1
img2 = dst2

# Initiate SIFT detector
sift = cv2.SIFT()

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

# Apply ratio test
good = []
for m, n in matches:
    if m.distance < 0.3 * n.distance:
        good.append([m])

# cv2.drawMatchesKnn expects list of lists as matches.
img3 = cv2.drawMatchesKnn(img1, kp1, img2, kp2, good, flags=2, outImg=img2)

plt.imshow(img3), plt.show()

【问题讨论】:

    标签: python opencv pattern-matching stereo-3d


    【解决方案1】:

    以下算法查找img1 的关键点与其在img2 中的特征匹配关键点之间的距离(省略第一行):

    # Apply ratio test
    good = []
    for m,n in matches:
        if m.distance < 0.3 * n.distance:
            good.append(m)
    
    # Featured matched keypoints from images 1 and 2
    pts1 = np.float32([kp1[m.queryIdx].pt for m in good])
    pts2 = np.float32([kp2[m.trainIdx].pt for m in good])
    
    # Convert x, y coordinates into complex numbers
    # so that the distances are much easier to compute
    z1 = np.array([[complex(c[0],c[1]) for c in pts1]])
    z2 = np.array([[complex(c[0],c[1]) for c in pts2]])
    
    # Computes the intradistances between keypoints for each image
    KP_dist1 = abs(z1.T - z1)
    KP_dist2 = abs(z2.T - z2)
    
    # Distance between featured matched keypoints
    FM_dist = abs(z2 - z1)
    

    因此,KP_dist1 是一个对称矩阵,img1 关键点之间的距离,KP_dist2img2 相同,FM_dist 是一个列表,其中包含两个图像的特征匹配关键点之间的距离len(FM_dist) == len(good).

    希望这有帮助!

    【讨论】:

      猜你喜欢
      • 2014-12-20
      • 2017-10-14
      • 2015-07-21
      • 1970-01-01
      • 1970-01-01
      • 2018-05-08
      • 1970-01-01
      • 2022-11-27
      • 1970-01-01
      相关资源
      最近更新 更多