Esempio n. 1
0
def test_ransac_fundamental_matrix_fit():

    x_0s = np.load('../data/points2_a.npy')
    x_1s = np.load('../data/points2_b.npy')
    error_tolerance = 10.0

    F, inliers_x_0, inliers_x_1 = ransac_fundamental_matrix(x_0s, x_1s)

    x_0s, x_1s = two_view_data.preprocess_data(x_0s, x_1s)
    res = fundamental_matrix.signed_point_line_errors(x_0s, F, x_1s)
    res = np.abs(res)
    res = np.average(res)
    print('average residual = ', res)

    assert res < error_tolerance
Esempio n. 2
0
def test_ransac_fundamental_matrix_error():

    points_a = np.load('../data/pointsa.npy')
    points_b = np.load('../data/pointsb.npy')
    error_tolerance = 0.5

    F, inliers_x_0, inliers_x_1 = ransac_fundamental_matrix(points_a, points_b)

    x_0s, x_1s = two_view_data.preprocess_data(inliers_x_0, inliers_x_1)
    res = fundamental_matrix.signed_point_line_errors(x_0s, F, x_1s)
    res = np.abs(res)
    res = np.average(res)
    print('average residual = ', res)

    assert res < error_tolerance
Esempio n. 3
0
def test_ransac_find_inliers():

    F = [[-2.49684084e-08, 7.37279178e-07, -5.99944364e-05],
         [-6.83245580e-07, -2.23634574e-08, 3.47641240e-03],
         [1.65302076e-04, -3.16334642e-03, -3.80986850e-02]]
    F = np.array(F)
    x_1s = np.load('../data/inliers2_a.npy')
    x_0s = np.load('../data/inliers2_b.npy')
    outliers = [1, 4, 10, 3, 5]
    for outlier in outliers[:-2]:
        x_0s[outlier] += 3
    for outlier in outliers[-2:]:
        x_0s[outlier] -= 3
    x_0s, x_1s = two_view_data.preprocess_data(x_0s, x_1s)

    inliers = ransac.find_inliers(x_0s, F, x_1s, 2)

    print(inliers)

    assert outliers not in inliers
    assert inliers.shape[0] == x_0s.shape[0] - len(outliers)
Esempio n. 4
0
def ransac_fundamental_matrix(x_0s, x_1s):
    """Find the fundamental matrix with RANSAC.

    Use RANSAC to find the best fundamental matrix by
    randomly sampling interest points. You will call your
    solve_F() from part 2 of this assignment
    and calculate_num_ransac_iterations().

    You will also need to define a new function (see above) for finding
    inliers after you have calculated F for a given sample.

    Tips:
        0. You will need to determine your P, k, and p values.
            What is an acceptable rate of success? How many points
            do you want to sample? What is your estimate of the correspondence
            accuracy in your dataset?
        1. A potentially useful function is numpy.random.choice for
            creating your random samples
        2. You will want to call your function for solving F with the random
            sample and then you will want to call your function for finding
            the inliers.
        3. You will also need to choose an error threshold to separate your
            inliers from your outliers. We suggest a threshold of 1.

    Args:
    -   x_0s: A numpy array of shape (N, 2) representing the coordinates
                   of possibly matching points from the left image
    -   x_1s: A numpy array of shape (N, 2) representing the coordinates
                   of possibly matching points from the right image
    Each row is a proposed correspondence (e.g. row #42 of x_0s is a point that
    corresponds to row #42 of x_1s)

    Returns:
    -   best_F: A numpy array of shape (3, 3) representing the best fundamental
                matrix estimation
    -   inliers_x_0: A numpy array of shape (M, 2) representing the subset of
                   corresponding points from the left image that are inliers with
                   respect to best_F
    -   inliers_x_1: A numpy array of shape (M, 2) representing the subset of
                   corresponding points from the right image that are inliers with
                   respect to best_F

    """

    best_F = None
    inliers_x_0 = None
    inliers_x_1 = None

    ##############################
    # TODO: Student code goes here
    "parameters"
    P = 0.999
    k = 9
    p = 0.8
    threshold = 1
    num_iteration = calculate_num_ransac_iterations(P, k, p)
    best_len = 0
    best_res = 100

    for i in range(num_iteration):
        samples = np.random.choice(len(x_0s), size=k, replace=False)
        x_0_sample, x_1_sample = two_view_data.preprocess_data(
            x_0s[samples], x_1s[samples])

        F = solve_F(x_0s[samples], x_1s[samples])
        inliers = find_inliers(x_0_sample, F, x_1_sample, threshold)
        res = fundamental_matrix.signed_point_line_errors(x_0s, F, x_1s)
        res = np.average(np.abs(res))

        if len(inliers) > best_len or (len(inliers) == best_len
                                       and res < best_res):
            best_F = F
            inliers_x_0 = x_0s[samples]
            inliers_x_1 = x_1s[samples]
            best_len = len(inliers)
            best_res = res

    # raise NotImplementedError
    ##############################

    return best_F, inliers_x_0, inliers_x_1
