Exemplo n.º 1
0
 def findGCPs(self, controlimage):
     self.matchtable = sift.match(self.descriptors,
                                  controlimage.descriptors)
     for i in range(len(self.matchtable)):
         if self.matchtable[i] > 0:
             g = GCP(self.locators[i, 0], self.locators[i, 1],
                     controlimage.locators[matchtable(i), 0],
                     controlimage.locators[matchtable(i), 0])
             self.gcps.append(g)
Exemplo n.º 2
0
def find_matches(image_names, root):
    l = {}
    d = {}
    n = len(image_names)
    for i, im in enumerate(image_names):
        resultname = os.path.join(root, '{}.sift'.format(im))
        if not os.path.isfile(resultname):
            sift.process_image(os.path.join(root, '{}.png'.format(im)), resultname)
        l[i], d[i] = sift.read_features_from_file(resultname)

    matches = {}
    for i in range(n - 1):
        matches[i] = sift.match(d[i + 1], d[i])
    return matches, l, d
Exemplo n.º 3
0
imname = glob.glob('out_Photos/IMG_*.jpg')
siftname = [os.path.splitext(im)[0] + '.sift' for im in imname]

tic.k('start')

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

tic.k('loaded')

matches = {}
if not os.path.exists('out_ch03_pano.pickle'):
    for i in range(len(imname) - 1):
        matches[i] = sift.match(d[i + 1], d[i])
        # Slightly better matches, but ransac can handle the worse quality:
        #matches[i] = sift.match_twosided(d[i + 1], d[i])
    pickle.dump(matches, open('out_ch03_pano.pickle', 'wb'))
matches = pickle.load(open('out_ch03_pano.pickle', 'rb'))

tic.k('matched')


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)
    return fp, tp
Exemplo n.º 4
0
res_count = 20

res = [w[1] for w in searcher.query(imlist[query_imid])[:res_count]]
print "regular results for query %d:" % query_imid, res

# Rerank by trying to fit a homography.
q_locs, q_descr = sift.read_features_from_file(featlist[query_imid])
fp = homography.make_homog(q_locs[:, :2].T)

model = homography.RansacModel()

rank = {}
for ndx in res[1:]:
    locs, descr = sift.read_features_from_file(featlist[ndx - 1])  # res is 1-based

    matches = sift.match(q_descr, descr)
    ind = matches.nonzero()[0]
    ind2 = [int(matches[i]) for i in ind]
    tp = homography.make_homog(locs[:, :2].T)

    try:
        H, inliers = homography.H_from_ransac(fp[:, ind], tp[:, ind2], model, match_threshold=4)
    except:
        inliers = []

    rank[ndx] = len(inliers)

sorted_rank = sorted(rank.items(), key=lambda t: t[1], reverse=True)
res_geom = [res[0]] + [s[0] for s in sorted_rank]
print "homography results for query %d" % query_imid, res_geom
l1, d1 = sift.read_or_compute('out_ch4pics/h_image.jpg',
                              'out_ch4pics/h_image.sift')

#figure()
#gray()
im0 = array(Image.open('out_ch4pics/h_template.jpg'))
#sift.plot_features(im0, l0, circle=True)
#
#figure()
#gray()
im1 = array(Image.open('out_ch4pics/h_image.jpg'))
#sift.plot_features(im1, l1, circle=True)
#show()

if not os.path.exists('out_ch04_markerpose.pickle'):
  matches = sift.match(d0, d1)
  pickle.dump(matches, open('out_ch04_markerpose.pickle', 'wb'))
matches = pickle.load(open('out_ch04_markerpose.pickle', 'rb'))

#figure()
#gray()
#sift.plot_matches(im0, im1, l0, l1, matches, show_below=False)
#show()

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)
Exemplo n.º 6
0
l1, d1 = sift.read_or_compute('out_ch4pics/h_image.jpg',
                              'out_ch4pics/h_image.sift')

