def train(self,featurefiles,k=100,subsampling=10):
     """ Train a vocabulary from features in files listed 
         in featurefiles using k-means with k number of words. 
         Subsampling of training data can be used for speedup. """
     
     nbr_images = len(featurefiles)
     # read the features from file
     descr = []
     descr.append(sift.read_features_from_file(featurefiles[0])[1])
     descriptors = descr[0] #stack all features for k-means
     for i in arange(1,nbr_images):
         descr.append(sift.read_features_from_file(featurefiles[i])[1])
         descriptors = vstack((descriptors,descr[i]))
         
     # k-means: last number determines number of runs
     self.voc,distortion = kmeans(descriptors[::subsampling,:],k,1)
     self.nbr_words = self.voc.shape[0]
     
     # go through all training images and project on vocabulary
     imwords = zeros((nbr_images,self.nbr_words))
     for i in range( nbr_images ):
         imwords[i] = self.project(descr[i])
     
     nbr_occurences = sum( (imwords > 0)*1 ,axis=0)
     
     self.idf = log( (1.0*nbr_images) / (1.0*nbr_occurences+1) )
     self.trainingdata = featurefiles
def read_gesture_features_labels(path):
    # create list of all files ending in .dsift
    featlist = [
        os.path.join(path, f) for f in os.listdir(path) if f.endswith('.dsift')
    ]

    # read the features
    features = []
    for featfile in featlist:
        l, d = sift.read_features_from_file(featfile)
        features.append(d.flatten())
    features = array(features)

    # create labels
    labels = [featfile.split('/')[-1][0] for featfile in featlist]

    return features, array(labels)
from PIL import Image
from pylab import *
from numpy import *
from pcv.localdescriptors import dsift, sift
"""
This is the dense SIFT illustration, it will reproduce the plot
in Figure 8-2.
"""
dsift.process_image_dsift('../data/empire.jpg', 'empire.sift', 90, 40, True)
l, d = sift.read_features_from_file('empire.sift')
im = array(Image.open('../data/empire.jpg'))
sift.plot_features(im, l, True)
show()

def my_calibration(sz):
    """
    Calibration function for the camera (iPhone4) used in this example.
    """
    row, col = sz
    fx = 2555 * col / 2592
    fy = 2586 * row / 1936
    K = diag([fx, fy, 1])
    K[0, 2] = 0.5 * col
    K[1, 2] = 0.5 * row
    return K


# compute features
sift.process_image('../data/book_frontal.JPG', 'im0.sift')
l0, d0 = sift.read_features_from_file('im0.sift')

sift.process_image('../data/book_perspective.JPG', 'im1.sift')
l1, d1 = sift.read_features_from_file('im1.sift')

# match features and estimate homography
matches = sift.match_twosided(d0, d1)
ndx = matches.nonzero()[0]
fp = homography.make_homog(l0[ndx, :2].T)
ndx2 = [int(matches[i]) for i in ndx]
tp = homography.make_homog(l1[ndx2, :2].T)

model = homography.RansacModel()
H, inliers = homography.H_from_ransac(fp, tp, model)

# camera calibration
from pcv.tools import imtools

"""
This will process all hand gesture images with the dense SIFT descriptor.

Assumes you downloaded the hand images to ..data/hand_gesture.
The plot at the end generates one of the images of Figure 8-3.
"""

path = '../data/hand_gesture/train/'
imlist = []
for filename in os.listdir(path):
    if os.path.splitext(filename)[1] == '.ppm':
        imlist.append(path+filename)


# process images at fixed size (50,50)
for filename in imlist:
    featfile = filename[:-3]+'dsift'
    dsift.process_image_dsift(filename, featfile, 10, 5, resize=(50,50))


# show an image with features
l,d = sift.read_features_from_file(featfile)
im = array(Image.open(filename).resize((50,50)))
print im.shape

figure()
sift.plot_features(im, l, True)
show()
from pcv.geometry import homography, warp
from pcv.localdescriptors import sift
"""
This is the panorama example from section 3.3.
"""

# set paths to data folder
featname = ['../data/Univ' + str(i + 1) + '.sift' for i in range(5)]
imname = ['../data/Univ' + str(i + 1) + '.jpg' for i in range(5)]

# extract features and match
l = {}
d = {}
for i in range(5):
    sift.process_image(imname[i], featname[i])
    l[i], d[i] = sift.read_features_from_file(featname[i])

matches = {}
for i in range(4):
    matches[i] = sift.match(d[i + 1], d[i])

# visualize the matches (Figure 3-11 in the book)
for i in range(4):
    im1 = array(Image.open(imname[i]))
    im2 = array(Image.open(imname[i + 1]))
    figure()
    sift.plot_matches(im2, im1, l[i + 1], l[i], matches[i], show_below=True)


# function to convert the matches to hom. points
def convert_points(j):
# list of downloaded filenames
imlist = imtools.get_imlist(download_path)
nbr_images = len(imlist)

# extract features
featlist = [imname[:-3] + 'sift' for imname in imlist]
for i, imname in enumerate(imlist):
    sift.process_image(imname, featlist[i])

matchscores = zeros((nbr_images, nbr_images))

for i in range(nbr_images):
    for j in range(i, nbr_images):  # only compute upper triangle
        print 'comparing ', imlist[i], imlist[j]
        l1, d1 = sift.read_features_from_file(featlist[i])
        l2, d2 = sift.read_features_from_file(featlist[j])
        matches = sift.match_twosided(d1, d2)
        nbr_matches = sum(matches > 0)
        print 'number of matches = ', nbr_matches
        matchscores[i, j] = nbr_matches

# copy values
for i in range(nbr_images):
    for j in range(i + 1, nbr_images):  # no need to copy diagonal
        matchscores[j, i] = matchscores[i, j]

threshold = 2  # min number of matches needed to create link

g = pydot.Dot(graph_type='graph')  # don't want the default directed graph
from pylab import *
from numpy import *
from PIL import Image

from pcv.localdescriptors import sift
"""
This is the twosided SIFT feature matching example from Section 2.2 (p 44).
"""

imname1 = '../data/climbing_1_small.jpg'
imname2 = '../data/climbing_2_small.jpg'

# process and save features to file
sift.process_image(imname1, imname1 + '.sift')
sift.process_image(imname2, imname2 + '.sift')

# read features and match
l1, d1 = sift.read_features_from_file(imname1 + '.sift')
l2, d2 = sift.read_features_from_file(imname2 + '.sift')
matchscores = sift.match_twosided(d1, d2)

# load images and plot
im1 = array(Image.open(imname1))
im2 = array(Image.open(imname2))

sift.plot_matches(im1, im2, l1, l2, matchscores, show_below=True)
show()