Example #1
0
def sevenpoint(pts1, pts2, M):
    # Replace pass by your implementation
    pts1 = pts1/M
    pts2 = pts2/M
    n = pts1.shape[0] #num points

    x1 = pts1[:,0]
    x2 = pts2[:,0]
    y1 = pts1[:,1]
    y2 = pts2[:,1]

    A = np.vstack([
        x2*x1,
        x2*y1,
        x2,
        y2*x1,
        y2*y1,
        y2,
        x1,
        y1,
        np.ones(n)])

    V = np.linalg.svd(np.transpose(A))[2]

    F1 = V[-1,:].reshape(3,3)
    F2 = V[-2,:].reshape(3,3)

    F1 = helper.refineF(F1, pts1, pts2)
    F2 = helper.refineF(F2, pts1, pts2)

    # given to find coefficients
    fun = lambda a: np.linalg.det(a * F1 + (1 - a) * F2)

    a0 = fun(0)
    a1 = 2*(fun(1) - fun(-1))/3 - (fun(2) - fun(-2))/12
    a2 = 0.5*fun(1) + 0.5*fun(-1) - fun(0) 
    #a3 = (fun(1) - fun(-1))/6 + (fun(2) - fun(-2))/12
    a3 = 0.5*(fun(1) - fun(-1)) - a1

    coeff = np.array([a3,a2,a1,a0])
    alpha = np.polynomial.polynomial.polyroots(coeff)
    # extract real solutions
    alpha = np.real(alpha[np.isreal(alpha)])

    T = np.diag([1.0/M, 1.0/M, 1.0])

    Farray = []
    for a in alpha:
        #a = a.real
        F = a*F1 +(1-a)*F2
        F = helper._singularize(F)
        F = helper.refineF(F, pts1, pts2)
        F = np.dot(np.transpose(T), np.dot(F,T))

        Farray.append(F)

    Farray = np.array(Farray)

    np.savez('../results/q2_2.npz', F=F, M=M, pts1=pts1, pts2=pts2)
    return Farray
def sevenpoint(pts1, pts2, M):
    # Replace pass by your implementation
    pts1 = pts1 * 1.0 / M
    pts2 = pts2 * 1.0 / M
    n, temp = pts1.shape
    x1 = pts1[:, 0]
    y1 = pts1[:, 1]
    x2 = pts2[:, 0]
    y2 = pts2[:, 1]
    A1 = (x2 * x1)
    A2 = (x2 * y1)
    A3 = x2
    A4 = y2 * x1
    A5 = y2 * y1
    A6 = y2
    A7 = x1
    A8 = y1
    A9 = np.ones(n)
    A = np.vstack((A1, A2, A3, A4, A5, A6, A7, A8, A9))
    A = A.T
    u, s, vh = np.linalg.svd(A)
    f1 = vh[-1, :]
    f2 = vh[-2, :]
    F1 = f1.reshape(3, 3)
    F2 = f2.reshape(3, 3)
    F1 = helper.refineF(F1, pts1, pts2)
    F2 = helper.refineF(F2, pts1, pts2)

    # w = sympy.Symbol('w')
    # m = sympy.Matrix(w*F1+(1-w)*F2)
    #
    # coeff = (m.det().as_poly().coeffs())
    fun = lambda a: np.linalg.det(a * F1 + (1 - a) * F2)
    a0 = fun(0)
    a1 = (fun(1) - fun(-1)) / 3 - (fun(2) - fun(-2)) / 12
    a2 = 0.5 * fun(1) + 0.5 * fun(-1) - fun(0)
    a3 = (fun(1) - fun(-1)) / 6 + (fun(2) - fun(-2)) / 12

    coeff = [a3, a2, a1, a0]
    # print(coeff)
    # coeff = coeff[::-1]
    # print(coeff)
    soln = np.roots(coeff)
    soln = soln[np.isreal(soln)]
    # print(soln)
    # print(soln.shape)
    Fs = []
    T = np.array([[1. / M, 0, 0], [0, 1. / M, 0], [0, 0, 1]])
    for i in range(len(soln)):
        F = (np.matmul(T.T, np.matmul((soln[i] * F1 + (1 - soln[i]) * F2), T)))
        # F = helper.refineF(F,pts1,pts2)
        # F = helper._singularize(F)
        Fs.append(F)
    # print (Fs)
    if (os.path.isfile('q2_2.npz') == False):
        np.savez('q2_2.npz', F=Fs[2], M=M, pts1=pts1, pts2=pts2)
    return Fs
def eightpoint(pts1, pts2, M):
    # normalize the coordinates
    x1, y1 = pts1[:, 0], pts1[:, 1]
    x2, y2 = pts2[:, 0], pts2[:, 1]
    x1, y1, x2, y2 = x1 / M, y1 / M, x2 / M, y2 / M
    # normalization matrix
    T = np.array([[1. / M, 0, 0], [0, 1. / M, 0], [0, 0, 1]])

    A = np.transpose(
        np.vstack((x2 * x1, x2 * y1, x2, y2 * x1, y2 * y1, y2, x1, y1,
                   np.ones(x1.shape))))

    # get F by SVD decomposition
    u, s, vh = np.linalg.svd(A)
    f = vh[-1, :]
    F = np.reshape(f, (3, 3))

    # refine F
    F = helper.refineF(F, pts1 / M, pts2 / M)

    # constraint of rank 2 by setting the last singular value to 0
    F = helper._singularize(F)

    # rescale the data
    F = np.dot(np.transpose(T), np.dot(F, T))

    return F
