コード例 #1
0
def eightpoint(pts1, pts2, M):

    pl = pts1 * 1.0 / M
    pr = pts2 * 1.0 / M

    xl = pl[:, 0]  #N*1
    yl = pl[:, 1]  #N*1

    xr = pr[:, 0]  #N*1
    yr = pr[:, 1]  #N*1

    U = np.vstack((xr * xl, xr * yl, xr, yr * xl, yr * yl, yr, xl, yl,
                   np.ones(pts1.shape[0])))
    U = U.T
    u, s, vh = np.linalg.svd(U)
    F = vh[-1, :]
    F = F.reshape(3, 3)
    #You must enforce the singularity condition of the F before unscaling

    #refine the solution by using local minimization
    F = util._singularize(F)
    F = util.refineF(F, pl, pr)

    #now we need to unnormalize F
    T = np.array([[1. / M, 0, 0], [0, 1. / M, 0], [0, 0, 1]])
    F = T.T @ F @ T

    #print("F1 is:",F)
    if (os.path.isfile('q2_1.npz') == False):
        np.savez('q2_1.npz', F=F, M=M)
    return F
コード例 #2
0
def eightpoint(pts1, pts2, M):
    x1 = pts1[:, 0] / M
    y1 = pts1[:, 1] / M
    x2 = pts2[:, 0] / M
    y2 = pts2[:, 1] / M

    U = np.vstack((x2 * x1, x2 * y1, x2, y2 * x1, y2 * y1, y2, x1, y1,
                   np.ones(np.shape(x1)))).T
    T = np.array([[1 / M, 0, 0], [0, 1 / M, 0], [0, 0, 1]])

    u, s, v = np.linalg.svd(U)
    F = v[-1, :].reshape(3, 3)

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

    F = T.T @ F @ T

    return F
コード例 #3
0
def eightpoint(pts1, pts2, M):

    assert pts1.shape == pts2.shape, "Points lists should be same size"
    assert pts1.shape[0] >= 8, "Need at least 8 points for 8 point method"

    pts1 = np.asarray(pts1)
    pts2 = np.asarray(pts2)

    #normalize
    normT = np.array([[2 / M, 0, -1], [0, 2 / M, -1], [0, 0, 1]])

    #homogenous
    hom1 = np.hstack((pts1, np.ones((pts1.shape[0], 1))))
    hom2 = np.hstack((pts2, np.ones((pts2.shape[0], 1))))

    #all col 3 is ones by design
    n1 = np.dot(normT, hom1.T).T
    n2 = np.dot(normT, hom2.T).T

    #calculate U
    U = np.array([[n1[row, 0] * n2[row, 0], n1[row, 1] * n2[row, 0], n2[row, 0], \
            n1[row, 0] * n2[row, 1], n1[row, 1] * n2[row, 1], n2[row, 1], \
            n1[row, 0], n1[row, 1], 1    ] for row in range(n1.shape[0])])

    #svd to get nonsingular F
    _, _, vh = np.linalg.svd(U)

    F_nonsing = np.reshape(vh[8, :], (3, 3))

    #make F singular
    w, s, vh2 = np.linalg.svd(F_nonsing)

    F = (w @ np.diag([s[0], s[1], 0])) @ vh2

    #refine
    F_refined = refineF(F, n1[:, :2], n2[:, :2])

    #unscale
    F_unscaled = normT.T @ F_refined @ normT

    return F_unscaled
コード例 #4
0
def eightpoint(pts1, pts2, M):
    n = pts1.shape[0]

    # Normalize the points
    pts1 = pts1 * 1.0 / M
    pts2 = pts2 * 1.0 / M
    T = np.array([[1.0 / M, 0, 0], [0, 1.0 / M, 0], [0, 0, 1.0]])

    x_l, y_l = pts2[:, 0], pts2[:, 1]
    x_r, y_r = pts1[:, 0], pts1[:, 1]
    A = np.array([
        x_l * x_r, x_l * y_r, x_l, x_r * y_l, y_l * y_r, y_l, x_r, y_r,
        np.ones(n)
    ]).T
    U, S, VT = np.linalg.svd(A)
    F = np.reshape(VT.T[:, -1], (3, 3))

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

    # Unscale F
    F = np.dot(np.dot(T.T, F), T)
    return F
コード例 #5
0
def eightpoint(pts1, pts2, M):
    N = len(pts1)

    # Scale the data
    pts1n = pts1 / M
    pts2n = pts2 / M

    # Create A
    A = np.zeros((N, 9))
    for i in range(N):
        pt1 = pts1n[i]
        x, y = pt1[0], pt1[1]
        pt2 = pts2n[i]
        x_, y_ = pt2[0], pt2[1]

        A[i, :] = [x_ * x, x_ * y, x_, y_ * x, y_ * y, y_, x, y, 1]

    # Find fundamental matrix
    ATA = A.T @ A
    _, _, V_T = np.linalg.svd(ATA)
    # Last column of V is last row of V_T
    F_est = V_T[-1, :].reshape((3, 3))
    # print(F_est)

    # Enforce rank2 constraint
    U, S, V_T = np.linalg.svd(F_est, full_matrices=True)
    S[-1] = 0.0
    F = U @ np.diag(S) @ V_T
    # print(F)

    F = refineF(F, pts1n, pts2n)

    # Unnormalize
    T = np.array([[1. / M, 0, 0], [0, 1. / M, 0], [0, 0, 1]], dtype=np.float)
    F = T.T @ F @ T
    # F /= F[-1, -1]
    # print(F)
    return F