def find_similar(imagenames, plot_features=False): '''Find similar images pairs ''' first_figure = True images = [ MyImage(i) for i in imagenames ] matches = defaultdict(list) length = len(images) if plot_features: for image in images: if first_figure: first_figure = False else: pylab.figure() sift.plot_features(image.im, image.locs, circle=True) for i in range(length): this = images[i] print 'Find match for', this.filename, ' ', for j in range(i+1, length): that = images[j] sys.stdout.write('.') sys.stdout.flush() matched_points = nn.match_twosided(this.desc, that.desc) similarity = sum(matched_points > 0) matches[i].append((similarity, j, matched_points)) matches[j].append((similarity, i, matched_points)) print ' ', # at least 10 points matched # and only need top 3 matches scores = sorted([k for k in matches[i] if k[0] > 10 ], reverse=True)[:3] if not scores: print 'No match :(' else: for sim, k, matched_points in scores: that = images[k] print '%s(%d) ' % (that.filename, sim), if first_figure: first_figure = False else: pylab.figure() if k < i: # only cal the top right triangle this = images[k] that = images[i] sift.plot_matches(this.im, that.im, this.locs, that.locs, matched_points) print pylab.show()
def find_similar(imagenames, plot_features=False): '''Find similar images pairs ''' first_figure = True images = [MyImage(i) for i in imagenames] matches = defaultdict(list) length = len(images) if plot_features: for image in images: if first_figure: first_figure = False else: pylab.figure() sift.plot_features(image.im, image.locs, circle=True) for i in range(length): this = images[i] print 'Find match for', this.filename, ' ', for j in range(i + 1, length): that = images[j] sys.stdout.write('.') sys.stdout.flush() matched_points = nn.match_twosided(this.desc, that.desc) similarity = sum(matched_points > 0) matches[i].append((similarity, j, matched_points)) matches[j].append((similarity, i, matched_points)) print ' ', # at least 10 points matched # and only need top 3 matches scores = sorted([k for k in matches[i] if k[0] > 10], reverse=True)[:3] if not scores: print 'No match :(' else: for sim, k, matched_points in scores: that = images[k] print '%s(%d) ' % (that.filename, sim), if first_figure: first_figure = False else: pylab.figure() if k < i: # only cal the top right triangle this = images[k] that = images[i] sift.plot_matches(this.im, that.im, this.locs, that.locs, matched_points) print pylab.show()
def plot_match(imagename1, imagename2, show=True, plot_features=True): '''Generate SIFT features for two images, plot their features onto the original images and link two-sided matching points with lines ''' pylab.gray() # plot features on images # plot matching points # matchscores = sift.match_twosided(desc1, desc2) matchscores = nn.match_twosided(desc1, desc2) sift.plot_matches(im1, im2, locs1, locs2, matchscores) pylab.figure() if show: pylab.show()
l = {} d = {} for i in range(5): sift.process_image(root + imname[i], root + 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): ndx = matches[j].nonzero()[0] fp = homography.make_homog(l[j + 1][ndx, :2].T) ndx2 = [int(matches[j][i]) for i in ndx] tp = homography.make_homog(l[j][ndx2, :2].T) # switch x and y - TODO this should move elsewhere fp = vstack([fp[1], fp[0], fp[2]]) tp = vstack([tp[1], tp[0], tp[2]]) return fp, tp
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()
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): ndx = matches[j].nonzero()[0] fp = homography.make_homog(l[j+1][ndx,:2].T) ndx2 = [int(matches[j][i]) for i in ndx] tp = homography.make_homog(l[j][ndx2,:2].T) # switch x and y - TODO this should move elsewhere fp = vstack([fp[1],fp[0],fp[2]]) tp = vstack([tp[1],tp[0],tp[2]]) return fp,tp
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()
l2, d2 = sift.read_features_from_file('im2.sift') matches = sift.match_twosided(d1, d2) ndx = matches.nonzero()[0] x1 = homography.make_homog(l1[ndx, :2].T)#将点集转化为齐次坐标表示 ndx2 = [int(matches[i]) for i in ndx] x2 = homography.make_homog(l2[ndx2, :2].T)#将点集转化为齐次坐标表示 d1n = d1[ndx] d2n = d2[ndx2] x1n = x1.copy() x2n = x2.copy() figure(figsize=(16,16)) sift.plot_matches(im1, im2, l1, l2, matches, True)#可视化 show() def F_from_ransac(x1, x2, model, maxiter=5000, match_threshold=1e-6): """ 使用RANSAC从点对应中稳健估计基本矩阵F. (来自http://www.scipy.org/Cookbook/RANSAC的ransac.py)。 input: x1, x2 (3*n arrays) points in hom. coordinates. """ from PCV.tools import ransac data = np.vstack((x1, x2)) d = 10 # 20 is the original # 计算F并返回inlier索引 F, ransac_data = ransac.ransac(data.T, model, 8, maxiter, match_threshold, d, return_all=True) return F, ransac_data['inliers']