In [2]:
from PIL import Image
from numpy import *
from pylab import *
In [3]:
import harris
In [4]:
import sift
In [5]:
sift=reload(sift)
In [7]:
imname1 = 'climbing_1_small'
imname2 = 'climbing_3_small'
im1 = array(Image.open(imname1+'.jpg').convert('L'))
im2 = array(Image.open(imname2+'.jpg').convert('L'))
sift.process_image(imname1+'.jpg', imname1+'.sift')
l1,d1 = sift.read_features_from_file(imname1+'.sift')
sift.process_image(imname2+'.jpg', imname2+'.sift')
l2,d2 = sift.read_features_from_file(imname2+'.sift')
In [8]:
matches = sift.match_twosided(d1, d2)
In [9]:
figure(figsize=(16, 16))
gray()
sift.plot_matches(im1, im2, l1, l2, matches)
show()
In [19]:
figure(figsize=(16,16))
show_below=True
im3 = sift.appendimages(im1, im2)
if show_below:
im3 = vstack((im3, im3))
imshow(im3)
cols1 = im1.shape[1]
p = []
for i,m in enumerate(matches):
if m>0:
plot([l1[i][0],l2[m][0]+cols1],[l1[i][1],l2[m][1]],'c')
p.append([l1[i][0], l1[i][1], l2[m][0], l2[m][1]])
axis('off')
show()
x1 = np.average((array(p).T)[0][2:-2])
y1 = np.average((array(p).T)[1][2:-2])
x2 = np.average((array(p).T)[2][2:-2])
y2 = np.average((array(p).T)[3][2:-2])
print '(', x1, ', ', y1, ')'
print '(', x2, ', ', y2, ')'
scale=[]
theta=[]
for i,m in enumerate(matches):
if m>0:
r1 = math.sqrt((l1[i][0]-x1)**2 + (l1[i][1]-y1)**2)
r2 = math.sqrt((l2[m][0]-x2)**2 + (l2[m][1]-y2)**2)
if (r2>10):
scale.append(r2/r1)
if (l2[m][0]-x2>10 and l1[i][0]-x1>10):
t = math.atan2(l2[m][1]-y2, l2[m][0]-x2)-math.atan2(l1[i][1]-y1, l1[i][0]-x1)
if (t<-pi):
t += 2*pi
if (t>pi):
t -= 2*pi
theta.append(t)
s = np.average(array(scale)[4:-4])
t = np.average(array(theta)[4:-4])
print "scale ratio:", s
print "rotation:", 180.0*t/pi
In [13]:
print x1, y1, x2, y2
In [14]:
print theta
In [52]:
print sum(theta)/len(theta)
In [56]:
0.4*180/pi
Out[56]:
In [54]:
print pi
In [ ]: