Exemple #1
0
    def match(image1, image2, visualize=False):
        """ Match features in 2 images """

        print('comparing ', image1, image2)
        locs1, desc1 = sift.read_features_from_file(
            SiftRunner.__get_sift_id__(image1))
        locs2, desc2 = sift.read_features_from_file(
            SiftRunner.__get_sift_id__(image2))
        scores = sift.match_twosided(desc1, desc2)

        if visualize:
            im1 = array(Image.open(image1).convert('L'))
            im2 = array(Image.open(image2).convert('L'))

            fig = figure()
            grid = GridSpec(2, 2)
            fig.add_subplot(grid[0, 0])
            sift.plot_feature(im1, locs1)
            fig.add_subplot(grid[0, 1])
            sift.plot_feature(im2, locs2)
            fig.add_subplot(grid[1, :])
            sift.plot_matches(im1, im2, locs1, locs2, scores, show_below=False)
            show()

        return scores
Exemple #2
0
def get_krt(im1, im2):
    ims = [im1, im2]
    sifts = []
    for x in range(2):
        sifts.append(ims[x][:-3]+"sift")

    # compute features                                                        
    #sift.process_image('../../data/book_frontal.JPG','../../data/im0.sift')
    sift.process_image(ims[0],sifts[0])

    l0,d0 = sift.read_features_from_file(sifts[0])
    #sift.process_image('../../data/book_perspective.JPG','../../data/im1.sift')
    sift.process_image(ims[1],sifts[1])
    l1,d1 = sift.read_features_from_file(sifts[1])
    # 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]
    print len(ndx2)
    tp = homography.make_homog(l1[ndx2,:2].T)
    model = homography.RansacModel()
    H,ransac_data = homography.H_from_ransac(fp,tp,model)


    # camera calibration
    #K = camera.my_calibration((747,1000))
    K = camera.my_calibration((Image.open(im2).size))
    # 3D points at plane z=0 with sides of length 0.2
    box = cube.cube_points([0,0,0.1],0.1)
    # project bottom square in first image
    cam1 = camera.Camera( hstack((K,dot(K,array([[0],[0],[-1]])) )) )
    # first points are the bottom square
    box_cam1 = cam1.project(homography.make_homog(box[:,:5]))
    # use H to transfer points to the second image
    print dot(H,box_cam1)
    box_trans = homography.normalize(dot(H,box_cam1))
    # compute second camera matrix from cam1 and H
    cam2 = camera.Camera(dot(H,cam1.P))
    A = dot(linalg.inv(K),cam2.P[:,:3])
    A = array([A[:,0],A[:,1],cross(A[:,0],A[:,1])]).T
    cam2.P[:,:3] = dot(K,A)
    # project with the second camera
    box_cam2 = cam2.project(homography.make_homog(box))
    # test: projecting point on z=0 should give the same
    point = array([1,1,0,1]).T
    print homography.normalize(dot(dot(H,cam1.P),point))
    print cam2.project(point)

    import pickle
    with open('%s.pkl' % ims[1][:-4],'w') as f:
        pickle.dump(K,f)
        pickle.dump(dot(linalg.inv(K),cam2.P),f)
    sys.stderr.write("K and Rt dumped to %s.pkl\n" % ims[1][:-4])
