如何从OpenCV的"cv2.keypoint"对象中提取x和y坐标?

15

我尝试使用以下代码:

    xCoordinate=point.x

(该点是cv2.keyPoint类型) 它给我一个错误,说cv2.keyPoint没有属性'x'


出现错误,指出cv2.keyPoint没有'x'属性。

7个回答

23
`point.pt` 是一个包含 x 和 y 坐标的元组。
因此,
x = point.pt[0]
y = point.pt[1]

或者,
(x,y) = point.pt

这个在文档中哪里可以找到? - Ender

14

OpenCV 提供了一个函数来实现此功能。您可以运行:

pts = cv2.KeyPoint_convert(kp)

10

您可以使用:

import numpy as np

pts = np.float([key_point.pt for key_point in kp]).reshape(-1, 1, 2)

pts将是一个关键点的数组。


8

点击阅读文档。

Keypoint类

突出点检测器的数据结构。

  • Point2f pt -- 关键点的坐标

  • float size -- 有意义的关键点邻域的直径

  • float angle ...¶

因此,point.pt是一个Point2f。

尝试x,y= point.pt


2
point.pt.x is invalid, please correct it as: x, y = point.pt - Nirmal

4

以下是我的代码示例:

import cv2, os
import numpy as np
import matplotlib.pyplot as plt

# INITIALISATION
filename = os.path.join('foo', 'bar.jpg')
img0 = cv2.imread(filename) # original image
gray = cv2.cvtColor(img0, cv2.COLOR_BGR2GRAY) # convert to grayscale
sift = cv2.xfeatures2d.SIFT_create() # initialize SIFT 
f, (ax1, ax2) = plt.subplots(1, 2) # create subplots

# DETECT AND DRAW KEYPOINTS
# sift.detect() returns a list of keypoints
# keypoint is a standard class of opencv (not just SIFT-related)
kp = sift.detect(gray,None) # calculates SIFT points
img1=cv2.drawKeypoints(gray,kp, None) # mae new image with keypoints drawn
ax1.imshow(img1) # plot 

# RETREIVE KEYPOINTS COORDINATES AND DRAW MANUALLY
# Reade these and make numpy array
pts = np.asarray([[p.pt[0], p.pt[1]] for p in kp])
cols = pts[:,0]
rows = pts[:,1]
ax2.imshow(cv2.cvtColor(img0, cv2.COLOR_BGR2RGB))
ax2.scatter(cols, rows)

plt.show()

2
我是这样解决你的问题的。
kp,des = surf.detectAndCompute(img,None) 
pts = [p.pt for p in kp]

现在,您可以获得图像中所有关键点的x,y坐标列表。

0
为了获得正确的光流输入形状,我使用了上述方法的组合。
kp = sift.detect(old_frame, None)
pts0 = cv2.KeyPoint.convert(kp).reshape(-1, 1, 2)

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接