Example #4
0
def eightpoint(pts1, pts2, M):
    #making a scaling T matrix 
    T=np.zeros((3,3))
    T[0][0]=1/M 
    T[1][1]=1/M
    T[2][2]=1
    #number of correspondences
    n=pts1.shape[0] 
    A=np.zeros((n,9))
    #normalizing the matrix
    pts1=pts1/M
    pts2=pts2/M
    #creating the a matrix 
    A[:,0]=np.multiply(pts1[:,0],pts2[:,0])
    A[:,1]=np.multiply(pts1[:,0],pts2[:,1])
    A[:,2]=pts1[:,0]
    A[:,3]=np.multiply(pts1[:,1],pts2[:,0])
    A[:,4]=np.multiply(pts1[:,1],pts2[:,1])
    A[:,5]=pts1[:,1]
    A[:,6]=pts2[:,0]
    A[:,7]=pts2[:,1]
    A[:,8]=np.ones(n)
    #svd and reshaping fundamental matrix 
    U,S,V=np.linalg.svd(A)
    F=np.transpose(V[-1,:]).reshape(3,3)
    #singulairy function 
    F=_singularize(F)
    #refine 
    F=refineF(F,pts1,pts2)
    #unnormalize the matrix 
    mat=np.matmul(T.transpose(),F)
    F=np.matmul(mat,T)
    #np.savez('/home/geekerlink/Desktop/Computer Vision/Homeworks/hw4/hw4/results/q2_1.npz',F=F,M=M)
    return F
Example #5
0
def eightpoint(pts1, pts2, M):
    pts1 = pts1 / M
    pts2 = pts2 / M
    u = pts1[:, 0]
    u1 = pts2[:, 0]
    v = pts1[:, 1]
    v1 = pts2[:, 1]

    # Construction A matrix
    A = np.vstack(
        [u * u1, v * u1, u1, u * v1, v * v1, v1, u, v,
         np.ones(u.shape)])
    # Get eigenvector
    _, _, V = np.linalg.svd(A.T)
    F = np.reshape(V[-1, :], (3, 3))

    # Local minimization
    from helper import refineF
    F = refineF(F, pts1, pts2)

    # Unscale F using T representing scaling by 1/M
    T = np.array([[1 / M, 0, 0], [0, 1 / M, 0], [0, 0, 1]])

    unscaledF = np.dot(np.dot(np.transpose(T), F), T)
    return unscaledF
Example #6
0
def eightpoint(pts1, pts2, M):
    # Replace pass by your implementation
    normPts1 = pts1 / M
    normPts2 = pts2 / M

    pts1Y = normPts1[:, 1]
    pts1X = normPts1[:, 0]

    pts2Y = normPts2[:, 1]
    pts2X = normPts2[:, 0]

    AMat = np.ones([np.size(pts1X), 9])

    AMat[:, 0] = np.multiply(pts2X, pts1X)
    AMat[:, 1] = np.multiply(pts2X, pts1Y)
    AMat[:, 2] = pts2X
    AMat[:, 3] = np.multiply(pts2Y, pts1X)
    AMat[:, 4] = np.multiply(pts2Y, pts1Y)
    AMat[:, 5] = pts2Y
    AMat[:, 6] = pts1X
    AMat[:, 7] = pts1Y

    U, S, Vt = np.linalg.svd(AMat)
    VNormal = np.transpose(Vt)
    Fiter1 = VNormal[:, -1]
    Fiter1Res = np.reshape(Fiter1, (3, 3))
    Unew, Snew, Vtnew = np.linalg.svd(Fiter1Res)
    Snew[-1] = 0
    SMat = np.diag(Snew)
    Fiter2 = np.matmul(np.matmul(Unew, SMat), Vtnew)
    #print (Fiter2)
    Fscaled = helper.refineF(Fiter2, normPts1, normPts2)
    TMat = np.array([[1 / M, 0, 0], [0, 1 / M, 0], [0, 0, 1]])
    Funscaled = np.matmul(np.matmul(TMat, Fscaled), TMat)
    return Funscaled
def eightpoint(pts1, pts2, M):
    # Replace pass by your implementation

    # Normalizing

    pts1 = pts1 / M
    pts2 = pts2 / M

    W = help_func(pts1, pts2)
    u, s, vh = np.linalg.svd(W)

    eigen_value = vh[-1, :]
    eigen_value = eigen_value.reshape(3, 3)

    F = helper._singularize(eigen_value)

    F = helper.refineF(F, pts1, pts2)

    M_list = [[1 / M, 0, 0], [0, 1 / M, 0], [0, 0, 1]]

    T_matrix = np.asarray(M_list)

    F = np.matmul(T_matrix.T, np.matmul(F, T_matrix))

    np.savez('q2_1.npz', F=F, M=M)

    return F
def sevenpoint(pts1, pts2, M):
    # normalize the coordinates
    x1, y1 = pts1[:, 0], pts1[:, 1]
    x2, y2 = pts2[:, 0], pts2[:, 1]
    x1, y1, x2, y2 = x1 / M, y1 / M, x2 / M, y2 / M
    # normalization matrix
    T = np.array([[1. / M, 0, 0], [0, 1. / M, 0], [0, 0, 1]])

    A = np.transpose(
        np.vstack((x2 * x1, x2 * y1, x2, y2 * x1, y2 * y1, y2, x1, y1,
                   np.ones(x1.shape))))

    # get F by SVD decomposition
    u, s, vh = np.linalg.svd(A)
    f1 = vh[-1, :]
    f2 = vh[-2, :]
    F1 = np.reshape(f1, (3, 3))
    F2 = np.reshape(f2, (3, 3))

    fun = lambda alpha: np.linalg.det(alpha * F1 + (1 - alpha) * F2)
    # get the coefficients of the polynomial
    a0 = fun(0)
    a1 = 2 * (fun(1) - fun(-1)) / 3 - (fun(2) - fun(-2)) / 12
    a2 = (fun(1) + fun(-1)) / 2 - a0
    a3 = (fun(1) - fun(-1)) / 2 - a1
    # solve for alpha
    alpha = np.roots([a3, a2, a1, a0])

    Farray = [a * F1 + (1 - a) * F2 for a in alpha]
    # refine F
    Farray = [helper.refineF(F, pts1 / M, pts2 / M) for F in Farray]
    # denormalize F
    Farray = [np.dot(np.transpose(T), np.dot(F, T)) for F in Farray]

    return Farray
Example #9
0
def sevenpoint(pts1, pts2, M):
    pts1 = pts1.astype('float64')
    pts2 = pts2.astype('float64')
    pts1 /= M
    pts2 /= M
    A = [pts2[:,0]*pts1[:,0], pts2[:,0]*pts1[:,1], pts2[:,0], pts2[:,1]*pts1[:,0], pts2[:,1]*pts1[:,1], pts2[:,1], pts1[:,0], pts1[:,1], np.ones_like(pts1[:,0])]
    A = np.asarray(A).T
    U, S, V = np.linalg.svd(A)
    F1 = V[-1].reshape(3,3)
    F2 = V[-2].reshape(3,3)
    func = lambda x: np.linalg.det((x * F1) + ((1 - x) * F2))
    x0 = func(0)
    x1 = (2 * (func(1) - func(-1)) / 3.0) - ((func(2) - func(-2)) / 12.0)
    x2 = (0.5 * func(1)) + (0.5 * func(-1)) - func(0)
    x3 = func(1) - x0 - x1 - x2
    alphas = np.roots([x3,x2,x1,x0])
    alphas = np.real(alphas[np.isreal(alphas)])
    final_F = []
    for a in alphas:
        F = (a * F1) + ((1 - a) * F2)
        F = refineF(F, pts1, pts2)
        t = np.array([[1.0/M,0.0,0.0],[0.0,1.0/M,0.0],[0.0,0.0,1.0]])
        F = t.T.dot(F).dot(t)
        final_F.append(F)
    # np.savez('q2_2.npz', F=final_F[-1], M=M, pts1=pts1, pts2=pts2)
    return final_F
Example #10
0
def sevenpoint(pts1, pts2, M):

    pts1 = pts1 / M
    pts2 = pts2 / M
    A = [pts2[:,0]*pts1[:,0], pts2[:,0]*pts1[:,1], pts2[:,0], pts2[:,1]*pts1[:,0], \
        pts2[:,1]*pts1[:,1], pts2[:,1], pts1[:,0], pts1[:,1], np.ones_like(pts1[:,0])]
    A = np.asarray(A).T
    u, s, v = np.linalg.svd(A)
    F1 = v[-1].reshape(3, 3)
    F2 = v[-2].reshape(3, 3)
    # use the property of determinant
    det = lambda x: np.linalg.det((x * F1) + ((1 - x) * F2))
    a0 = det(0)
    a1 = (2 * (det(1) - det(-1)) / 3.0) - ((det(2) - det(-2)) / 12.0)
    a2 = (0.5 * det(1)) + (0.5 * det(-1)) - det(0)
    a3 = det(1) - a0 - a1 - a2
    alphas = np.roots([a3, a2, a1, a0])
    alphas = np.real(alphas[np.isreal(alphas)])
    Fs = []
    for a in alphas:
        F = a * F1 + (1 - a) * F2
        u, s, vh = np.linalg.svd(np.array(F))
        s[2] = 0
        F = u @ np.diag(s) @ vh
        F = helper.refineF(F, pts1, pts2)
        T = np.diag((1 / M, 1 / M, 1))
        F = T.T @ F @ T
        Fs.append(F)
    Fs = np.array(Fs)
    # uncomment this to save F
    # np.savez('q2_2.npz', F=final, M=M, pts1=pts1, pts2=pts2)
    return Fs
def eightpoint(pts1, pts2, M):
    pts1 = pts1 / M
    pts2 = pts2 / M
    x1 = pts1[:, 0]
    y1 = pts1[:, 1]
    x2 = pts2[:, 0]
    y2 = pts2[:, 1]

    # SVD solve AF=0
    A = np.stack(
        [x2 * x1, x2 * y1, x2, y2 * x1, y2 * y1, y2, x1, y1,
         np.ones(len(x1))],
        axis=1)
    (U, S, V) = np.linalg.svd(A)
    V = np.transpose(V)
    F = np.reshape(V[:, len(V) - 1], (3, 3))

    # enforce the singularity condition of the F before unscaling  Set rank=2
    [U1, S1, V1] = np.linalg.svd(F)
    S1[2] = 0
    F = np.dot(U1, np.diag(S1))
    F = np.dot(F, V1)
    # refine F before unscaling F
    F = helper.refineF(F, pts1, pts2)

    # unscaling F
    T = np.array([[1 / M, 0, 0], [0, 1 / M, 0], [0, 0, 1]])
    F = np.dot(np.transpose(T), F).dot(T)
    # np.savez('../jingruwu/data/q2_1.npz', F=F, M=M)
    return F
def eightpoint(pts1, pts2, M):
    # Replace pass by your implementation
    # pass
    pts1_norm = pts1 / M
    pts2_norm = pts2 / M

    pts1_x, pts1_y = pts1_norm[:, 0], pts1_norm[:, 1]
    pts2_x, pts2_y = pts2_norm[:, 0], pts2_norm[:, 1]

    solve_A = np.ones([np.size(pts1_x), 9])

    solve_A[:, 0] = np.multiply(pts2_x, pts1_x)
    solve_A[:, 1] = np.multiply(pts2_x, pts1_y)
    solve_A[:, 2] = pts2_x
    solve_A[:, 3] = np.multiply(pts2_y, pts1_x)
    solve_A[:, 4] = np.multiply(pts2_y, pts1_y)
    solve_A[:, 5] = pts2_y
    solve_A[:, 6] = pts1_x
    solve_A[:, 7] = pts1_y

    U, S, Vt = np.linalg.svd(solve_A)
    V = np.transpose(Vt)
    F_svd1 = np.reshape(V[:, -1], (3, 3))
    F_singular = helper._singularize(F_svd1)

    refined_F = helper.refineF(F_singular, pts1_norm, pts2_norm)

    T = np.array([[1 / M, 0, 0], [0, 1 / M, 0], [0, 0, 1]])
    result = T @ refined_F @ T
    return result
Example #13
0
def eightpoint(pts1, pts2, M):

    T = np.array(([1 / M, 0, 0], [0, 1 / M, 0], [0, 0, 1]))

    pts1_n = [np.dot(T, np.insert(pt1, 2, 1)).T for pt1 in pts1]
    pts2_n = [np.dot(T, np.insert(pt2, 2, 1)).T for pt2 in pts2]

    U = []

    for i in range(pts1.shape[0]):
        a = [
            pts1_n[i][0] * pts2_n[i][0], pts1_n[i][1] * pts2_n[i][0],
            pts2_n[i][0], pts1_n[i][0] * pts2_n[i][1],
            pts1_n[i][1] * pts2_n[i][1], pts2_n[i][1], pts1_n[i][0],
            pts1_n[i][1], 1
        ]
        U.append(a)

    U = np.vstack(U)

    u, s, v = np.linalg.svd(U)

    F = v[-1].reshape(3, 3)

    pts1_n = np.array(pts1_n)
    pts2_n = np.array(pts2_n)

    pts1_n = np.delete(pts1_n, -1, axis=1)
    pts2_n = np.delete(pts2_n, -1, axis=1)

    F = hp.refineF(F, np.array(pts1_n), np.array(pts2_n))
    F = np.dot(T.T, np.dot(F, T))

    return F
Example #14
0
def eightpoint(pts1, pts2, M):
    # Replace pass by your implementation
    pts1 = np.divide(pts1, M)
    pts2 = np.divide(pts2, M)
    N = pts1.shape[0]

    pts1_x = pts1[:, 0]
    pts1_y = pts1[:, 1]
    pts2_x = pts2[:, 0]
    pts2_y = pts2[:, 1]

    A = np.vstack(
        (np.multiply(pts1_x, pts2_x), np.multiply(pts1_x, pts2_y), pts1_x,
         np.multiply(pts1_y, pts2_x), np.multiply(pts1_y, pts2_y), pts1_y,
         pts2_x, pts2_y, np.ones(N)))
    A = A.T

    u, s, v = np.linalg.svd(A)
    F = np.reshape(v[8, :], (3, 3))

    F_refined = hp.refineF(F, pts1, pts2)
    t = 1.0 / M

    T = np.array([[t, 0, 0], [0, t, 0], [0, 0, 1]])
    F = np.matmul(T.T, np.matmul(F_refined, T))

    return F
Example #15
0
def sevenpoint(pts1, pts2, M):
    # Replace pass by your implementation

    pts1_scaled = pts1 / M
    pts2_scaled = pts2 / M

    A_f = np.zeros((pts1_scaled.shape[0], 9))

    for i in range(pts1_scaled.shape[0]):
        A_f[i, :] = [
            pts2_scaled[i, 0] * pts1_scaled[i, 0],
            pts2_scaled[i, 0] * pts1_scaled[i, 1], pts2_scaled[i, 0],
            pts2_scaled[i, 1] * pts1_scaled[i, 0],
            pts2_scaled[i, 1] * pts1_scaled[i, 1], pts2_scaled[i, 1],
            pts1_scaled[i, 0], pts1_scaled[i, 1], 1
        ]

    # print('A: ', A_f)
    # print('A shape: ', A_f.shape)

    u, s, vh = np.linalg.svd(A_f)
    v = vh.T
    f1 = v[:, -1].reshape(3, 3)
    f2 = v[:, -2].reshape(3, 3)

    fun = lambda a: np.linalg.det(a * f1 + (1 - a) * f2)

    a0 = fun(0)
    a1 = (2 / 3) * (fun(1) - fun(-1)) - ((fun(2) - fun(-2)) / 12)
    a2 = 0.5 * fun(1) + 0.5 * fun(-1) - fun(0)
    a3 = (-1 / 6) * (fun(1) - fun(-1)) + (fun(2) - fun(-2)) / 12

    coeff = [a3, a2, a1, a0]
    # coeff = [a0, a1, a2, a3]   // WRONG
    roots = np.roots(coeff)

    # print('roots: ', roots)

    T = np.diag([1 / M, 1 / M, 1])
    F_list = np.zeros((3, 3, 1))

    for root in roots:
        if np.isreal(root):
            a = np.real(root)
            F = a * f1 + (1 - a) * f2
            # F = refineF(F, pts1_scaled, pts2_scaled)
            unscaled_F = T.T.dot(F).dot(T)
            if np.linalg.matrix_rank(unscaled_F) == 3:
                print(
                    '---------------------------------------------------------------------------'
                )
                F = refineF(F, pts1_scaled, pts2_scaled)
                unscaled_F = F
            F_list = np.dstack((F_list, unscaled_F))

    F_list = F_list[:, :, 1:]

    # print('F_list shape: ', F_list.shape)

    return F_list