Exemple #3
0
def get_krt(im1, im2):
    ims = [im1, im2]
    sifts = []
    for x in range(2):
        sifts.append(ims[x][:-3] + "sift")

    # compute features
    #sift.process_image('../../data/book_frontal.JPG','../../data/im0.sift')
    sift.process_image(ims[0], sifts[0])

    l0, d0 = sift.read_features_from_file(sifts[0])
    #sift.process_image('../../data/book_perspective.JPG','../../data/im1.sift')
    sift.process_image(ims[1], sifts[1])
    l1, d1 = sift.read_features_from_file(sifts[1])
    # 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]
    print len(ndx2)
    tp = homography.make_homog(l1[ndx2, :2].T)
    model = homography.RansacModel()
    H, ransac_data = homography.H_from_ransac(fp, tp, model)

    # camera calibration
    #K = camera.my_calibration((747,1000))
    K = camera.my_calibration((Image.open(im2).size))
    # 3D points at plane z=0 with sides of length 0.2
    box = cube.cube_points([0, 0, 0.1], 0.1)
    # project bottom square in first image
    cam1 = camera.Camera(hstack((K, dot(K, array([[0], [0], [-1]])))))
    # first points are the bottom square
    box_cam1 = cam1.project(homography.make_homog(box[:, :5]))
    # use H to transfer points to the second image
    print dot(H, box_cam1)
    box_trans = homography.normalize(dot(H, box_cam1))
    # compute second camera matrix from cam1 and H
    cam2 = camera.Camera(dot(H, cam1.P))
    A = dot(linalg.inv(K), cam2.P[:, :3])
    A = array([A[:, 0], A[:, 1], cross(A[:, 0], A[:, 1])]).T
    cam2.P[:, :3] = dot(K, A)
    # project with the second camera
    box_cam2 = cam2.project(homography.make_homog(box))
    # test: projecting point on z=0 should give the same
    point = array([1, 1, 0, 1]).T
    print homography.normalize(dot(dot(H, cam1.P), point))
    print cam2.project(point)

    import pickle
    with open('%s.pkl' % ims[1][:-4], 'w') as f:
        pickle.dump(K, f)
        pickle.dump(dot(linalg.inv(K), cam2.P), f)
    sys.stderr.write("K and Rt dumped to %s.pkl\n" % ims[1][:-4])
Exemple #4
0
def sift_matrix():
    featurelist = sift_pan_desc_generator('/home/aurora/hdd/workspace/PycharmProjects/data/pcv_img/panoimages/')
    imlist = getFiles('/home/aurora/hdd/workspace/PycharmProjects/data/pcv_img/panoimages/')
    nbr_images = len(imlist)
    matchscores = np.zeros((nbr_images, nbr_images))
    for i in range(nbr_images):
        for j in range(i, nbr_images):
            print 'comparing ', imlist[i], imlist[j]
            l1, d1 = sift.read_feature_from_file(featurelist[i])
            l2, d2 = sift.read_feature_from_file(featurelist[j])

            matches = sift.match_twosided(d1, d2)
            nbr_matches = sum(matches > 0)
            print 'number of matches = ', nbr_matches
            matchscores[i, j] = nbr_matches
    for i in range(nbr_images):
        for j in range(i + 1, nbr_images):
            matchscores[j, i] = matchscores[i, j]
    np.save('pan_img_matchscore', matchscores)
Exemple #5
0
def sift_matrix():
    featurelist = sift_pan_desc_generator('/home/aurora/hdd/workspace/PycharmProjects/data/pcv_img/panoimages/')
    imlist = getFiles('/home/aurora/hdd/workspace/PycharmProjects/data/pcv_img/panoimages/')
    nbr_images = len(imlist)
    matchscores = np.zeros((nbr_images, nbr_images))
    for i in range(nbr_images):
        for j in range(i, nbr_images):
            print 'comparing ', imlist[i], imlist[j]
            l1, d1 = sift.read_feature_from_file(featurelist[i])
            l2, d2 = sift.read_feature_from_file(featurelist[j])

            matches = sift.match_twosided(d1, d2)
            nbr_matches = sum(matches > 0)
            print 'number of matches = ', nbr_matches
            matchscores[i, j] = nbr_matches
    for i in range(nbr_images):
        for j in range(i + 1, nbr_images):
            matchscores[j, i] = matchscores[i, j]
    np.save('pan_img_matchscore', matchscores)