#figure()
#gray()
im0 = array(Image.open('out_ch4pics/h_template.jpg'))
#sift.plot_features(im0, l0, circle=True)
#
#figure()
#gray()
im1 = array(Image.open('out_ch4pics/h_image.jpg'))
#sift.plot_features(im1, l1, circle=True)
#show()

if not os.path.exists('out_ch04_markerpose.pickle'):
    matches = sift.match(d0, d1)
    pickle.dump(matches, open('out_ch04_markerpose.pickle', 'wb'))
matches = pickle.load(open('out_ch04_markerpose.pickle', 'rb'))

#figure()
#gray()
#sift.plot_matches(im0, im1, l0, l1, matches, show_below=False)
#show()

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)
Exemplo n.º 7
0
Arquivo: 0515.py Projeto: ta-oyama/PCV
ransac.test()

#3.3.2 ロボストなホモグラフィー推定
#SIFT特徴量を用いて対応点を表示する
featname = ['Univ' + str(i+1)+'.sift' for i in range(5)]
imname = ['Univ' + str(i+1)+'.jpg' 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])
    l[i][:,[0,1]] = l[i][:, [1,0]]  #x,y -> row,col
            
matches = {}
for i in range(4):
    matches[i] = sift.match(d[i+1], d[i])

#表示する
im = [np.array(Image.open(imname[i]).convert('L')) for i in range(5)]
for i in range(4):
    plt.figure(i+1)    
    plt.gray()
    sift.plot_matches(im[i+1], im[i], l[i+1], l[i], matches[i],locs_order_xy=False)


# RANSACを対応点に適用
#対応点を同次座標の点に変換する関数
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]
Exemplo n.º 8
0
from triangulation import *
from fmatrix import fmatrix
from rectification import *
from visualization import *
from stereo import *

# Estimate the fundamental matrix between image pairs
#files = array(['TsukubaL.pgm', 'TsukubaR.pgm'])
files = array(['a1.pgm', 'a2.pgm'])
images = array([flipud(imread(f)) for f in files])
w, h = images.shape[2], images.shape[1]

# Detect SIFT features and match the images
sift.detect(files[0], '0.key')
sift.detect(files[1], '1.key')
xy1, xy2 = sift.match('0.key', '1.key')
xy1 = hstack((xy1, ones((len(xy1), 1)))).round()
xy2 = hstack((xy2, ones((len(xy2), 1)))).round()

# Estimate the fundamental matrix
F, inliers = fmatrix(xy1, xy2)

# Correct the projection of the features to sub-pixel accuracy
xy1_corrected, xy2_corrected = correct_projection(F, xy1[inliers],  xy2[inliers])

# Polar rectification
reference_image, F_ft, xy_from, xy_to, rectified = setup_rectification(images, F, xy1_corrected, xy2_corrected)
image_from, image_to = images[1 - reference_image], images[reference_image]

if rectified:
	# Images are already rectified, do nothing
Exemplo n.º 9
0
res = [w[1] for w in searcher.query(imlist[query_imid])[:res_count]]
print 'regular results for query %d:' % query_imid, res

# Rerank by trying to fit a homography.
q_locs, q_descr = sift.read_features_from_file(featlist[query_imid])
fp = homography.make_homog(q_locs[:, :2].T)

model = homography.RansacModel()

rank = {}
for ndx in res[1:]:
    locs, descr = sift.read_features_from_file(featlist[ndx -
                                                        1])  # res is 1-based

    matches = sift.match(q_descr, descr)
    ind = matches.nonzero()[0]
    ind2 = [int(matches[i]) for i in ind]
    tp = homography.make_homog(locs[:, :2].T)

    try:
        H, inliers = homography.H_from_ransac(fp[:, ind],
                                              tp[:, ind2],
                                              model,
                                              match_threshold=4)
    except:
        inliers = []

    rank[ndx] = len(inliers)

sorted_rank = sorted(rank.items(), key=lambda t: t[1], reverse=True)
Exemplo n.º 10
0
import tic
import warp

