Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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'

like image 314
mengmengxyz Avatar asked Mar 09 '16 06:03

mengmengxyz


5 Answers

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()
like image 21
quickbug Avatar answered Nov 17 '22 09:11

quickbug


OpenCV provides a function for this. You can run:

pts = cv2.KeyPoint_convert(kp)
like image 168
Vik Avatar answered Nov 17 '22 07:11

Vik


point.pt is a tuple(x,y)`.

So,

x = point.pt[0]
y = point.pt[1]

or,

(x,y) = point.pt
like image 22
saikat sarkar Avatar answered Nov 17 '22 07:11

saikat sarkar


You can use:

import numpy as np

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

pts will be an array of keypoints.

like image 10
Francesco Nazzaro Avatar answered Nov 17 '22 08:11

Francesco Nazzaro


Read the docs.

class KeyPoint

Data structure for salient point detectors.

  • Point2f pt -- coordinates of the keypoint

  • float size -- diameter of the meaningful keypoint neighborhood

  • float angle ...¶

So point.pt is a Point2f.

Try x,y= point.pt

like image 7
roadrunner66 Avatar answered Nov 17 '22 07:11

roadrunner66