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])
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
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
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(1) H_12 = homography.H_from_ransac(fp, tp, model)[0] tic.k('12 homogd') fp, tp = convert_points(0) H_01 = homography.H_from_ransac(fp, tp, model)[0] tic.k('01 homogd') tp, fp = convert_points(2) # Note: Reversed. H_32 = homography.H_from_ransac(fp, tp, model)[0] tic.k('32 homogd') tp, fp = convert_points(3) # Note: Reversed. H_43 = homography.H_from_ransac(fp, tp, model)[0] tic.k('43 homogd') # FIXME: Consider using bundle adjustment and Levenberg-Marquardt instead of
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 # estimate the homographies model = homography.RansacModel() fp, tp = convert_points(1) H_12 = homography.H_from_ransac(fp, tp, model)[0] #im 1 to 2 fp, tp = convert_points(0) H_01 = homography.H_from_ransac(fp, tp, model)[0] #im 0 to 1 tp, fp = convert_points(2) #NB: reverse order H_32 = homography.H_from_ransac(fp, tp, model)[0] #im 3 to 2 tp, fp = convert_points(3) #NB: reverse order H_43 = homography.H_from_ransac(fp, tp, model)[0] #im 4 to 3 delta = 2000 #for padding and translation im1 = array(Image.open(imname[1])) im2 = array(Image.open(imname[2])) im_12 = warp.panorama(H_12, im1, im2, delta, delta) im1 = array(Image.open(imname[0])) im_02 = warp.panorama(dot(H_12, H_01), im1, im_12, delta, delta)
bf = cv2.BFMatcher(crossCheck=True) matches = bf.match(d0, d1) #https://stackoverflow.com/questions/30716610/how-to-get-pixel-coordinates-from-feature-matching-in-opencv-python kp0 = [l0[mat.queryIdx].pt for mat in matches] kp1 = [l1[mat.trainIdx].pt for mat in matches] kp0 = np.array(kp0) kp1 = np.array(kp1) #%% ''' Compute from and to points ''' fp = homography.make_homog(kp0[:100, :2].T) tp = homography.make_homog(kp1[:100, :2].T) model = homography.RansacModel() H, _ = homography.H_from_ransac(fp, tp, model) #%% ''' Draw cone ''' def cube_points(c, wid): """ Creates a list of points for plotting a cube with plot. (the first 5 points are the bottom square, some sides repeated). """ p = [] #bottom p.append([c[0] - wid, c[1] - wid, c[2] - wid]) p.append([c[0] - wid, c[1] + wid, c[2] - wid]) p.append([c[0] + wid, c[1] + wid, c[2] - wid]) p.append([c[0] + wid, c[1] - wid, c[2] - wid])
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 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)) #单应性矩阵的变换
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 imagesearch.plot_results(searcher, res[:8]) imagesearch.plot_results(searcher, res_geom[:8])
kp1, des1 = m_ORB.detectAndCompute(G_img1, None) kp2, des2 = m_ORB.detectAndCompute(G_img2, None) bf = cv2.BFMatcher(cv2.NORM_HAMMING, crossCheck=True) matches = bf.match(des1, des2) matches = sorted( matches, key=lambda x: x.distance) # Sort them in the order of their distance. obj1, obj2 = Cli.Manage_data(matches, kp1, kp2) model = h**o.RansacModel() H = h**o.H_from_ransac(obj1, obj2, model) # # Draw first 60 matches. # img3 = img2 # img3 = cv2.drawMatches(img1, kp1, img2, kp2, matches[:60], img3) # cv2.imshow('result', img3) # cv2.imwrite('result.jpg', img3) # cv2.waitKey() # 位于边长为0.2, z = 0平面上底部的正方形 Box = cube_points([0, 0, 0.1], 0.1) cam1 = Cli.Camera( numpy.hstack((K, numpy.dot(K, numpy.array([[0], [0], [-1]]))))) box_cam1 = cam1.project(h**o.make_homog(Box[:, :5]))
# 対応点を同次座標の点に変換する関数 def convert_points(matches,kp1,kp2): # ndx = matches.nonzero()[0] fp=np.array([kp1[m.queryIdx].pt for m in matches]) fp = homography.make_homog(fp.T) # ndx2 = [int(matches[i]) for i in ndx] tp=np.array([kp2[m.trainIdx].pt for m in matches]) tp = homography.make_homog(tp.T) return fp,tp # + # ホモグラフィーを推定 model = homography.RansacModel() fp,tp = convert_points(matches,kp1,kp2) H,inlier=homography.H_from_ransac(fp,tp,model) inlier # - inl_matches=[matches[i] for i in inlier] img3 = cv2.drawMatches(im1, kp1, im2, kp2, inl_matches, None, flags=2) ip.show_img(img3,figsize=(20,30)) ip.imwrite('inl_match.jpg',img3) np.vstack((fp,tp)) # + MIN_MATCH_COUNT=10 # Initiate AKAZE detector
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 fp = np.vstack([fp[1], fp[0], fp[2]]) tp = np.vstack([tp[1], tp[0], tp[2]]) return fp, tp # estimate the homographies model = homography.RansacModel() fp, tp = convert_points(0) H_01 = homography.H_from_ransac(fp, tp, model)[0] # im 0 to 1 fp, tp = convert_points(1) H_12 = homography.H_from_ransac(fp, tp, model)[0] # im 1 to 2 # warp the images delta = 800 im0 = np.array(Image.open(imname[0]), "uint8") im1 = np.array(Image.open(imname[1]), "uint8") im2 = np.array(Image.open(imname[2]), "uint8") im_01 = warp.panorama(H_01, im0, im1, delta, delta) im_12 = warp.panorama(H_12, im1, im2, delta, delta) im0 = np.array(Image.open(imname[0]), "f") im_02 = warp.panorama(np.dot(H_12, H_01), im0, im_12, delta, delta)
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) tic.k('converted') if True: # Ignore wrong matches. H_12 = homography.H_from_ransac(fp, tp, model)[0] else: # Assume all matches are correct. H_12 = homography.H_from_points(fp, tp) tic.k('homogd') # ... delta = 600 im1 = array(Image.open(imname[0]).convert('L')) im2 = array(Image.open(imname[1]).convert('L')) tic.k('imloaded') im_12 = warp.panorama(H_12, im1, im2, delta, delta, alpha=0.5)
if __name__ == "__main__": import cv2 import ORB_Match img1 = cv2.imread('5.jpg', cv2.IMREAD_COLOR) img2 = cv2.imread('6.jpg', cv2.IMREAD_COLOR) # 记录程序运行时间 e1 = cv2.getTickCount() # your code execution kp1, kp2, Matched = ORB_Match.ORB_match(img1, img2) def convert_points(j, matches): """将匹配转换成齐次坐标点的函数""" ndx = matches[j].nonzero()[0] fp = homography.make_homog(kp1[ndx, :2].T) ndx2 = [int(matches[j][i]) for i in ndx] tp = homography.make_homog(kp2[ndx2, :2].T) return fp, tp model = homography.RansacModel() fp, tp = convert_points(1) H_12 = homography.H_from_ransac(fp, tp, model)[0] # img1到img2的单应性矩阵 img_12 = panorama(H_12, img1, img2, 2000, 2000) # Detect_and_Draw() e2 = cv2.getTickCount() time = (e2 - e1) / cv2.getTickFrequency() print "Processing time is %f s" % time