def runEighPoint(im1, im2, pts1, pts2, M): F = eightpoint(pts1, pts2, M) try: hp.displayEpipolarF(im1, im2, F) except Exception: pass np.savez('../results/files/q2_1.npz', F=F, M=M)
def testEightPoint(): im1 = cv2.imread('../data/im1.png') im2 = cv2.imread('../data/im2.png') pts = np.load('../data/some_corresp.npz') pts1 = pts["pts1"] pts2 = pts["pts2"] M = max((im1.shape[0], im1.shape[1])) F = eightpoint(pts1, pts2, M) from helper import displayEpipolarF displayEpipolarF(im1, im2, F) np.savez('q2_1.npz', F=F, M=M)
def runRansac(pts1noisy, pts2noisy, M, im1, im2): F = eightpoint(pts1noisy, pts2noisy, M) F_ransac, _ = ransacF(pts1noisy, pts2noisy, M) try: hp.displayEpipolarF(im1, im2, F) except Exception: pass try: hp.displayEpipolarF(im1, im2, F_ransac) except Exception: pass
def q5_1(I1, I2, M): with np.load("../data/some_corresp_noisy.npz") as data: pts1 = data['pts1'] pts2 = data['pts2'] inliers = len(pts1) # F = eightpoint(pts1, pts2, M) F, inliers = ransacF(pts1=pts1, pts2=pts2, M=M, nIters=200, tol=4.5) print(f"F:\n{F} matched {np.sum(inliers)}/{len(pts1)} points") I1 = I1[::, ::, ::-1] I2 = I2[::, ::, ::-1] displayEpipolarF(I1, I2, F)
def runSevenPoint(im1, im2, pts1, pts2, M): dist = np.power(np.subtract(pts1[:, 0], pts2[:, 0]), 2) + np.power( np.subtract(pts1[:, 1], pts2[:, 1]), 2) points = dist.argsort()[:7] pts1_temp = pts1[points, :] pts2_temp = pts2[points, :] Farray = sevenpoint(pts1, pts2, M) for i in range(0, Farray.shape[2]): F = Farray[:, :, i] try: hp.displayEpipolarF(im1, im2, F) except Exception: continue # saving the best F np.savez('../results/files/q2_2.npz', F=Farray[:, :, 0], M=M, pts1=pts1_temp, pts2=pts2_temp)
def q2_1(I1, I2, M): # Find the fundamental matrix with np.load("../data/some_corresp.npz") as data: pts1 = data['pts1'] pts2 = data['pts2'] F = eightpoint(pts1=pts1, pts2=pts2, M=M) # np.savez(q2_1_file, F=F, M=M) # sanity check with np.load(q2_1_file) as data: F = data['F'] assert F.shape == (3, 3), f"F shape is {F.shape} instead of (3, 3)" M = data['M'] print(f"F:\n{F}\nM:{M}") # Display epipolar lines I1 = I1[::, ::, ::-1] I2 = I2[::, ::, ::-1] displayEpipolarF(I1, I2, F)
def testSevenPoint(): im1 = cv2.imread('../data/im1.png') im2 = cv2.imread('../data/im2.png') pts = np.load('../data/some_corresp.npz') indexes = [0, 1, 17, 19, 58, 91, 109] # 157 231 # 309 284 # 132 329 # 474 384 # 62 154 # 425 223 # 236 159 pts1 = pts["pts1"][indexes] pts2 = pts["pts2"][indexes] M = max((im1.shape[0], im1.shape[1])) Farray = sevenpoint(pts1, pts2, M) F = Farray[0] print(F) from helper import displayEpipolarF displayEpipolarF(im1, im2, F) np.savez('q2_2.npz', F=F, M=M, pts1=pts1, pts2=pts2)
import helper import numpy as np import submission import matplotlib.pyplot as plt img1 = plt.imread("../data/im1.png") img2 = plt.imread("../data/im2.png") h, w, c = img1.shape points = np.load("../data/some_corresp.npz") p1 = points['pts1'] p2 = points['pts2'] M = max(h, w) F = submission.sevenpoint(p1, p2, M) print(F.shape) helper.displayEpipolarF(img1, img2, F)
pts1 = pts[a[0]] pts2 = pts[a[1]] image1 = cv2.imread( '/home/geekerlink/Desktop/Computer Vision/Homeworks/hw4/hw4/data/im1.png' ) image2 = cv2.imread( '/home/geekerlink/Desktop/Computer Vision/Homeworks/hw4/hw4/data/im2.png' ) im_width = image1.shape[1] im_height = image1.shape[0] #calculating the scaling factor M = max(im_height, im_width) M = float(M) #calculating the fundamental matrix F = submission.eightpoint(pts1, pts2, M) helper.displayEpipolarF(image1, image2, F) #seven point algorithm F_seven_temp = submission.sevenpoint(pts1, pts2, M) errorvalue = [] for i in range(0, 3): least_error = np.sum(np.square(np.subtract(F, F_seven_temp[i]))) errorvalue.append(least_error) error_index = np.argmin(errorvalue) F_seven = F_seven_temp[error_index] helper.displayEpipolarF(image1, image2, F_seven) #intrinsic parameters intrinsic = np.load( '/home/geekerlink/Desktop/Computer Vision/Homeworks/hw4/hw4/data/intrinsics.npz' ) b = intrinsic.files k1 = intrinsic[b[0]]
import numpy as np import cv2 from helper import displayEpipolarF from submission import eightpoint, ransacF im1 = cv2.imread('../data/im1.png')[:, :, ::-1] im2 = cv2.imread('../data/im2.png')[:, :, ::-1] corresp = np.load('../data/some_corresp_noisy.npz') pts1 = corresp['pts1'] pts2 = corresp['pts2'] M = np.max(im1.shape) print('======no RANSAC=======') F1 = eightpoint(pts1, pts2, M) print(F1) displayEpipolarF(im1, im2, F1) print('======RANSAC=======') F2, _ = ransacF(pts1, pts2, M) print(F2) displayEpipolarF(im1, im2, F2)
""" import numpy as np from mpl_toolkits.mplot3d import Axes3D import matplotlib.pyplot as plt import submission as sub import helper data = np.load('../data/some_corresp.npz') im1 = plt.imread('../data/im1.png') im2 = plt.imread('../data/im2.png') N = data['pts1'].shape[0] M = 640 # 2.1 F8 = sub.eightpoint(data['pts1'], data['pts2'], M) helper.displayEpipolarF(im1, im2, F8) np.savez('q2_1.npz', F=F8, M=M) # 2.2 pts1 = np.array([[256,270],[162,152],[199,127],[147,131],[381,236],[193,290],[157,231]]) pts2 = np.array([[257,266],[161,151],[197,135],[146,133],[380,215],[194,284],[157,211]]) Farray = sub.sevenpoint(pts1, pts2, M) helper.displayEpipolarF(im1, im2, Farray[1]) np.savez('q2_2.npz', F=Farray[1], M=M, pts1=pts1, pts2=pts2) # 3.1 intrinsic = np.load('../data/intrinsics.npz') K1, K2 = intrinsic['K1'], intrinsic['K2'] E = sub.essentialMatrix(F8, K1, K2) print(E)
# print('win2 shape',win2.shape) dist_tmp = np.sum((win2-win1)**2 * weight) if dist_tmp < dist: dist = dist_tmp x2_ans = x2[i] y2_ans = y2[i] return x2_ans, y2_ans ''' Q6.1 Multi-View Reconstruction of keypoints. Input: C1, the 3x4 camera matrix pts1, the Nx3 matrix with the 2D image coordinates and confidence per row C2, the 3x4 camera matrix pts2, the Nx3 matrix with the 2D image coordinates and confidence per row C3, the 3x4 camera matrix pts3, the Nx3 matrix with the 2D image coordinates and confidence per row Output: P, the Nx3 matrix with the corresponding 3D points for each keypoint per row err, the reprojection error. ''' def MultiviewReconstruction(C1, pts1, C2, pts2, C3, pts3, Thres): # Replace pass by your implementation pass if __name__ == '__main__': image1 = np.load('../data/img1.png') image2 = np.load('../data/img2.png') F = eightpoint() displayEpipolarF(image1,image2,F)
# roots=np.roots([a0,a1,a2,a3]) # roots=np.real(roots) # print('roots=',roots) # # T = np.array([[1/M, 0, 0], # [0, 1/M, 0], # [0, 0, 1]]) # Farray=np.zeros([3,3,len(roots)]) # for i in range(len(roots)): # F=roots[i] * F1 + (1 - roots[i]) * F2 # # [U1, S1, V1] = np.linalg.svd(F) # # S1[2]=0 # # F = np.dot(U1,np.diag(S1)).dot(V1) # F=np.dot(np.transpose(T), F).dot(T) # Farray[:,:,i]=F # np.savez('../data/q2_2.npz', F=F, M=M, pts1=pts1,pts2=pts2) # return Farray if __name__ == '__main__': data = np.load('../data/some_corresp.npz') im1 = plt.imread('../data/im1.png') im2 = plt.imread('../data/im2.png') N = data['pts1'].shape[0] # N=110 M = 640 pts1 = data['pts1'] pts2 = data['pts2'] Farray = sub.sevenpoint(pts1[7:14, :], pts2[7:14, :], M) print('F=', Farray[:, :, 2]) helper.displayEpipolarF(im1, im2, Farray[:, :, 2])
pts2 = corresp['pts2'] # select 7 points N = pts1.shape[0] select_ids = [53, 17, 43, 46, 27, 56, 77] pts1 = pts1[select_ids, :].copy() pts2 = pts2[select_ids, :].copy() M = np.max(im1.shape) Farray = sevenpoint(pts1, pts2, M) for idx, F in enumerate(Farray): print('visualizing F%d' % idx) print(F) displayEpipolarF(im1, im2, F) print('Enter the id of F which is correct:') correct_id = int(input()) assert 0 <= correct_id < len(Farray) F_correct = Farray[correct_id] np.savez('q2_2.npz', F=F_correct, M=M, pts1=pts1, pts2=pts2) ''' visualizing F0 [[ 1.37155686e-08 -8.61003099e-08 -8.27427609e-04] [ 2.16183531e-07 1.90452142e-09 -2.92701083e-05] [ 7.89066675e-04 1.84620407e-05 3.37863104e-03]] visualizing F1 [[-1.48732736e-07 2.26439993e-06 -2.62220314e-04] [-1.97195996e-06 -2.63484236e-06 2.05684451e-03] [ 3.33774809e-04 -8.47499098e-04 -1.28974831e-01]]
M = np.max(I1.shape) data = np.load("../data/some_corresp.npz") pts1 = data['pts1'] pts2 = data['pts2'] data = np.load("../data/templeCoords.npz") x1 = data['x1'] y1 = data['y1'] F = eightpoint(pts1, pts2, M) np.savez_compressed('./q2_1.npz', F=F, M=M) helper.displayEpipolarF(I1, I2, F) bestF, Farray_best, points1, points2 = BestFSevenPoints(pts1, pts2, M) np.savez_compressed('./q2_2.npz', F=bestF, M=M, pts1=points1, pts2=points2) helper.displayEpipolarF(I1, I2, bestF) E = essentialMatrix(F, K1, K2) Ms = helper.camera2(E) M2, M1, C2, C1, Points = findM2_function(K1, K2, Ms, pts1, pts2) np.savez_compressed('./q3_3.npz', M2=M2, C2=C2, P=Points)
#5.1 pts = np.load('../data/some_corresp_noisy.npz') pts1 = pts["pts1"] pts2 = pts["pts2"] im1 = plt.imread('../data/im1.png') im2 = plt.imread('../data/im2.png') M = np.max(np.shape(im1)) # F = eightpoint(pts1, pts2, M) nIters = 200 tol = 0.002 args = [] args.append((pts1, pts2, M, nIters, tol)) p = Pool() F = p.map(ransacF, args) F = np.array(F).reshape(3, 3) p.close() p.join() # F, inliers = ransacF(pts1, pts2, M) print("F = ", F) intrinsics = np.load('../data/intrinsics.npz') K1 = intrinsics["K1"] K2 = intrinsics["K2"] E = essentialMatrix(F, K1, K2) print("E = ", E) helper.displayEpipolarF(im1, im2, F)
import numpy as np import matplotlib.pyplot as plt import submission as sub import helper as hp data = np.load('../data/some_corresp.npz') im1 = plt.imread('../data/im1.png') im2 = plt.imread('../data/im2.png') K_matrices = np.load('../data/intrinsics.npz') N = data['pts1'].shape[0] M = 640 # 2.1 #F8 = sub.eightpoint(data['pts1'], data['pts2'], M) #hp.displayEpipolarF(im1,im2,F8) #F = F8 #2.2 F7 = sub.sevenpoint(data['pts1'][21:28, :], data['pts2'][21:28, :], M) hp.displayEpipolarF(im1, im2, F7) E = sub.essentialMatrix(F, K_matrices['K1'], K_matrices['K2'])