简体   繁体   English

如何从OpenCV“cv2.keypoint”对象中提取x,y坐标?

[英]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关键点。

Read the docs. 阅读文档。

class KeyPoint 类KeyPoint

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.

相关问题 当类从cv2.KeyPoint继承时,一个错误导致崩溃 - A bug causes crash when class inherit from cv2.KeyPoint 酸洗 cv2.KeyPoint 导致 PicklingError - Pickling cv2.KeyPoint causes PicklingError 有没有办法从 opencv object tracker kcf 中提取 cv2.rectangle 的坐标? - Is there a way to extract the coordinates from cv2.rectangle from the opencv object tracker kcf? 如何从图像中提取(x,y)坐标并写入 CSV 文件? - How to extract (x, y) coordinates from image and write to CSV file? 在OpenCV中获取具有X,Y和到对象的距离的3D坐标 - Get 3D coordinates in OpenCV having X,Y and distance to object 在 Open CV Python 中从 cv2.contour 获取 x 和 y 坐标并将其存储到不同的变量中 - Getting the x and y coordinates from cv2.contour in Open CV Python and store it to different variables 如何使用 python 在 opencv 中打印 x 和 y 坐标? - How to print x and y coordinates in opencv using python? 在 Python 中从图像中提取每个像素的 x,y 坐标 - Extract x,y coordinates of each pixel from an image in Python 如何从python中的轮廓线提取坐标并将其存储在新的x和y变量中? - How to extract coordinates from contour lines in python and store them in new x and y variables? 如何使用Python从地理数据中提取x,y和z坐标? - How can I extract x, y and z coordinates from geographical data by Python?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM