Ejemplo n.º 1
0
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
Ejemplo n.º 2
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
Ejemplo n.º 3
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
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
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
Ejemplo n.º 6
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
Ejemplo n.º 7
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
Ejemplo n.º 8
0
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
Ejemplo n.º 9
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
Ejemplo n.º 10
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
Ejemplo n.º 11
0
def sevenpoint(pts1, pts2, M):

    #pts1 and pts2 are 7 x 2
    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

    # seven 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)
    f1 = np.reshape(vh[-1],(3,3))
    f2 = np.reshape(vh[-2],(3,3))

    func = lambda x: np.linalg.det((1-x) * f1 + x * f2)
    d = func(0)
    c = 2 * (func(1) - func(-1)) / 3 - (func(2) - func(-2)) / 12
    b = (func(1) + func(-1)) / 2 - d
    a = (func(1) - func(-1)) / 2 - c
    x = np.roots([a, b, c, d])


    Fs = [(1 - x_) * f1 + x_ * f2 for x_ in x]
    Fs = [refineF(F, pts1_norm[:2].T, pts2_norm[:2].T) for F in Fs]
    Fs = [_singularize(F) for F in Fs]
    Fs = [TT.T @ F @ TT for F in Fs]

    return Fs
Ejemplo n.º 12
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]])
    pts1 = pts1 / M
    pts2 = pts2 / M
    A = []
    for i in range(pts1.shape[0]):
        A.append(
            np.array([
                pts1[i][0] * pts2[i][0], pts1[i][0] * pts2[i][1], pts1[i][0],
                pts1[i][1] * pts2[i][0], pts1[i][1] * pts2[i][1], pts1[i][1],
                pts2[i][0], pts2[i][1], 1
            ]))
    A = np.vstack(A)
    _, _, v = np.linalg.svd(A)
    F = v[-1, :].reshape(3, 3)
    F = helper._singularize(helper.refineF(F, pts1, pts2))
    unnormalized_F = np.dot(np.dot(T.T, F), T)
    return unnormalized_F
Ejemplo n.º 13
0
def sevenpoint(pts1, pts2, M, refine=True):
    assert (pts1.shape[0] == 7)
    assert (pts1.shape[0] == pts2.shape[0])

    # Normalize points
    p1 = pts1/M
    p2 = pts2/M
    T = np.diag([1/M, 1/M, 1])

    # Create A matrix
    A = np.vstack([p2[:, 0]*p1[:, 0], p2[:, 0]*p1[:, 1], p2[:, 0],
                   p2[:, 1]*p1[:, 0], p2[:, 1]*p1[:, 1], p2[:, 1],
                   p1[:, 0], p1[:, 1], np.ones(p1.shape[0])])

    U, S, V = np.linalg.svd(A.T)
    F1 = V[-1, :].reshape(3, 3)
    F2 = V[-2, :].reshape(3, 3)

    # 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(2) - fun(-2))/12 - (fun(1) - fun(-1))/6

    # Find roots of the polynomial
    roots = np.roots([a3, a2, a1, a0])
    roots = np.real(roots[np.isreal(roots)])

    Farray = []
    for a in roots:
        F = a*F1 + (1 - a)*F2
        if refine:
            F = helper._singularize(F)      # Singularize
            F = helper.refineF(F, p1, p2)   # Refine F
        F = T.T @ F @ T                     # Denormalize F
        Farray.append(F)

    return Farray
Ejemplo n.º 14
0
def eightpoint(pts1, pts2, M):
    assert(pts1.shape[0] >= 8)
    assert(pts1.shape[0] == pts2.shape[0])

    # Normalize points
    p1 = pts1/M
    p2 = pts2/M
    T = np.diag([1/M, 1/M, 1])

    # Create A matrix
    A = np.vstack([p1[:, 0]*p2[:, 0], p1[:, 0]*p2[:, 1], p1[:, 0],
                   p1[:, 1]*p2[:, 0], p1[:, 1]*p2[:, 1], p1[:, 1],
                   p2[:, 0], p2[:, 1], np.ones(p1.shape[0])])

    U, S, V = np.linalg.svd(A.T)
    F = V[-1, :].reshape(3, 3)

    F = helper._singularize(F)      # Singularize
    F = helper.refineF(F, p1, p2)   # Refine F
    F = T.T @ F @ T                 # Denormalize F

    return F
Ejemplo n.º 15
0
def eightpoint(pts1, pts2, M):
    x1 = pts1[:, 0] / M
    y1 = pts1[:, 1] / M
    x2 = pts2[:, 0] / M
    y2 = pts2[:, 1] / M
    T = np.array([[1 / M, 0, 0], [0, 1 / M, 0], [0, 0, 1]])
    A = []

    for i in range(len(x1)):
        A.append([
            x1[i] * x2[i], x1[i] * y2[i], x1[i], y1[i] * x2[i], y1[i] * y2[i],
            y1[i], x2[i], y2[i], 1
        ])

    u, s, v_h = np.linalg.svd(A)
    f = v_h[-1, :]
    F = f.reshape((3, 3))

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

    F_unnorm = np.transpose(T) @ (F @ T)
    return F_unnorm
Ejemplo n.º 16
0
def eightpoint(pts1, pts2, M):
    # Replace pass by your implementation
    # normalize points
    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]
    F = V[-1,:].reshape(3,3)

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

    # un normalize data
    T = np.diag([1.0/M, 1.0/M, 1.0])
    F = np.dot(np.transpose(T), np.dot(F,T))

    # save results
    np.savez('../results/q2_1.npz', F=F, M=M)

    return F