def q4_1(I1, I2): if os.path.exists(q4_1_file): with np.load(q4_1_file) as data: F = data['F'] assert F.shape == (3, 3), f"F shape is {F.shape} instead of (3, 3)" pts1 = data['pts1'] pts2 = data['pts2'] assert pts1.shape == pts2.shape, \ f"pt1s shape {pts1.shape} != pts2 size {pts2.shape}" print(f"F:\n{F} matched {len(pts1)} points") else: if os.path.exists(q2_1_file): with np.load(q2_1_file, allow_pickle=True) as data: F = data['F'] else: with np.load("../data/some_corresp.npz") as data: pts1 = data['pts1'] pts2 = data['pts2'] M = max(I1.shape[0], I1.shape[1]) F = eightpoint(pts1=pts1, pts2=pts2, M=M) np.savez(q2_1_file, F=F, M=M) # Epipolar matching I1 = I1[::, ::, ::-1] I2 = I2[::, ::, ::-1] epipolarMatchGUI(I1=I1, I2=I2, F=F)
def testEpipolarCorrespondence(): 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 epipolarMatchGUI epipolarMatchGUI(im1, im2, F) np.savez('q4_1.npz', F=F, pts1=pts1, pts2=pts2)
def runEpipolarCorrespondence(im1, im2, pts1, pts2, M): F8 = eightpoint(pts1, pts2, M) try: p1, p2 = hp.epipolarMatchGUI(im1, im2, F8) np.savez('../results/files/q4_1.npz', F=F8, pts1=p1, pts2=p2) except Exception: pass
for f7 in F7: assert f7.shape == (3, 3), 'seven returns list of 3x3 matrix' #print(F7.shape) print('F7') print(F7) #helper.displayEpipolarF(im1, im2, F7[0]) # 3.1 print('E') print(sub.essentialMatrix(F8, K1, K2)) C1 = np.concatenate([np.random.rand(3, 3), np.ones([3, 1])], axis=1) C2 = np.concatenate([np.random.rand(3, 3), np.ones([3, 1])], axis=1) P, err = sub.triangulate(C1, data['pts1'], C2, data['pts2']) print(err) assert P.shape == (N, 3), 'triangulate returns Nx3 matrix P' assert np.isscalar(err), 'triangulate returns scalar err' # 4.1 x2, y2 = sub.epipolarCorrespondence(im1, im2, F8, data['pts1'][0, 0], data['pts1'][0, 1]) assert np.isscalar(x2) & np.isscalar( y2), 'epipolarCorrespondence returns x & y coordinates' np.savez('../results/q4_1.npz', F=F8, pts1=data['pts1'], pts2=data['pts2']) helper.epipolarMatchGUI(im1, im2, F8) print('Format check passed.')
from helper import epipolarMatchGUI ''' Q4.1: 3D visualization of the temple images. Input: im1, the first image im2, the second image F, the fundamental matrix x1, x-coordinates of a pixel on im1 y1, y-coordinates of a pixel on im1 Output: x2, x-coordinates of the pixel on im2 y2, y-coordinates of the pixel on im2 ''' if __name__ == '__main__': data = np.load('../data/some_corresp.npz') im1 = plt.imread('../data/im1.png') im2 = plt.imread('../data/im2.png') pts1 = data['pts1'] pts2 = data['pts2'] N = data['pts1'].shape[0] # N=110 M = 640 P1 = pts1 P2 = np.zeros(P1.shape) F = sub.eightpoint(pts1, pts2, M) for i in range(P1.shape[0]): [P2[i, 0], P2[i, 1]] = sub.epipolarCorrespondence(im1, im2, F, P1[i, 0], P1[i, 1]) # np.savez('../jingruwu/data/q4_1.npz',P1=P1,P2=P2) epipolarMatchGUI(im1, im2, F)
data = np.load('../data/templeCoords.npz') im1 = plt.imread('../data/im1.png') im2 = plt.imread('../data/im2.png') x1 = data['x1'] y1 = data['y1'] num, temp = x1.shape pts1 = np.hstack((x1, y1)) pts2 = np.zeros((num, 2)) for i in range(num): x2, y2 = epipolarCorrespondence(im1, im2, F, x1[i, 0], y1[i, 0]) pts2[i, 0] = x2 pts2[i, 1] = y2 p1,p2=helper.epipolarMatchGUI(im1, im2, F) P, err = triangulate(C1, pts1, C2, pts2) fig = plt.figure() ax = Axes3D(fig) ax.scatter(P[:, 0], P[:, 1], P[:, 2]) ax.set_xlabel('X') ax.set_ylabel('Y') ax.set_zlabel('Z') plt.show() if (os.path.isfile('q4_2.npz') == False): np.savez('q4_2.npz', F=F, M1=M1, M2=M2, C1=C1, C2=C2)
# 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) # 4.1 selected_pts1, selected_pts2 = helper.epipolarMatchGUI(im1, im2, F8) #np.savez('q4_1.npz', F=F8, pts1=selected_pts1, pts2=selected_pts2) # 5.1 noise_data = np.load('../data/some_corresp_noisy.npz') F, inliers = sub.ransacF(noise_data['pts1'], noise_data['pts2'], M) np.savez('tmpnew.npz', F=F, inliers=inliers) # helper.displayEpipolarF(im1, im2, F) # F_compare = sub.eightpoint(noise_data['pts1'], noise_data['pts2'], M) # helper.displayEpipolarF(im1, im2, F_compare) # 5.2 r = np.ones([3, 1]) R = sub.rodrigues(r) assert R.shape == (3, 3), 'rodrigues returns 3x3 matrix'
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) helper.epipolarMatchGUI(I1, I2, F) np.savez_compressed('./q4_1.npz', F=F, pts1=pts1, pts2=pts2) visualize.Visualize(I1, I2, x1, y1, C1, C2, F) np.savez_compressed('./q4_2.npz', F=F, C1=C1, C2=C2, M2=M2, M1=M1)
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 K1 = K_matrices['K1'] K2 = K_matrices['K2'] F = sub.eightpoint(data['pts1'], data['pts2'], M) E = sub.essentialMatrix(F, K1, K2) M1 = np.concatenate([np.eye(3), np.zeros([3, 1])], axis=1) C1 = np.dot(K1, M1) M2s = hp.camera2(E) ''' for i in range(M2s.shape[-1]): C2 = np.dot(K2,M2s[:,:,i]) pts_3D, error = sub.triangulate(C1, data['pts1'], C2, data['pts2']) print(error) ''' M2 = M2s[:, :, 2] C2 = np.dot(K2, M2) pts_3D, error = sub.triangulate(C1, data['pts1'], C2, data['pts2']) #np.savez('../results/q3_3.npz',M2=M2,C2=C2,P=pts_3D) hp.epipolarMatchGUI(im1, im2, F)