-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathsift.py
81 lines (72 loc) · 2.28 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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#!/usr/bin/python3
import os,platform,subprocess
import numpy as np
from PIL import Image
from matplotlib import pyplot as plt
def getSystemInfo():
sys_str=platform.system()
arch_str=platform.architecture()[0]
return sys_str+'_'+arch_str
def SIFTFeatures(filename,outfile,params='--edge-thresh 10 --peak-thresh 5'):
if not os.path.exists(filename):
raise ValueError('File %s does not exists!' %filename)
if filename[-3:]!='pgm':#must use the image in pgm format
img=Image.open(filename).convert('L')
img.save('tmp.pgm')
tmpfilename=os.path.abspath('tmp.pgm')
workdir=os.path.abspath('.')
siftdir='.'
sysinfo=getSystemInfo().lower()
exe_str='sift'
if sysinfo=='windows_64bit':
siftdir='sift_bin/win64'
elif sysinfo=='windows_32bit':
siftdir='sift_bin/win32'
elif sysinfo=='linux_64bit':
siftdir='sift_bin/glnxa64'
elif sysinfo=='linux_32bit':
siftdir='sift_bin/glnx86'
elif sysinfo=='darwin_64bit':
siftdir='sift_bin/maci64'
elif sysinfo=='darwin_32bit':
siftdir='sift_bin/maci'
else:
raise ValueError('Unknown platform %s' %sysinfo)
os.chdir(siftdir)
call_cmd=str(exe_str+' '+tmpfilename+' --output='+outfile+' '+params)
#os.system(call_cmd)
subprocess.call(call_cmd)
sift_fea=np.genfromtxt(outfile,dtype=np.float)
location=sift_fea[:,:4] #(row,col,scale,orientation of each feature
sift=sift_fea[:,4:]
pos=filename.rfind(r'/')
print(filename[pos+1:])
os.chdir(workdir)
return sift,location
def plot_features(img,locs,circle=False):
"""
Show image with features.
locs:row,col,scale,orientation of each feature
"""
def draw_circle(pos,radius):
t=np.arange(0,1.01,.01)*2*np.pi
x=radius*np.cos(t)+pos[0]
y=radius*np.sin(t)+pos[1]
plt.plot(x,y,'b',linewidth=2)
plt.figure()
plt.gray()
plt.imshow(img)
if circle:
for p in locs:
draw_circle(p[:2],p[2])
else:
plt.plot(locs[:,0],locs[:,1],'ob')
plt.axis('off')
plt.show()
if __name__=='__main__':
filename='25.jpg'
img=Image.open(filename)
img=img.convert('L')
im=np.array(img)
fea,locs=SIFTFeatures(filename,'tmp')
plot_features(im,locs,False)