Example #16
0
def eightpoint(pts1, pts2, M):

    # Scale the correspondence
    A = np.empty((pts1.shape[0], 9))

    pts1 = pts1 / M
    pts2 = pts2 / M
    x1 = pts1[:, 0]
    y1 = pts1[:, 1]
    x2 = pts2[:, 0]
    y2 = pts2[:, 1]

    T = np.array([[1 / M, 0, 0], [0, 1 / M, 0], [0, 0, 1]])

    # Construct A matrix
    A = np.vstack((x2 * x1, x2 * y1, x2, y2 * x1, y2 * y1, y2, x1, y1,
                   np.ones(pts1.shape[0]))).T

    u, s, vh = np.linalg.svd(A)  # Find SVD of AtA
    F = vh[-1].reshape(
        3, 3
    )  # Fundamental Matrix is column corresponding to the least singular values

    F = helper.refineF(F, pts1, pts2)  # Refine F by using local minimization

    # Enforce rank2 constraint a.k.a singularity condition
    F = helper._singularize(F)

    # Unscale the fundamental matrix
    F = np.dot((np.dot(T.T, F)), T)

    return F
def eightpoint(pts1, pts2, M):
    # Replace pass by your implementation
    pts1 = pts1 * 1.0 / M
    pts2 = pts2 * 1.0 / M
    n, temp = pts1.shape
    x1 = pts1[:, 0]
    y1 = pts1[:, 1]
    x2 = pts2[:, 0]
    y2 = pts2[:, 1]
    A1 = (x2 * x1)
    # print(A1.shape)
    A2 = (x2 * y1)
    A3 = x2
    A4 = y2 * x1
    A5 = y2 * y1
    A6 = y2
    A7 = x1
    A8 = y1
    A9 = np.ones(n)
    A = np.vstack((A1, A2, A3, A4, A5, A6, A7, A8, A9))
    A = A.T
    u, s, vh = np.linalg.svd(A)
    f = vh[-1, :]
    F = f.reshape(3, 3)
    F = helper._singularize(F)
    F = helper.refineF(F, pts1, pts2)
    T = np.array([[1. / M, 0, 0], [0, 1. / M, 0], [0, 0, 1]])
    F = np.matmul(T.T, np.matmul(F, T))
    # print(F)
    if (os.path.isfile('q2_1.npz') == False):
        np.savez('q2_1.npz', F=F, M=M)
    return F
Example #18
0
def eightpoint(p1, p2, M):  #  inverse p1 & p2 ??

    assert (p1.shape[0] == p2.shape[0])
    assert (p1.shape[1] == 2)

    #review this
    p1 = p1 / M
    p2 = p2 / M

    # todo: relation between p1 and p2
    n = p1.shape[0]
    A = np.zeros((n, 9))
    A[0:n, 0] = p2[:, 0] * p1[:, 0]
    A[0:n, 1] = p2[:, 0] * p1[:, 1]
    A[0:n, 2] = p2[:, 0]
    A[0:n, 3] = p2[:, 1] * p1[:, 0]
    A[0:n, 4] = p2[:, 1] * p1[:, 1]
    A[0:n, 5] = p2[:, 1]
    A[0:n, 6] = p1[:, 0]
    A[0:n, 7] = p1[:, 1]
    A[0:n, 8] = 1

    u, s, vh = np.linalg.svd(np.array(A))
    F = vh[-1].reshape(3, 3)

    u, s, vh = np.linalg.svd(np.array(F))
    s[2] = 0
    F = u @ np.diag(s) @ vh
    F = helper.refineF(F, p1, p2)
    T = np.diag((1 / M, 1 / M, 1))
    F = T.T @ F @ T

    # np.savez('q2_1.npz', F=F, M=M)  # resave this
    return F
Example #19
0
def eightpoint(pts1, pts2, M):
    # Replace pass by your implementation
    T = np.array([[1 / M, 0, 0], [0, 1 / M, 0], [0, 0, 1]])

    x1 = pts1[:, 0] / M
    y1 = pts1[:, 1] / M

    x1_ = pts2[:, 0] / M
    y1_ = pts2[:, 1] / M

    U = np.vstack((x1 * x1_, y1 * x1_, x1_, x1 * y1_, y1 * y1_, y1_, x1, y1,
                   np.ones((x1.shape[0]))))

    V = np.matmul(U, np.transpose(U))

    w, v = np.linalg.eig(V)

    e_vals, e_vecs = np.linalg.eig(V)

    F = e_vecs[:, np.argmin(e_vals)]

    F = np.reshape(F, (3, 3))

    F = helper.refineF(F, pts1 / M, pts2 / M)

    F = np.transpose(T) @ F @ T

    return F
