Search code examples
pythoncsvsift

Writing Sift Key-Points in CSV file in Python


I want to get the values of KP in CSV file to get to the gather multiple feature values into the machine learning classifier

import cv2
img = cv2.imread(path,cv2.IMREAD_GRAYSCALE)
sift = cv2.xfeatures2d.SIFT_create(400)
kp = sift.detect(img,None)
img = cv2.drawKeypoints(img, kp, None)
cv2.imshow("",img)
cv2.waitKey(0)
cv2.destroyAllWindows()
import csv
with open('f:/sift.csv', 'w') as f:
        writer = csv.writer(f)
        writer.writerow(['sift'])
        writer.writerows(kp)

Spyder output screen


Solution

  • import cv2
    import csv
    path = 'an image.jpg'
    im=cv2.imread(path)
    gr=cv2.cvtColor(im,cv2.COLOR_BGR2GRAY)
    d = cv2.xfeatures2d.SIFT_create(500)
    kp=d.detect(gr)
    gr = cv2.drawKeypoints(gr, kp, None)
    cv2.imshow("Sift",gr)
    cv2.waitKey(0)
    cv2.destroyAllWindows()
    index = []
    for point in kp:
        temp = (point.pt, point.size, point.angle, point.response, point.octave,point.class_id)
        index.append(temp)
    with open('c:/sift.csv', 'w') as myfile:
         wr = csv.writer(myfile)
         wr.writerows(index)