-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsift.py
60 lines (46 loc) · 1.61 KB
/
sift.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
"""
Python module for use with David Lowe's SIFT code available at:
http://www.cs.ubc.ca/~lowe/keypoints/
adapted from the matlab code examples.
http://www.janeriksolem.net/2009/02/sift-python-implementation.html
"""
import os
from numpy import *
import pylab
def process_image(imagename, resultname,count):
""" process an image and save the results in a .sift ascii file"""
#check if linux or windows
if os.name == "posix":
cmmd = "./sift <"+imagename.split('/',1)[1]+"> "+resultname
else:
cmmd = "siftWin32 <"+imagenamesplit('/',1)[1]+">"+resultname
os.system(cmmd)
print('processed',imagename)
def read_features_from_file(filename):
""" read feature properties and return in matrix form"""
f = open(filename, 'r')
header = f.readline().split()
num = int(header[0]) #number of features
featlength = int(header[1]) #length of the descriptor
if featlength != 128: #should be 128
raise RuntimeError('Keypoint descriptor length invalid (should be 128).' )
locs = zeros((num, 4))
descriptors = zeros((num, featlength));
#parse the .key file
e = f.read().split() #split the rest into individual elements
pos = 0
for point in range(num):
#row, col, scale, orientation of each feature
for i in range(4):
locs[point,i] = float(e[pos+i])
pos += 4
#the descriptor values of each feature
for i in range(featlength):
descriptors[point,i] = int(e[pos+i])
#print(descriptors[point])
pos += 128
#normalize each input vector to unit length
descriptors[point] = descriptors[point]/linalg.norm(descriptors[point])
#print(descriptors[point])
f.close()
return locs,descriptors