Example #20
0
def eightpoint(pts1, pts2, M):
    M = float(M)
    pts1 = pts1 / M
    pts2 = pts2 / M

    # curate A matrix
    A = np.zeros((len(pts1), 9))
    A[:, 0] = pts1[:, 0] * pts2[:, 0]
    A[:, 1] = pts1[:, 0] * pts2[:, 1]
    A[:, 2] = pts1[:, 0]
    A[:, 3] = pts1[:, 1] * pts2[:, 0]
    A[:, 4] = pts1[:, 1] * pts2[:, 1]
    A[:, 5] = pts1[:, 1]
    A[:, 6] = pts2[:, 0]
    A[:, 7] = pts2[:, 1]
    A[:, 8] = np.ones(len(pts1))

    # perform SVD, the last row of V is the eigen vector corresponding to the least eigen value
    S, D, V = np.linalg.svd(A)
    F = V[-1].reshape(3, 3)
    # enforce the singularity condition
    F = helper._singularize(F)
    F = helper.refineF(F, pts1, pts2)

    # unscale the fundamental matrix
    T = np.diag((1.0 / M, 1.0 / M, 1.0))
    F = (np.transpose(T).dot(F)).dot(T)
    np.savez('../results/q2_1.npz', F=F, M=M)
    return F
Example #21
0
def sevenpoint(pts1, pts2, M):

    #Normalizing
    pts1 = pts1/M
    pts2 = pts2/M

    # List of F matrices
    Farray = []

    # Forming matrix A
    A = np.zeros([7, 9])
    for i in range(7):
        A[i,0] = pts2[i][0]*pts1[i][0]
        A[i,1] = pts2[i][0]*pts1[i][1]
        A[i,2] = pts2[i][0]
        A[i,3] = pts2[i][1]*pts1[i][0]
        A[i,4] = pts2[i][1]*pts1[i][1]
        A[i,5] = pts2[i][1]
        A[i,6] = pts1[i][0]
        A[i,7] = pts1[i][1]
        A[i,8] = 1

    # print (A.shape)

    u,s,vT = np.linalg.svd(A)
    v = vT.T
    v1 = v[:,-1]
    v2 = v[:,-2]

    F1 = v1.reshape(3,3) 
    F2 = v2.reshape(3,3)


    fun = lambda a: np.linalg.det(a * F1 + (1 - a) * F2)
    a0 =  fun(0)
    a1 = 2/3*(fun(1)-fun(-1))-(fun(2)-fun(-2))/12
    a2 = 0.5*fun(1)+0.5*fun(-1)-fun(0)
    a3 = (fun(1)-fun(-1))/2 - a1

    coeff = [a3, a2, a1, a0]
    # print (coeff)
    a = np.roots(coeff)
    number_roots = 3

    for i in range(3):
        if isinstance(a[i], complex):
            number_roots = 1

    T = np.array([[1/M, 0, 0], [0, 1/M, 0],[0,0,1]])
    a1 = T.T

    for k in range(number_roots):
        temp = a[k] * F1 + (1 - a[k]) * F2
        Farray.append(temp)    
        Farray[k] = helper.refineF(Farray[k],pts1,pts2)
    # #Unnormalizing
        Farray[k] = np.real(np.matmul(np.matmul(a1, Farray[k]),T))

    return Farray
Example #22
0
def eightpoint(pts1, pts2, M):
    x1, y1, x2, y2 = pts1[:, 0] / M, pts1[:, 1] / M, pts2[:, 0] / M, pts2[:, 1] / M
    T = np.array([[1 / M, 0, 0], [0, 1 / M, 0], [0, 0, 1]])
    A = np.vstack((x2 * x1, x2 * y1, x2, y2 * x1, y2 * y1, y2, x1, y1, np.ones(x1.shape))).T
    _, _, vh = np.linalg.svd(A)
    F = vh[-1, :].reshape(3, 3)
    F = helper.refineF(F, pts1 / M, pts2 / M)
    F = helper._singularize(F)
    F = np.dot(np.dot(T.T, F), T)
    return F
def sevenpoint(pts1, pts2, M):
    # Replace pass by your implementation
    pts1 = pts1 / M
    pts2 = pts2 / M

    W = help_func(pts1, pts2)
    u, s, vh = np.linalg.svd(W)

    e1 = vh[-1, :]
    e2 = vh[-2, :]

    e1 = e1.reshape(3, 3)
    e2 = e2.reshape(3, 3)

    F1 = helper.refineF(e1, pts1, pts2)
    F2 = helper.refineF(e2, pts1, pts2)

    def det(alpha):
        out = np.linalg.det(alpha * F1 + (1 - alpha) * F2)
        return out

    res1 = det(0)
    res2 = 2 * (det(1) - det(-1)) / 3 - (det(2) - det(-2)) / 12
    res3 = 0.5 * (det(1)) + 0.5 * (det(-1) - det(0))
    res4 = det(1) - res1 - res2 - res3

    res = [res4, res3, res2, res1]

    M_list = [[1 / M, 0, 0], [0, 1 / M, 0], [0, 0, 1]]

    T_matrix = np.asarray(M_list)

    roots = np.roots(res)

    Fstack = []
    for alpha in roots:
        if np.isreal(alpha) == True:
            new_F = alpha * F1 + (1 - alpha) * F2
            F = np.matmul(T_matrix.T, np.matmul(new_F, T_matrix))
            Fstack.append(F)

    return Fstack
