Example #1
0
 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
Example #2
0
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
Example #3
0
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
Example #4
0
 def bof_image_retrieval(self):
     # load vocabulary and query feature
     src = self.image_searcher()
     q_descr, fp = self.load_query_feature()
     # RANSAC model for homography fitting
     model = homography.RansacModel()
     rank = {}
     # query
     match_scores = [
         w[0] for w in src.query(self.imlist[q_ind])[:nbr_results]
     ]
     res_reg = [w[1] for w in src.query(self.imlist[q_ind])[:nbr_results]]
     print('top matches:', res_reg)
     self.plot_results(res_reg[:6], match_scores[:6])
     if self.bof_rearrange:
         # load image features for result
         for ndx in res_reg[1:]:
             locs, descr = sift.read_features_from_file(self.featlist[ndx])
             # get matches
             matches = sift.match(q_descr, descr)
             ind = matches.nonzero()[0]
             ind2 = matches[ind]
             locs = np.array(locs)
             tp = homography.make_homog(locs[:, :2].T)
             # compute homography, count inliers.
             try:
                 H, inliers = homography.H_from_ransac(fp[:, ind],
                                                       tp[:, ind2],
                                                       model,
                                                       match_theshold=4)
             except:
                 inliers = []
             # store inlier count
             rank[ndx] = len(inliers)
             # sort dictionary to get the most inliers first
             sorted_rank = sorted(rank.items(),
                                  key=lambda t: t[1],
                                  reverse=True)
             res_geom = [res_reg[0]] + [s[0] for s in sorted_rank]
         # print('top matches (homography):', res_geom)
         # show results
         self.plot_results(res_geom[:6], match_scores[:6])
Example #5
0
    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
cam1 = camera.Camera(hstack((K, dot(K, array([[0], [0], [-1]])))))
# first points are the bottom square
Example #6
0
    data = np.vstack((x1, x2))
    d = 20 # 20 is the original
    F, ransac_data = ransac.ransac(data.T, model,
                                   18, maxiter, match_threshold, d, return_all=True)
    return F, ransac_data['inliers']
output_path = r'\\192.168.104.99\double\2020-09-15\151104\output\debug'
X1_list, X2_list = loaddata(output_path)

X1_array = np.array(X1_list).reshape(-1, 2)
X2_array = np.array(X2_list).reshape(-1, 2)
left_array = load_pickle_file(os.path.join(output_path, 'left_camera_array_{}.pkl'.format(0)))
right_array = load_pickle_file(os.path.join(output_path, 'right_camera_array_{}.pkl'.format(0)))
middle_ind = load_pickle_file(os.path.join(output_path, 'middle_camera_ind_{}.pkl'.format(0)))
x1 = left_array[middle_ind, :]
x2 = right_array[middle_ind, :]
x1n = homography.make_homog(x1.T)
x2n = homography.make_homog(x2.T)
# x1n = homography.make_homog(left_array[middle_ind, :].T)
# x2n = homography.make_homog(right_array[middle_ind, :].T)
model = sfm.RansacModel()
while True:
    try:
        F, inliers = F_from_ransac(x1n, x2n, model, maxiter=5000, match_threshold=0.1)
    except Exception as e:
        print(e)
        continue
    # print("the length middle ind is {}".format(len(middle_ind)))
    print("inleiers is {}".format(inliers))
    fx = 640
    fy = 640
    cx = 480
    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
cam1 = camera.Camera(hstack((K, dot(K, array([[0], [0], [-1]])))))
# first points are the bottom square
Example #8
0
imlist = [os.path.join(path, f) for f in os.listdir(path)]
imnbr = len(imlist)
featlist = [imlist[i].split('\\')[-1][:-3] + 'sift' for i in range(imnbr)]
with open('vocabulary.pkl', 'rb') as f:
    voc = pickle.load(f)

src = imagesearch.Searcher('testImgAdd.db', voc)

q_ind = 0
nbr_results = 20

