Python 如何从 OpenCV“cv2.keypoint”对象中提取 x,y 坐标?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/35884409/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to extract x,y coordinates from OpenCV "cv2.keypoint" object?
提问by mengmengxyz
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”
回答by saikat sarkar
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
回答by Francesco Nazzaro
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
关键点。
回答by roadrunner66
class KeyPoint
类 KeyPoint
Data structure for salient point detectors.
显着点检测器的数据结构。
Point2f pt -- coordinates of the keypoint
float size -- diameter of the meaningful keypoint neighborhood
float angle ...?
Point2f pt -- 关键点的坐标
浮动大小——有意义的关键点邻域的直径
浮动角度...?
So point.pt
is a Point2f.
所以point.pt
是一个Point2f。
Try x,y= point.pt
尝试 x,y= point.pt
回答by Vik
OpenCV provides a function for this. You can run:
OpenCV 为此提供了一个函数。你可以运行:
pts = cv2.KeyPoint_convert(kp)
回答by quickbug
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()
回答by Alexander Hunt
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 坐标列表。