imname = glob.glob('out_corner/IMG_*.jpg')
siftname = [os.path.splitext(im)[0] + '.sift' for im in imname]

tic.k('start')

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

tic.k('loaded sifts')

if not os.path.exists('out_ch03_ex07_match.pickle'):
    matches = sift.match(d[1], d[0])
    pickle.dump(matches, open('out_ch03_ex07_match.pickle', 'wb'))
matches = pickle.load(open('out_ch03_ex07_match.pickle', 'rb'))

tic.k('matched')

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

tic.k('converted')

im1 = array(Image.open(imname[0]).convert('L'))
im2 = array(Image.open(imname[1]).convert('L'))
if len(im1.shape) == 2:
import warp

imname = glob.glob('out_corner/IMG_*.jpg')
siftname = [os.path.splitext(im)[0] + '.sift' for im in imname]

tic.k('start')

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

tic.k('loaded sifts')


if not os.path.exists('out_ch03_ex06_match.pickle'):
  matches = sift.match(d[1], d[0])
  pickle.dump(matches, open('out_ch03_ex06_match.pickle', 'wb'))
matches = pickle.load(open('out_ch03_ex06_match.pickle', 'rb'))

tic.k('matched')

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


tic.k('converted')

im1 = array(Image.open(imname[0]).convert('L'))
im2 = array(Image.open(imname[1]).convert('L'))
Exemplo n.º 12
0
def get_kpm(img1,img2, mode='string' ,less=0, from_index=-1, to_index=-1):
    # in string mode, img1, img2 parameters are only strings (path to image files) -> testing, presentation, prints out infos
    # in only_match mode, these are ImageObject objects, no prints. 2 params are 2 [loc,desc] lists
    
    if mode=='string':
        print 'Preparing images...'  
        
    im1 = None
    im2 = None
    loc1, desc1 = None, None
    loc2, desc2 = None, None
    # only_match mod eseten a gyorsabb futas miatt csak a match-et nezem, a tobbi erteket elore kiszamolom a kepekre
    if mode=='string':
        img1 = image_module.convert_to_pgm(img1)
        img2 = image_module.convert_to_pgm(img2)
        img1 = make_keypoints(img1)
        img2 = make_keypoints(img2)
        
        loc1, desc1 = get_descriptors(img1)
        loc2, desc2 = get_descriptors(img2)
        
        im1 = get_image_as_array(img1)
        im2 = get_image_as_array(img2)
    elif mode=='only_match':
        loc1 = img1[0]
        desc1 = img1[1]
        loc2 = img2[0] 
        desc2 = img2[1]
    else:
        print 'Not supported mode!'
        exit(-1)
    
    if mode=='string': print '\nSearching for matchings...'
    matchscores = sift.match(desc1, desc2)
    
    bool_indexing = False    
    
    # ezzel a 2 indexxel kivalasztom mettol meddig levo matched keypointokat akarom kirajzolni..atlathatosag
    if from_index>to_index:
        from_index = -1
        to_index = -1
    if from_index!=-1 and to_index!=-1:
        bool_indexing = True
    if bool_indexing:
        if from_index<1: from_index = 1
        if to_index>matchscores.size: to_index = matchscores.size
    
    # because of the 4x4 storing method in lowe's paper
    num_keypoints1 = loc1.size/4
    num_keypoints2 = loc2.size/4
    
    num_matches = nonzero(matchscores)[0].size
    if mode=='string':        
        for counter, m in enumerate(matchscores.tolist()):
            if bool_indexing and (not from_index-1<=counter<=to_index-1):
                matchscores[counter] = np.float64(0.0)
            else:
                # ezzel a parameterrel minden 'less'-edik matchet rajzolja csak ki..atlathatosag miatt ugyancsak                
                if less != 0:
                    if counter % less != 0: matchscores[counter] = np.float64(0.0)
    
    print "Number of matching keypoints:", num_matches
    if mode=='string':
        print "number of displayed matches:", nonzero(matchscores)[0].size
    
    qom, MatchPercent = get_qom(num_keypoints1, num_keypoints2, num_matches)
    print "Quality of match: ", qom, '\n'
    
    return im1, im2, matchscores,qom,MatchPercent
