[英]how to extract x,y coordinates from OpenCV “cv2.keypoint” object?
I tried to use the following code: 我试着使用以下代码:
xCoordinate=point.x
(point is type of cv2.keyPoint) It gives me error saying cv2.keyPoint has no attribute 'x' (点是cv2.keyPoint的类型)它给我错误说cv2.keyPoint没有属性'x'
point.pt is a tuple
(x,y)`. point.pt is a tuple
(x,y)`。
So, 所以,
x = point.pt[0]
y = point.pt[1]
or, 要么,
(x,y) = point.pt
You can use: 您可以使用:
import numpy as np
pts = np.float([kp[idx].pt for idx in range(0, len(kp))]).reshape(-1, 1, 2)
pts
will be an array
of keypoints. pts
将是array
关键点。
Data structure for salient point detectors. 突出点检测器的数据结构。
Point2f pt -- coordinates of the keypoint Point2f pt - 关键点的坐标
float size -- diameter of the meaningful keypoint neighborhood float size - 有意义的关键点邻域的直径
float angle ...¶ 浮角...¶
So point.pt
is a Point2f. 所以point.pt
是Point2f。
Try x,y= point.pt
试试x,y= point.pt
Here is my take (runable code): 这是我的(可运行代码):
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()
OpenCV provides a function for this. OpenCV为此提供了一个功能。 You can run: 你可以运行:
pts = cv2.KeyPoint_convert(kp)
I solved your problem like this. 我这样解决了你的问题。
kp,des = surf.detectAndCompute(img,None)
pts = [p.pt for p in kp]
Now you get a list of x,y co-ordinates for all keypoints in your image. 现在,您将获得图像中所有关键点的x,y坐标列表。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.