user2967127
user2967127

Reputation: 41

How to create a SIFT's descriptors database [with Python]?

How do I create a database of SIFT descriptors (of images)? I'm using OpenCV in Pyhton 2.7, and my intention is to implement a supervisioned training set on Support Vector Machine.

Here is my code so far, for 1 image

import cv2
import numpy as np

img = cv2.imread('C:\Python27\.Clocktower1.jpg')
gray= cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)

sift = cv2.SIFT()
kp = sift.detect(gray,None)

img=cv2.drawKeypoints(gray,kp)

cv2.imwrite('sift_keypoints.jpg',img)

kp, des = sift.detectAndCompute(gray,None)

cv2.imshow('image',img)
k = cv2.waitKey(0)
if k == 27:        
    cv2.destroyAllWindows()
elif k == ord('s'): 
    cv2.destroyAllWindows()

a = np.arange(127*127).reshape(127, 127)
np.set_printoptions(edgeitems=127)

f = open('file.txt','w')
f.write('answer:'+str(des))
f.close()

I'm having trouble in saving the matrix des, which contains the kypoint vectors, and i still don't know a function to automatize the algorithm to more than 1 image per cycle.

Thank You

Edit:

the output is in the form:

answer:[[   5.    1.    0.    7.   14.    1.    3.    8.    2.    1.    0.    0.
     0.    3.   12.    8.    0.    7.   14.    3.    2.    4.    3.    1.
     1.    1.    6.    6.   17.    3.    0.    1.   29.   32.    4.   10.
    29.   77.   22.   14.   98.   31.    1.    0.    4.   41.   84.   76.
    13.   14.   38.   10.   59.  115.   53.   22.   16.    4.   21.   13.
    56.   90.   72.   31.   45.   19.    7.    3.   10.   89.   85.   46.
   115.   12.    2.    6.   16.   27.  115.  103.   55.    3.    4.   20.
   115.  101.   15.   29.  113.    8.   16.   40.   33.   53.   61.   47.
    65.   16.    0.    0.    5.   24.   90.   86.    8.    0.    8.   47.
    63.   33.   67.   51.    6.    1.   19.   64.   89.   73.   36.   18.
    21.    8.   60.  115.   31.   58.   11.    8.] ...
(other 128-numbers vectors)

I need to select the n best descriptors, in the sence of restricting the amount of keypoints i get in the output; is there an implementation to it?

Upvotes: 0

Views: 1435

Answers (1)

Kirubakumaresh
Kirubakumaresh

Reputation: 11

SIFT can take a parameter which states the number of best features to retain

When you instantiate SIFT, you should do it like this

sift = cv2.SIFT(n)

Refer http://docs.opencv.org/trunk/modules/nonfree/doc/feature_detection.html

Upvotes: 1

Related Questions