Exemplo n.º 13
0
 def findGCPs(self, controlimage):
     self.matchtable = sift.match(self.descriptors, controlimage.descriptors)
     for i in range(len(self.matchtable)):
         if self.matchtable[i] > 0:
             g = GCP(self.locators[i,0], self.locators[i,1], controlimage.locators[matchtable(i),0] , controlimage.locators[matchtable(i),0])
             self.gcps.append(g)
Exemplo n.º 14
0
l, d = {}, {}
for i in range(len(imname)):
  l[i], d[i] = sift.read_or_compute(imname[i], siftname[i])

  # Only use features from the top of the image:
  #bettera, betterb = [], []
  #for j in range(len(l[i])):
  #  if l[i][j][1] < 900:
  #    bettera.append(l[i][j])
  #    betterb.append(d[i][j])
  #l[i] = array(bettera)
  #d[i] = array(betterb)

tic.k('loaded')

matches = [sift.match(d[1], d[0])]
#matches = [sift.match_twosided(d[1], d[0])]  # Not needed with ransac.

tic.k('matched')

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)
  return fp, tp

model = homography.RansacModel()

fp, tp = convert_points(0)
Exemplo n.º 15
0
    
    img_height, img_width, depth = image.shape
    scale_w = w / img_width
    scale_h = h / img_height
    image = cv2.resize(image, (0,0), fx=scale_w, fy=scale_h)
    image_cp = image.copy()

    grey_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

    #template = grey_postit_img
    #template_corrds = np.float32([[0,0],[0,h],[w,h],[w,0] ]).reshape(-1,1,2)
    
    template = rect
    template_corrds = np.float32([[60,40],[60,280],[180,280],[180,40]]).reshape(-1,1,2)
    
    (matches, kp1_img, kp2_img, img3, M, centroid) = sift.match(template, grey_image)

    #if M is not None:
    #	dst = cv2.perspectiveTransform(template_corrds, M)
    #	img2 = cv2.polylines(grey_image,[np.int32(dst)],True,255)
    #	cv2.imshow('detected', img2)
    	
    if centroid is not None:
    	img2 = cv2.circle(image_cp, centroid, 20, (0, 0, 255), 1)
    	cv2.imshow('detected', img2) 
	
	# Compose 2x2 grid with all previews
    grid = np.zeros([2*h, 2*w, 3], np.uint8)
    grid[0:h, 0:w] = np.dstack([kp1_img])
    # We need to convert each of them to RGB from grescaled 8 bit format
    grid[0:h, w:2*w] = np.dstack([kp2_img])
Exemplo n.º 16
0
from triangulation import *
from fmatrix import fmatrix
from rectification import *
from visualization import *
from stereo import *

# Estimate the fundamental matrix between image pairs
#files = array(['TsukubaL.pgm', 'TsukubaR.pgm'])
files = array(['a1.pgm', 'a2.pgm'])
images = array([flipud(imread(f)) for f in files])
w, h = images.shape[2], images.shape[1]

# Detect SIFT features and match the images
sift.detect(files[0], '0.key')
sift.detect(files[1], '1.key')
xy1, xy2 = sift.match('0.key', '1.key')
xy1 = hstack((xy1, ones((len(xy1), 1)))).round()
xy2 = hstack((xy2, ones((len(xy2), 1)))).round()

# Estimate the fundamental matrix
F, inliers = fmatrix(xy1, xy2)

# Correct the projection of the features to sub-pixel accuracy
xy1_corrected, xy2_corrected = correct_projection(F, xy1[inliers],
                                                  xy2[inliers])

# Polar rectification
reference_image, F_ft, xy_from, xy_to, rectified = setup_rectification(
    images, F, xy1_corrected, xy2_corrected)
image_from, image_to = images[1 - reference_image], images[reference_image]