def get_matching_pts_sift(im_path_1, im_path_2): """ Gets the matching points in the two images, using SIFT features. """ # Process and save features to file params = "--edge-thresh 10 --peak-thresh 5 --verbose" sift.process_image(im_path_1, im_path_1 + '.sift', params=params) sift.process_image(im_path_2, im_path_2 + '.sift', params=params) # Read features from the two images. l1, d1 = sift.read_features_from_file(im_path_1 + '.sift') l2, d2 = sift.read_features_from_file(im_path_2 + '.sift') # matchscores will have an entry for each feature in im1. # The entry will be 0 if there is not a match. # If there is a match, the entry will be the index of the matching feature in im2. matchscores = sift.match_twosided(d1, d2) pts1, pts2 = get_matches(l1, l2, matchscores) return pts1, pts2
from pylab 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, 'climbing_1_small.sift') sift.process_image(imname2, 'climbing_2_small.sift') #sift.process_image(imname1, imname1+'.sift') #sift.process_image(imname2, imname2+'.sift') # read features and match l1, d1 = sift.read_features_from_file('climbing_1_small.sift') l2, d2 = sift.read_features_from_file('climbing_2_small.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()
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 K = my_calibration((747, 1000)) # 3D points at plane z=0 with sides of length 0.2 box = cube_points([0, 0, 0.1], 0.1) # project bottom square in first image
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 for i in range(nbr_images): for j in range(i + 1, nbr_images):
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 print "The match scores is: %d", matchscores #np.savetxt(("../data/panoimages/panoramio_matches.txt",matchscores) # 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
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 K = my_calibration((747, 1000)) # 3D points at plane z=0 with sides of length 0.2 box = cube_points([0, 0, 0.1], 0.1) # project bottom square in first image
from pylab 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, './climbing_1_small.sift') sift.process_image(imname2, './climbing_2_small.sift') # sift.process_image(imname1, imname1+'.sift') # sift.process_image(imname2, imname2+'.sift') # read features and match l1, d1 = sift.read_features_from_file('./climbing_1_small.sift') l2, d2 = sift.read_features_from_file('./climbing_2_small.sift') #matchscores = sift.match(d1, d2) 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()
tic.k('start') l, d = {}, {} for i in range(len(imname)): # sift.process_image('./images/salcatraz1.jpg','./images/salcatraz1.sift') l[i], d[i] = sift.read_features_from_file(siftname[i]) tic.k('loaded sifts') print '{} / {} features'.format(len(d[0]), len(d[1])) immd5 = md5.md5(''.join(imname)).hexdigest() matchcache = 'out_ch05ex02_cache_matches_%s.pickle' % immd5 if not os.path.exists(matchcache): #matches = sift.match(d[0], d[1]) matches = sift.match_twosided(d[0], d[1]) pickle.dump(matches, open(matchcache, 'wb')) matches = pickle.load(open(matchcache, 'rb')) tic.k('matched') ndx = matches.nonzero()[0] x1 = homography.make_homog(l[0][ndx, :2].T) x1 = numpy.array([x1[1,:],x1[0,:],x1[2,:]]) ndx2 = [int(matches[i]) for i in ndx] x2 = homography.make_homog(l[1][ndx2, :2].T) x2 = numpy.array([x2[1,:],x2[0,:],x2[2,:]]) print '{} matches'.format(len(ndx)) image = [numpy.array(Image.open(name)) for name in imname]