Exemple #6
0
def get_H(im0_path, im1_path):
    """
    Get the Homography matrix.
    """
    # compute features
    sift.process_image(im0_path, 'im0.sift')
    l0, d0 = sift.read_features_from_file('im0.sift')

    sift.process_image(im1_path, '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 = homography.H_from_ransac(fp, tp, model)[0]

    return H
Exemple #7
0
def compute_homography():
    # compute features
    #sift.process_image('../../data/book_frontal.JPG','../../data/im0.sift')
    im1='../../data/space_front.jpg'
    im2='../../data/space_perspective.jpg'
    im1='../../data/mag_front.jpg'
    im2='../../data/mag_perspective.jpg'
    ims = [im1, im2]
    sifts = []
    for k in range(2):
        sifts.append(ims[k][:-4]+".sift")
    l0,d0 = sift.read_features_from_file(sifts[0])
    l1,d1 = sift.read_features_from_file(sifts[1])

    # 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,ransac_data = homography.H_from_ransac(fp,tp,model)
    return H
Exemple #8
0
def _match():
    imlist = []
    featlist = []
    nbr_image = len(imlist)
    
    matchscores = zeros((nbr_images, nbr_images))
    
    for i in xrange(nbr_images):
        for j in xrange(i, nbr_image): # 上三角成分だけを計算する
            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 or matches = ', nbr_matches
            matchscores[j,i] = nbr_matches
        
    # 値をコピーする
    for i in xrange(nbr_images):
        for j in xrange(i+1, nbr_images): # 対角成分はコピー不要
            matchscores[j,i] = matchscores[i,j]
Exemple #9
0
def compute_homography():
    # compute features
    #sift.process_image('../../data/book_frontal.JPG','../../data/im0.sift')
    im1 = '../../data/space_front.jpg'
    im2 = '../../data/space_perspective.jpg'
    im1 = '../../data/mag_front.jpg'
    im2 = '../../data/mag_perspective.jpg'
    ims = [im1, im2]
    sifts = []
    for k in range(2):
        sifts.append(ims[k][:-4] + ".sift")
    l0, d0 = sift.read_features_from_file(sifts[0])
    l1, d1 = sift.read_features_from_file(sifts[1])

    # 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, ransac_data = homography.H_from_ransac(fp, tp, model)
    return H
import homography 
import sfm
import sift
from numpy import *
from matplotlib.pylab import *
# calibration
K = array([[2394,0,932],[0,2398,628],[0,0,1]])
print "load images and compute features"
im1 = array(Image.open('../data/alcatraz1.jpg')) 
sift.process_image('../data/alcatraz1.jpg','im1.sift')
l1,d1 = sift.read_features_from_file('im1.sift')
im2 = array(Image.open('../data/alcatraz2.jpg'))
sift.process_image('../data/alcatraz2.jpg','im2.sift')
l2,d2 = sift.read_features_from_file('im2.sift')
print "match features"
matches = sift.match_twosided(d1,d2)
ndx = matches.nonzero()[0]
# make homogeneous and normalize with inv(K)
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 = dot(inv(K),x1)
x2n = dot(inv(K),x2)
print "estimate E with RANSAC"
model = sfm.RansacModel()
E,inliers = sfm.F_from_ransac(x1n,x2n,model)
print "compute camera matrices (P2 will be list of four solutions)"
P1 = array([[1,0,0,0],[0,1,0,0],[0,0,1,0]]) 
P2 = sfm.compute_P_from_essential(E)
print "pick the solution with points in front of cameras"
ind = 0
# 100 to 170 (which helps quality, but also slows down the program a lot, from
# from 20s to 60s):
# (with twosided matching, matches go from 85 to 113 for out_corner)
# NOTE: delete caches after changing this!
histeq = False

l, d = {}, {}
for i in range(len(imname)):
  l[i], d[i] = sift.read_or_compute(imname[i], siftname[i], histeq)

tic.k('loaded sifts')

print '{} / {} features'.format(len(d[0]), len(d[1]))
if not os.path.exists('out_ch05_recover_match.pickle'):
  #matches = sift.match(d[0], d[1])
  matches = sift.match_twosided(d[0], d[1])
  pickle.dump(matches, open('out_ch05_recover_match.pickle', 'wb'))
matches = pickle.load(open('out_ch05_recover_match.pickle', 'rb'))

tic.k('matched')

ndx = matches.nonzero()[0]
x1 = homography.make_homog(l[0][ndx, :2].T)
ndx2 = [int(matches[i]) for i in ndx]
x2 = homography.make_homog(l[1][ndx2, :2].T)

print '{} matches'.format(len(ndx))

image = [numpy.array(Image.open(name)) for name in imname]

# calibration (FIXME?)
Exemple #12
0
    fy = 2586 * row / 1936
    K = diag([fx, fy, 1])
    K[0, 2] = 0.5 * col
    K[1, 2] = 0.5 * row
    return K


sift.process_image('book_frontal.JPG', 'im0.sift')
l0, d0 = sift.read_features_from_file('im0.sift')
sift.process_image('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)  #vstack
ndx2 = [int(matches[i]) for i in ndx]
tp = homography.make_homog(l1[ndx2, :2].T)  #vstack

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
Exemple #13
0
# 100 to 170 (which helps quality, but also slows down the program a lot, from
# from 20s to 60s):
# (with twosided matching, matches go from 85 to 113 for out_corner)
# NOTE: delete caches after changing this!
histeq = False

l, d = {}, {}
for i in range(len(imname)):
    l[i], d[i] = sift.read_or_compute(imname[i], siftname[i], histeq)

tic.k('loaded sifts')

print '{} / {} features'.format(len(d[0]), len(d[1]))
if not os.path.exists('out_ch05_recover_match.pickle'):
    #matches = sift.match(d[0], d[1])
    matches = sift.match_twosided(d[0], d[1])
    pickle.dump(matches, open('out_ch05_recover_match.pickle', 'wb'))
matches = pickle.load(open('out_ch05_recover_match.pickle', 'rb'))

tic.k('matched')

ndx = matches.nonzero()[0]
x1 = homography.make_homog(l[0][ndx, :2].T)
ndx2 = [int(matches[i]) for i in ndx]
x2 = homography.make_homog(l[1][ndx2, :2].T)

print '{} matches'.format(len(ndx))

image = [numpy.array(Image.open(name)) for name in imname]

# calibration (FIXME?)
imname = 'book_test.jpg'
im1 = array(Image.open(imname).convert('L'))
l1, d1 = sift.read_features_from_file('im1.sift')

figure()
gray()
sift.plot_features(im1, l1, circle=True)
show()





# 匹配特征
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)

print 'fp', fp
print 'tp', tp

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

# 计算相机标定矩阵
K = my_calibration((747, 1000))

# 位于边长为0.2, z=0平面上的三维点
from pylab import *
from numpy import *
from PIL import Image

import sift

imname1 = 'C:\Users\HASEE\Desktop\climbing_1_small.jpg'
imname2 = 'C:\Users\HASEE\Desktop\climbing_2_small.jpg'

# 处理并将结果保存到文件中
sift.process_image(imname1, imname1 + '.sift')
sift.process_image(imname2, imname2 + '.sift')

# 读取特征进行匹配
l1, d1 = sift.read_features_from_file(imname1 + '.sift')
l2, d2 = sift.read_features_from_file(imname2 + '.sift')
matchscores = sift.match_twosided(d1, d2)

# 加载并会图
im1 = array(Image.open(imname1))
im2 = array(Image.open(imname2))

sift.plot_matches(im1, im2, l1, l2, matchscores, show_below=True)
show()
Exemple #16
0
# load images and compute features
#im1_path = 'C:/Users/User/Desktop/course/3D/project/pcv_data/alcatraz1.jpg'
#im2_path = 'C:/Users/User/Desktop/course/3D/project/pcv_data/alcatraz2.jpg'
im1_path = './e1.jpg'
im2_path = './e2.jpg'

im1 = np.array(Image.open(im1_path))
sift.process_image(im1_path, 'im1.sift')
l1, d1 = sift.read_features_from_file('im1.sift')

im2 = np.array(Image.open(im2_path))
sift.process_image(im2_path, 'im2.sift')
l2, d2 = sift.read_features_from_file('im2.sift')

# match features
matches = sift.match_twosided(d1, d2)
ndx = matches.nonzero()[0]

# make homogeneous and normalize with inv(K)
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)

# estimate E with RANSAC
model = sfm.RansacModel()
E, inliers = sfm.F_from_ransac(x1n, x2n, model)

# compute camera matrices (P2 will be list of four solutions)
from numpy import *

import sift
imname = 'baby_1.jpg'
im1 = array(Image.open(imname).convert('L'))
imshow(im1)
sift.process_image(imname, 'baby.sift')
l1, d1 = sift.read_features_from_file('baby.sift')
figure()
gray()
sift.plot_features(im1, l1, circle=True)
show()
imname1 = 'climbing_1_small.jpg'
imname2 = '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
l2, d2 = sift.read_features_from_file(imname1 + '.sift')
l3, d3 = sift.read_features_from_file(imname2 + '.sift')
matchscores = sift.match_twosided(d2, d3)

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

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