Example #24
0
def sevenpoint(pts1, pts2, M):
    # Replace pass by your implementation
    pts1 = np.divide(pts1, M)
    pts2 = np.divide(pts2, M)
    N = pts1.shape[0]

    pts1_x = pts1[:, 0]
    pts1_y = pts1[:, 1]
    pts2_x = pts2[:, 0]
    pts2_y = pts2[:, 1]

    A = np.vstack(
        (np.multiply(pts1_x, pts2_x), np.multiply(pts1_x, pts2_y), pts1_x,
         np.multiply(pts1_y, pts2_x), np.multiply(pts1_y, pts2_y), pts1_y,
         pts2_x, pts2_y, np.ones(N)))
    A = A.T

    u, s, v = np.linalg.svd(A)
    f1 = v[8, :]
    f2 = v[7, :]

    F1 = np.reshape(f1, (3, 3))
    F2 = np.reshape(f2, (3, 3))

    fun = lambda a: np.linalg.det(a * F1 + (1 - a) * F2)
    a0 = fun(0)
    a1 = (2 * (fun(1) - fun(-1)) / 3.0) - ((fun(2) - fun(-2)) / 12.0)
    a2 = ((fun(1) + fun(-1)) / 2.0) - fun(0)
    a3 = ((fun(1) - fun(-1)) / 2.0) - a1
    coeff = np.array([a3, a2, a1, a0])
    roots = np.roots(coeff)

    t = 1.0 / M
    T = np.array([[t, 0, 0], [0, t, 0], [0, 0, 1]])

    Farray = []
    for i in range(0, len(roots)):
        x = roots[i]
        if x.imag == 0:
            x = x.real
            F = x * F1 + (1 - x) * F2
            F_refined = hp.refineF(F, pts1, pts2)
            F = np.matmul(T.T, np.matmul(F_refined, T))
            Farray.append(F)
        else:
            continue

    Farray = np.stack(Farray, axis=-1)
    return Farray
Example #25
0
def sevenpoint(pts1, pts2, M):

    '''
    Q2.2: Seven Point Algorithm
        Input:  pts1, Nx2 Matrix
                pts2, Nx2 Matrix
                M, a scalar parameter computed as max (imwidth, imheight)
        Output: Farray, a list of estimated fundamental matrix.
    '''
    T = np.array(([1/M,0,0],[0,1/M,0],[0,0,1]))

    pts1_n = [np.dot(T,np.insert(pt1,2,1)).T for pt1 in pts1]
    pts2_n = [np.dot(T,np.insert(pt2,2,1)).T for pt2 in pts2]

    U = []

    for i in range(pts1.shape[0]):
        a = [pts1_n[i][0]*pts2_n[i][0],pts1_n[i][1]*pts2_n[i][0],pts2_n[i][0],pts1_n[i][0]*pts2_n[i][1],pts1_n[i][1]*pts2_n[i][1],pts2_n[i][1],pts1_n[i][0],pts1_n[i][1],1]
        U.append(a)

    U = np.vstack(U)

    u, s, v = np.linalg.svd(U)

    F1 = v[-1].reshape(3,3)
    F2 = v[-2].reshape(3,3)

    fun = lambda a: np.linalg.det(a * F1 + (1 - a) * F2)

    a0 = fun(0)
    a1 = 2*(fun(1)-fun(-1))/3-(fun(2)-fun(-2))/12
    a2 = 0.5*fun(1)+0.5*fun(-1)-fun(0)
    a3 = ((fun(2)-fun(-2))-(2*(fun(1)-fun(-1))))/12

    roots = np.roots([a3,a2,a1,a0])

    F = (1-roots[0]*F1) + (roots[0]*F2)

    pts1_n = np.array(pts1_n)
    pts2_n = np.array(pts2_n)

    pts1_n = np.delete(pts1_n,-1,axis=1)
    pts2_n = np.delete(pts2_n,-1,axis=1)

    F = hp.refineF(F,np.array(pts1_n),np.array(pts2_n))
    F = np.dot(T.T,np.dot(F,T))

    return F
Example #26
0
def sevenpoint(pts1, pts2, M):
    x1, y1, x2, y2 = pts1[:, 0] / M, pts1[:, 1] / M, pts2[:, 0] / M, pts2[:, 1] / M
    T = np.array([[1 / M, 0, 0], [0, 1 / M, 0], [0, 0, 1]])
    A = np.vstack((x2 * x1, x2 * y1, x2, y2 * x1, y2 * y1, y2, x1, y1, np.ones(x1.shape))).T
    _, _, vh = np.linalg.svd(A)
    F1 = vh[-1, :].reshape(3, 3)
    F2 = vh[-2, :].reshape(3, 3)
    a0 = solution(0.0, F1, F2)
    a1 = 2.0 * (solution(1.0, F1, F2) - solution(-1.0, F1, F2)) / 3.0 - (solution(2.0, F1, F2) - solution(-2.0, F1, F2)) / 12.0
    a2 = (solution(1.0, F1, F2) + solution(-1.0, F1, F2)) / 2.0 - a0
    a3 = (solution(1.0, F1, F2) - solution(-1.0, F1, F2)) / 2.0 - a1
    alphas = np.roots(np.array([a3, a2, a1, a0]))
    Farray = [alpha * F1 + (1 - alpha) * F2 for alpha in alphas]
    Farray = [helper.refineF(F, pts1 / M, pts2 / M) for F in Farray]
    Farray = [np.dot(np.dot(T.T, F), T) for F in Farray]
    return Farray
