Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OpenCV-Python : Find a code for writing keypoins to a file

Tags:

python

opencv

I am using OpenCV-Python binding to write my image processing application. I am finding a way to write keypoints of a image in to a file which we can get back for matching purpose. There is code in C/C++ to do this, but could not find a way to this by using python

Please anyone have an idea about this, please share with me & all of us

Thanks

like image 491
Dhanushka Gayashan Avatar asked Dec 25 '22 03:12

Dhanushka Gayashan


1 Answers

This is how you can do it, inspired from the link I gave earlier.

Save keypoints in a file

import cv2
import cPickle

im=cv2.imread("/home/bikz05/Desktop/dataset/checkered-3.jpg")
gr=cv2.cvtColor(im,cv2.COLOR_BGR2GRAY)
d=cv2.FeatureDetector_create("SIFT")
kp=d.detect(gr)

index = []
for point in kp:
    temp = (point.pt, point.size, point.angle, point.response, point.octave, 
        point.class_id) 
    index.append(temp)

# Dump the keypoints
f = open("/home/bikz05/Desktop/dataset/keypoints.txt", "w")
f.write(cPickle.dumps(index))
f.close()

Load and Display keypoints in the image

import cv2
import cPickle

im=cv2.imread("/home/bikz05/Desktop/dataset/checkered-3.jpg")

index = cPickle.loads(open("/home/bikz05/Desktop/dataset/keypoints.txt").read())

kp = []

for point in index:
    temp = cv2.KeyPoint(x=point[0][0],y=point[0][1],_size=point[1], _angle=point[2], 
                            _response=point[3], _octave=point[4], _class_id=point[5]) 
    kp.append(temp)

# Draw the keypoints
imm=cv2.drawKeypoints(im, kp);
cv2.imshow("Image", imm);
cv2.waitKey(0)

INPUT IMAGE to 1st script

enter image description here

Displayed IMAGE in 2nd script

enter image description here

like image 191
bikz05 Avatar answered Dec 28 '22 19:12

bikz05