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
This is the example graph illustration of matching images from Figure 2-10. To download the images, see ch2_download_panoramio.py. """ download_path = "panoimages" # set this to the path where you downloaded the panoramio images path = "C:\Users\SW\PycharmProjects\geotag\panoimages" # path to save thumbnails (pydot needs the full system path) # 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): if os.path.isfile(imname) == False: # File exist test 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):
from PIL import Image from numpy import * from pylab import * import numpy as np from PCV.geometry import homography, camera,sfm from PCV.localdescriptors import sift import importlib camera = importlib.reload(camera) homography = importlib.reload(homography) sfm = importlib.reload(sfm) sift = importlib.reload(sift) # 提取特征,注意读取图片的顺序! im1 = array(Image.open('D:/study/machine_learning/images/yosemite2.jpg')) sift.process_image('D:/study/machine_learning/images/yosemite2.jpg', 'im1.sift') im2 = array(Image.open('D:/study/machine_learning/images/yosemite1.jpg')) sift.process_image('D:/study/machine_learning/images/yosemite1.jpg', 'im2.sift') l1, d1 = sift.read_features_from_file('im1.sift') 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]
# -*- coding: utf-8 -*- from PIL import Image from pylab import * from PCV.localdescriptors import sift from PCV.localdescriptors import harris # 添加中文字体支持 from matplotlib.font_manager import FontProperties font = FontProperties(fname=r"c:\windows\fonts\SimSun.ttc", size=14) imname = '../data/empire.jpg' im = array(Image.open(imname).convert('L')) sift.process_image(imname, 'empire.sift') l1, d1 = sift.read_features_from_file('empire.sift') figure() gray() subplot(131) sift.plot_features(im, l1, circle=False) title(u'SIFT特征',fontproperties=font) subplot(132) sift.plot_features(im, l1, circle=True) title(u'用圆圈表示SIFT特征尺度',fontproperties=font) # 检测harris角点 harrisim = harris.compute_harris_response(im) subplot(133) filtered_coords = harris.get_harris_points(harrisim, 6, 0.1) imshow(im) plot([p[1] for p in filtered_coords], [p[0] for p in filtered_coords], '*')
# If you have PCV installed, these imports should work 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 process_image(filename): sift_filename = get_sift_filename(filename) if not os.path.exists(sift_filename): sift.process_image(filename, sift_filename) return sift_filename
""" This is the example graph illustration of matching images from Figure 2-10. To download the images, see ch2_download_panoramio.py. """ download_path = "panoimages" # set this to the path where you downloaded the panoramio images path = "/FULLPATH/panoimages/" # path to save thumbnails (pydot needs the full system path) # 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):
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)
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()
''' from PIL import Image from pylab import * import sfm from PCV.geometry import camera, homography from PCV.localdescriptors import sift import tic # calibration K = array([[2394,0,932],[0,2398,628],[0,0,1]]) tic.k('start') # load images and compute featuers im1 = array(Image.open('./images/salcatraz1.jpg')) sift.process_image('./images/salcatraz1.jpg','./images/salcatraz1.sift') l1,d1 = sift.read_features_from_file('./images/salcatraz1.sift') im2 = array(Image.open('./images/salcatraz2.jpg')) sift.process_image('./images/salcatraz2.jpg','./images/salcatraz2.sift') l2,d2 = sift.read_features_from_file('./images/salcatraz2.sift') tic.k('loadd sifts') print '{} / {} features'.format(len(d1), len(d2)) # match features matches = sift.match_twosided(d1,d2) ndx = matches.nonzero()[0] tic.k('matched')
from PCV.geometry import homography, sfm from PCV.localdescriptors import sift import numpy as np from PIL import Image from pylab import * K = np.array([[2394, 0, 932], [0, 2398, 628], [0, 0, 1]]) imname = '../data/alcatraz1.jpg' im2name = '../data/alcatraz2.jpg' im1 = np.array(Image.open(imname)) sift.process_image(imname, 'im1.sift') l1, d1 = sift.read_features_from_file('im1.sift') im2 = np.array(Image.open(im2name)) sift.process_image(im2name, 'im2.sift') 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) x1n = np.dot(np.linalg.inv(K), x1) x2n = np.dot(np.linalg.inv(K), x2) model = sfm.RansacModel() E, inliners = sfm.F_from_ransac(x1n, x2n, model)
# -*- coding: utf-8 -*- from PIL import Image from pylab import * from PCV.localdescriptors import sift from PCV.localdescriptors import harris # 添加中文字体支持 from matplotlib.font_manager import FontProperties font = FontProperties(fname=r"c:\windows\fonts\SimSun.ttc", size=14) imname = '../data/empire.jpg' im = array(Image.open(imname).convert('L')) sift.process_image(imname, 'empire.sift') l1, d1 = sift.read_features_from_file('empire.sift') figure() gray() subplot(131) sift.plot_features(im, l1, circle=False) title(u'SIFT特征', fontproperties=font) subplot(132) sift.plot_features(im, l1, circle=True) title(u'用圆圈表示SIFT特征尺度', fontproperties=font) # 检测harris角点 harrisim = harris.compute_harris_response(im) subplot(133) filtered_coords = harris.get_harris_points(harrisim, 6, 0.1) imshow(im) plot([p[1] for p in filtered_coords], [p[0] for p in filtered_coords], '*')
# compute features # sift.process_image('./data/simage10.jpg','./data/simage10.sift') # l0,d0 = sift.read_features_from_file('./data/simage10.sift') # # sift.process_image('./data/simage11.jpg','./data/simage11.sift') # l1,d1 = sift.read_features_from_file('./data/simage11.sift') # sift.process_image('./data/image1.JPG', './data/im2.sift') # l0,d0 = sift.read_features_from_file('./data/im2.sift') # # sift.process_image('./data/image2.JPG', './data/im3.sift') # l1,d1 = sift.read_features_from_file('./data/im3.sift') sift.process_image('./data/s20160720_113416.JPG', './data/im2.sift') l0,d0 = sift.read_features_from_file('./data/im2.sift') sift.process_image('./data/s20160720_113436.JPG', './data/im3.sift') l1,d1 = sift.read_features_from_file('./data/im3.sift') # match features and estimate homography matches = sift.match_twosided(d0,d1) # im0 = array(Image.open('./data/book_frontal.JPG')) # im1 = array(Image.open('./data/book_perspective.JPG')) # im0 = array(Image.open('./data/simage10.JPG')) # im1 = array(Image.open('./data/simage11.JPG')) # figure() # sift.plot_matches(im0,im1,l0,l1, matches, show_below=True) # show()
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()
from PCV.localdescriptors import sift if len(sys.argv) >= 3: im1f, im2f = sys.argv[1], sys.argv[2] else: im1f = r'e:\Study\pythonxyProject\sift\images/02.jpg' im2f = r'e:\Study\pythonxyProject\sift\images/04.jpg' # im1f = '../data/crans_1_small.jpg' # im2f = '../data/crans_2_small.jpg' # im1f = '../data/climbing_1_small.jpg' # im2f = '../data/climbing_2_small.jpg' im1 = array(Image.open(im1f)) im2 = array(Image.open(im2f)) sift.process_image(im1f, 'out_sift_1.txt') l1, d1 = sift.read_features_from_file('out_sift_1.txt') figure() gray() subplot(121) sift.plot_features(im1, l1, circle=False) sift.process_image(im2f, 'out_sift_2.txt') l2, d2 = sift.read_features_from_file('out_sift_2.txt') subplot(122) sift.plot_features(im2, l2, circle=False) matches = sift.match(d1, d2) matches = sift.match_twosided(d1, d2) print '{} matches'.format(len(matches.nonzero()[0]))
import glob import os from PCV.localdescriptors import sift for f in glob.glob('out/*.jpg'): base = os.path.splitext(os.path.basename(f))[0] siftpath = os.path.join('out', base + '.sift') if os.path.exists(siftpath): continue print 'processing', f sift.process_image(f, siftpath)
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()
@author: liaoyuhua ''' from PIL import Image from numpy import * from pylab import * from scipy import * from PCV.localdescriptors import sift from PCV.geometry import homography,warp if __name__ == '__main__': featname = [r'D:\Python_Computer_Vision\data\Univ'+str(i+1)+'.sift' for i in range(5)] imname = [r'D:\Python_Computer_Vision\data\Univ'+str(i+1)+'.png' for i in range(5)] 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]) # function to convert the matches to hom. points def convert_points(j): ndx = matches[j].nonzero()[0] print array([l[j+1][ndx,1],l[j+1][ndx,0]],'f') fp = homography.make_homog(array([l[j+1][ndx,1],l[j+1][ndx,0]],'f')) ndx2 = [int(matches[j][i]) for i in ndx] tp = homography.make_homog(array([l[j][ndx2,1],l[j][ndx2,0]],'f')) return fp,tp # estimate the homographies model = homography.RansacModel() fp,tp = convert_points(1)
# -*- coding: utf-8 -*- import pickle from PCV.imagesearch import vocabulary from PCV.tools.imtools import get_imlist from PCV.localdescriptors import sift #获取图像列表 imlist = get_imlist('./first500/') nbr_images = len(imlist) #获取特征列表 featlist = [imlist[i][:-3] + 'sift' for i in range(nbr_images)] #提取文件夹下图像的sift特征 for i in range(nbr_images): sift.process_image(imlist[i], featlist[i]) #生成词汇 voc = vocabulary.Vocabulary('ukbenchtest') voc.train(featlist, 1000, 10) #保存词汇 # saving vocabulary with open('./first500/vocabulary.pkl', 'wb') as f: pickle.dump(voc, f) print('vocabulary is:', voc.name, voc.nbr_words)
# -*- coding: utf-8 -*- import pickle from PCV.imagesearch import vocabulary from PCV.tools.imtools import get_imlist from PCV.localdescriptors import sift imlist = get_imlist('./first500/') nbr_images = len(imlist) featlist = [imlist[i][:-3]+'sift' for i in range(nbr_images)] for i in range(nbr_images): sift.process_image(imlist[i], featlist[i]) voc = vocabulary.Vocabulary('ukbench') voc.train(featlist, 1000, 10) # saving vocabulary with open('./first500/vocabulary.pkl', 'wb') as f: pickle.dump(voc, f) print 'vocabulary is:', voc.name, voc.nbr_words
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)
import glob import os from PCV.localdescriptors import sift for f in glob.glob('out/*.jpg'): base = os.path.splitext(os.path.basename(f))[0] siftpath = os.path.join('out', base + '.sift') if os.path.exists(siftpath): continue print('processing', f) sift.process_image(f, siftpath)
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()
import os root = "D:\\homework\\homework\\house\\11\\" """ This is the panorama example from section 3.3. """ # set paths to data folder featname = ['../data/wanren/uu' + str(i + 1) + '.sift' for i in range(5)] imname = ['../data/wanren/uu' + str(i + 1) + '.jpg' for i in range(5)] # extract features and match 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
from PCV.geometry import sfm, homography from matplotlib.pyplot import plot, axis, show, imshow, figure, gray from PCV.geometry import camera from numpy import array, loadtxt, genfromtxt, ones, vstack, dot from PIL import Image # calibration from PCV.localdescriptors import sift K = array([[2394, 0, 932], [0, 2398, 628], [0, 0, 1]]) print "computing sift.." # load images and compute features im1 = array(Image.open('../dataset_alcatraz/alcatraz1.jpg')) sift.process_image('../dataset_alcatraz/alcatraz1.jpg', '../dataset_alcatraz/alcatraz1.sift') l1, d1 = sift.read_features_from_file('dataset_alcatraz/alcatraz1.sift') im2 = array(Image.open('../dataset_alcatraz/alcatraz2.jpg')) sift.process_image('../dataset_alcatraz/alcatraz2.jpg', '../dataset_alcatraz/alcatraz2.sift') l2, d2 = sift.read_features_from_file('../dataset_alcatraz/alcatraz2.sift') print "sift.. done" print "match features..." # match features print "\tsearching matches" matches = sift.match_twosided(d1, d2) ndx = matches.nonzero()[0] # make homogeneous and normalize with inv(K)