Example #27
0
def eightpoint(pts1, pts2, M):
    #TODO :: check  if 1 and 2 are not inversed!
    #normalize pts1 and pts2
    # mean_pt1 = findCenter(pts1)
    # mean_pt2 = findCenter(pts2)
    # T1 = np.eye(3)
    # T2 = np.eye(3)

    # T1[0] = [1 / M, 0, -mean_pt1[0] / M]
    # T1[1] = [0, 1 / M, -mean_pt1[1] / M]
    # T2[0] = [1 / M, 0, -mean_pt2[0] / M]
    # T2[1] = [0, 1 / M, -mean_pt2[1] / M]
    TT = np.eye(3)
    TT[0] = [2 / M, 0, -1]
    TT[1] = [0, 2/M, -1]

    pts1_norm = TT @ np.vstack((pts1.T,np.ones(pts1.shape[0]))) #dim: 3 x N
    pts2_norm = TT @ np.vstack((pts2.T,np.ones(pts2.shape[0]))) #dim: 3 x N

    # eight point algorithm
    N = pts1.shape[0]
    U = np.ones((N,9))
    x1 = pts1_norm[0,:]
    y1 = pts1_norm[1,:]
    x2 = pts2_norm[0,:]
    y2 = pts2_norm[1,:]
    U[:,0] = x1*x2
    U[:,1] = x1*y2
    U[:,2] = x1
    U[:,3] = y1*x2
    U[:,4] = y1*y2
    U[:,5] = y1
    U[:,6] = x2
    U[:,7] = y2

    _,_,vh = np.linalg.svd(U)
    f = np.reshape(vh[8],(3,3))
    F = (1 / f[-1,-1]) * f #not sure if we need to normalized
    #enforce singularity condition

    F = refineF(F, pts1_norm[:2].T, pts2_norm[:2].T)
    F = _singularize(F)

    F = TT.T @ F @ TT

    return F
Example #28
0
def eightpoint(pts1, pts2, M):
    pts1 = pts1.astype('float64')
    pts2 = pts2.astype('float64')
    pts1 /= M
    pts2 /= M
    A = [pts2[:,0]*pts1[:,0], pts2[:,0]*pts1[:,1], pts2[:,0], pts2[:,1]*pts1[:,0], pts2[:,1]*pts1[:,1], pts2[:,1], pts1[:,0], pts1[:,1], np.ones_like(pts1[:,0])]
    A = np.asarray(A).T
    U, S, V = np.linalg.svd(A)
    F = V[-1].reshape(3,3)
    U, S, V = np.linalg.svd(F)
    S[-1] = 0.0
    F = U.dot(np.diag(S)).dot(V)
    F = refineF(F, pts1, pts2)
    t = np.array([[1.0/M,0.0,0.0],[0.0,1.0/M,0.0],[0.0,0.0,1.0]])
    F = t.T.dot(F).dot(t)
    # np.savez('q2_1.npz', F=F, M=M)
    return F
Example #29
0
def sevenpoint(pts1, pts2, M):

    T = np.array(([1 / M, 0, 0], [0, 1 / M, 0], [0, 0, 1]))

    pts1_n = [np.dot(T, np.insert(pt1, 2, 1)).T for pt1 in pts1]
    pts2_n = [np.dot(T, np.insert(pt2, 2, 1)).T for pt2 in pts2]

    U = []

    for i in range(pts1.shape[0]):
        a = [
            pts1_n[i][0] * pts2_n[i][0], pts1_n[i][1] * pts2_n[i][0],
            pts2_n[i][0], pts1_n[i][0] * pts2_n[i][1],
            pts1_n[i][1] * pts2_n[i][1], pts2_n[i][1], pts1_n[i][0],
            pts1_n[i][1], 1
        ]
        U.append(a)

    U = np.vstack(U)

    u, s, v = np.linalg.svd(U)

    F1 = v[-1].reshape(3, 3)
    F2 = v[-2].reshape(3, 3)

    fun = lambda a: np.linalg.det(a * F1 + (1 - a) * F2)

    a0 = fun(0)
    a1 = 2 * (fun(1) - fun(-1)) / 3 - (fun(2) - fun(-2)) / 12
    a2 = 0.5 * fun(1) + 0.5 * fun(-1) - fun(0)
    a3 = ((fun(2) - fun(-2)) - (2 * (fun(1) - fun(-1)))) / 12

    roots = np.roots([a3, a2, a1, a0])

    F = (1 - roots[0] * F1) + (roots[0] * F2)

    pts1_n = np.array(pts1_n)
    pts2_n = np.array(pts2_n)

    pts1_n = np.delete(pts1_n, -1, axis=1)
    pts2_n = np.delete(pts2_n, -1, axis=1)

    F = hp.refineF(F, np.array(pts1_n), np.array(pts2_n))
    F = np.dot(T.T, np.dot(F, T))

    return F
Example #30
0
def sevenpoint(pts1, pts2, M):
    # Replace pass by your implementation
    T = np.array([[1 / M, 0, 0], [0, 1 / M, 0], [0, 0, 1]])

    x1 = pts1[:, 0] / M
    y1 = pts1[:, 1] / M

    x1_ = pts2[:, 0] / M
    y1_ = pts2[:, 1] / M

    U = np.vstack((x1 * x1_, y1 * x1_, x1_, x1 * y1_, y1 * y1_, y1_, x1, y1,
                   np.ones((x1.shape[0]))))

    u, s, v = np.linalg.svd(U.T)
    f1 = v[8, :]
    f2 = v[7, :]

    F1 = np.reshape(f1, (3, 3))
    F2 = np.reshape(f2, (3, 3))

    l = symbols('l')

    F = (l) * F1 + (1 - l) * F2

    F_ = sympy.Matrix(F)

    eq = F_.det()

    roots = solve(eq)

    roots = np.fromiter(roots, dtype=complex)

    Farray = []
    for i in range(0, len(roots)):
        x = roots[i]
        if x.imag == 0:
            x = x.real
            F = x * F1 + (1 - x) * F2
            F = helper.refineF(F, pts1 / M, pts2 / M)
            F = np.transpose(T) @ F @ T
            Farray.append(F)

    Farray = np.array(Farray)
    return Farray