res_reg = [w[1] for w in src.query(imlist[q_ind])[:nbr_results]]
print 'top matches (regular) : ', res_reg

q_locs, q_descr = sift.read_features_from_file(featlist[q_ind])
fp = homography.make_homog(q_locs[:, :2].T)

model = homography.RansacModel()
rank = {}

for ndx in res_reg[1:]:
    locs, descr = sift.read_features_from_file(featlist[ndx])
    matches = sift.match(q_descr, descr)
    ind = matches.nonzero()[0]
    ind2 = matches[ind]
    tp = homography.make_homog(locs[:, :2].T)
    try:
        H, inliners = homography.H_from_ransac(fp[:, ind],
                                               tp[:, ind2],
                                               model,
                                               match_theshold=4)
Example #9
0
# We use OpenCV instead of the calculus of the homography present in the book
H = find_homography(kp0, desc0, kp1, desc1)

# Note: always resize image to 747 x 1000 or change the K below

# 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
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
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))

# plotting
im0 = array(Image.open(img0_name))
Example #10
0
from pylab import *
from numpy import *
from PIL import Image
from PCV.geometry import camera, homography
from PCV.localdescriptors import sift

# compute features
sift.process_image('./data/book_frontal.JPG', './data/im0.sift')
l0,d0 = sift.read_features_from_file('./data/im0.sift')

sift.process_image('./data/book_perspective.JPG', './data/im1.sift')
l1,d1 = sift.read_features_from_file('./data/im1.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)
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)

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]
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.
src = imagesearch.Searcher('web.db',voc)

# 查询图线索引和返回的图像数
# index of query image and number of results to return
q_ind = 0
nbr_results = 20

# 常规查询
# regular query
res_reg = [w[1] for w in src.query(imlist[q_ind])[:nbr_results]]
print 'top matches (regular):', res_reg  # res_reg保存的是候选图像(欧式距离)

# 载入查询图像特征
# load image features for query image
q_locs,q_descr = sift.read_features_from_file(featlist[q_ind])
fp = homography.make_homog(q_locs[:,:2].T)

# RANSAC model for homography fitting
model = homography.RansacModel()

rank = {}
# load image features for result
for ndx in res_reg[1:]:
    locs,descr = sift.read_features_from_file(featlist[ndx-1])  # because 'ndx' is a rowid of the DB that starts at 1.
    # locs,descr = sift.read_features_from_file(featlist[ndx])
    # get matches
    matches = sift.match(q_descr,descr)
    ind = matches.nonzero()[0]
    ind2 = matches[ind]
    tp = homography.make_homog(locs[:,:2].T)
    # compute homography, count inliers. if not enough matches return empty list
Example #13
0
 def load_query_feature(self):
     # load image features for query image
     q_locs, q_descr = sift.read_features_from_file(self.featlist[q_ind])
     q_locs = np.array(q_locs)
     fp = homography.make_homog(q_locs[:, :2].T)
     return q_descr, fp
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')

# make homogeneous and normalize with inv(K)
x1 = homography.make_homog(l1[ndx,:2].T)
x1 = array([x1[1,:],x1[0,:],x1[2,:]])
ndx2 = [int(matches[i]) for i in ndx]
x2 = homography.make_homog(l2[ndx2,:2].T)
x2 = array([x2[1,:],x2[0,:],x2[2,:]])

x1n = dot(inv(K),x1)
x2n = dot(inv(K),x2)

tic.k('normalized')

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

tic.k('ransacd, %d inliers' % len(inliers))
Example #15
0
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)

P1 = np.array([[1, 0, 0, 0], [0, 1, 0, 0], [0, 0, 1, 0]])
P2 = sfm.compute_P_from_essential(E)

ind = 0
maxres = 0
infront = 0
Example #16
0
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]

# calibration (FIXME?)
K = camera.my_calibration(image[0].shape[:2])

# Normalize with inv(K) (allows metric reconstruction).
x1n = numpy.dot(numpy.linalg.inv(K), x1)
x2n = numpy.dot(numpy.linalg.inv(K), x2)