def solve_F(x_0s, x_1s):
    x_0s, x_1s = two_view_data.preprocess_data(x_0s, x_1s)
    p0 = (skew(1, 0, 0)).flatten()  # stereo
    result = optimize(p0, x_0s, x_1s)
    F = np.reshape(result, (3, 3))
    return F
Esempio n. 6
0
def ransac_fundamental_matrix(x_0s: int, 
                              x_1s: int) -> Tuple[
                                  np.ndarray, np.ndarray, np.ndarray]:
    """Find the fundamental matrix with RANSAC.

    Use RANSAC to find the best fundamental matrix by
    randomly sampling interest points. You will call your
    solve_F() from part 2 of this assignment
    and calculate_num_ransac_iterations().

    You will also need to define a new function (see above) for finding
    inliers after you have calculated F for a given sample.

    Tips:
        0. You will need to determine your P, k, and p values.
            What is an acceptable rate of success? How many points
            do you want to sample? What is your estimate of the correspondence
            accuracy in your dataset?
        1. A potentially useful function is numpy.random.choice for
            creating your random samples
        2. You will want to call your function for solving F with the random
            sample and then you will want to call your function for finding
            the inliers.
        3. You will also need to choose an error threshold to separate your
            inliers from your outliers. We suggest a threshold of 1.

    Args:
    -   x_0s: A numpy array of shape (N, 2) representing the coordinates
                   of possibly matching points from the left image
    -   x_1s: A numpy array of shape (N, 2) representing the coordinates
                   of possibly matching points from the right image
    Each row is a proposed correspondence (e.g. row #42 of x_0s is a point that
    corresponds to row #42 of x_1s)

    Returns:
    -   best_F: A numpy array of shape (3, 3) representing the best fundamental
                matrix estimation
    -   inliers_x_0: A numpy array of shape (M, 2) representing the subset of
                   corresponding points from the left image that are inliers with
                   respect to best_F
    -   inliers_x_1: A numpy array of shape (M, 2) representing the subset of
                   corresponding points from the right image that are inliers with
                   respect to best_F

    """

    best_F = None
    inliers_x_0 = None
    inliers_x_1 = None

    ##############################
    # TODO: Student code goes here

    # Defined success criteria and find the number of RANSAC iterations we need to run
    P = 0.999     # success rate
    k = 20         # sample size
    p = 0.90      # individual success probability
    threshold = 1 # error threshold
    S = calculate_num_ransac_iterations(P, k, p)
    Fs = []
    inliers_num = []

    # Preprocess the data (N, 2) -> (N, 3)
    x_0s3, x_1s3 = two_view_data.preprocess_data(x_0s, x_1s)

    # Run iterations
    for i in range(S):
        # Select k-sized random sample
        rand_idx = np.random.choice(np.arange(x_0s.shape[0]), k)

        x0i, x1i = x_0s[rand_idx], x_1s[rand_idx]
        F = solve_F(x0i, x1i)
        inliers = find_inliers(x_0s3, F, x_1s3, threshold)
        Fs.append(F)
        inliers_num.append(len(inliers))

    best_F = Fs[np.argmax(inliers_num)]
    best_inliers_idx = find_inliers(x_0s3, best_F, x_1s3, threshold)
    inliers_x_0, inliers_x_1 = x_0s[best_inliers_idx], x_1s[best_inliers_idx]

    ##############################

    return best_F, inliers_x_0, inliers_x_1