コード例 #1
0
ファイル: ransac.py プロジェクト: KokJianYu/CS4476-Gatech-
def find_inliers(x_0s, F, x_1s, threshold):
    """ Find the inliers' indices for a given model.

    There are multiple methods you could use for calculating the error
    to determine your inliers vs outliers at each pass. CoHowever, we suggest
    using the line to point distance function we wrote for the
    optimization in part 2.

    Args:
    -   x_0s: A numpy array of shape (N, 2) representing the coordinates
                   of possibly matching points from the left image
    -   F: The proposed fundamental matrix
    -   x_1s: A numpy array of shape (N, 2) representing the coordinates
                   of possibly matching points from the right image
    -   threshold: the maximum error for a point correspondence to be
                    considered an inlier
    Each row in x_1s and x_0s is a proposed correspondence (e.g. row #42 of x_0s is a point that
    corresponds to row #42 of x_1s)

    Returns:
    -    inliers: 1D array of the indices of the inliers in x_0s and x_1s

    """
    ##############################
    # TODO: Student code goes here
    if x_0s.shape[1] == 2 and x_1s.shape[1] == 2:
        x_0s, x_1s = two_view_data.preprocess_data(x_0s, x_1s)

    errors = fundamental_matrix.signed_point_line_errors(x_0s, F, x_1s)
    errors = np.absolute(errors)
    inliers = []
    for i in range(x_0s.shape[0]):
        if errors[i * 2] < threshold and errors[i * 2 + 1] < threshold:
            inliers.append(i)
    ##############################

    return np.array(inliers)
コード例 #2
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

    """
    number_of_iteration = calculate_num_ransac_iterations(0.999, 9, 0.7)
    x0_concat, x1_concat = np.concatenate((x_0s, np.ones((len(x_0s), 1))),
                                          axis=1), np.concatenate(
                                              (x_1s, np.ones((len(x_1s), 1))),
                                              axis=1)

    minavgE = 99999
    for _ in range(number_of_iteration):
        rand_sample = np.random.choice(len(x_0s), 9, replace=False)
        x0_process, x1_process = two_view_data.preprocess_data(
            x_0s[rand_sample], x_1s[rand_sample])
        F = solve_F(x_0s[rand_sample], x_1s[rand_sample])
        found_inliers = find_inliers(x0_process, F, x1_process, 1)

        if len(found_inliers) >= 0:
            errors = fundamental_matrix.signed_point_line_errors(
                x0_concat[found_inliers], F, x1_concat[found_inliers])
            for i in range(len(errors)):
                errors[i] = abs(errors[i])
            average_error = sum(errors) / len(errors)
            if average_error < minavgE:
                minavgE, best_F, inliers_x_0, inliers_x_1 = average_error, F, x_0s[
                    found_inliers], x_1s[found_inliers]
    ##############################

    return best_F, inliers_x_0, inliers_x_1
コード例 #3
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

    """
    P = 0.999
    k = 30
    p = 0.9
    threshold = 1
    num_points = x_0s.shape[0]
    iterations = calculate_num_ransac_iterations(P, k, p)

    max_inliers = 0
    best_inlier_indices = None
    best_F = None
    for _ in range(iterations):
        sample_indices = np.random.choice(a=num_points, size=k, replace=False)
        sample_x0 = x_0s[sample_indices]
        sample_x1 = x_1s[sample_indices]
        F = solve_F(sample_x0, sample_x1)
        sample_x0, sample_x1 = two_view_data.preprocess_data(
            sample_x0, sample_x1)
        inlier_indices = find_inliers(sample_x0, F, sample_x1, threshold)
        if (inlier_indices.shape[0] > max_inliers):
            max_inliers = inlier_indices.shape[0]
            best_inlier_indices = inlier_indices
            best_F = F
    inliers_x_0 = x_0s[best_inlier_indices]
    inliers_x_1 = x_1s[best_inlier_indices]
    return best_F, inliers_x_0, inliers_x_1
コード例 #4
0
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
コード例 #5
0
ファイル: ransac.py プロジェクト: jielyugt/camera_projection
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

    """
    ##############################
    # TODO: Student code goes here
    # raise NotImplementedError

    # make coordinates homogeneous
    x_0s_3d, x_1s_3d = two_view_data.preprocess_data(x_0s, x_1s)

    # solve_F(x_0s, x_1s) takes in two lists of paired homogenuous coordinates and returns an estimated fundamental matrix
    prob_success = 0.999  # pick one yourself
    sample_size = 9  # ?
    ind_prob_correct = 0.9  # get this for SIFT
    num_samples = calculate_num_ransac_iterations(prob_success, sample_size,
                                                  ind_prob_correct)
    print('\n\n{}\n\n'.format(num_samples))

    best_F = None
    best_F_inliers_num = -1
    inliers_x_0 = None
    inliers_x_1 = None

    indices = np.arange(len(x_0s))
    for each in range(num_samples):
        sample = np.random.choice(indices, sample_size, replace=False)
        F_candidate = solve_F(x_0s[sample], x_1s[sample])
        inliers_indices = find_inliers(x_0s_3d, F_candidate, x_1s_3d, 1)
        num_inliers = len(inliers_indices)
        if num_inliers > best_F_inliers_num:
            best_F_inliers_num = num_inliers
            best_F = F_candidate
            inliers_x_0 = x_0s[inliers_indices]
            inliers_x_1 = x_1s[inliers_indices]

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

    return best_F, inliers_x_0, inliers_x_1
コード例 #6
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_model: 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_model
    -   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_model

    """
    ##############################
    # TODO: Student code goes here
    best_model = np.zeros((3, 3))
    num_inliers = 0
    iterations = calculate_num_ransac_iterations(.97, 9, .68)

    for i in range(iterations):
        length = x_0s.shape[0]
        p_idx = np.random.choice(length, 11, replace=False)
        p_x0s = []
        p_x1s = []
        for idx in p_idx:
            p_x0s.append(x_0s[idx])
            p_x1s.append(x_1s[idx])
        p_x0s = np.array(p_x0s)
        p_x1s = np.array(p_x1s)
        curr_inliers = find_inliers(
            two_view_data.preprocess_data(x_0s, x_1s)[0],
            solve_F(p_x0s, p_x1s),
            two_view_data.preprocess_data(x_0s, x_1s)[1], 1)

        if (curr_inliers.shape[0] > num_inliers):
            num_inliers = curr_inliers.shape[0]
            best_model = solve_F(p_x0s, p_x1s)
            inliers_x_0 = []
            inliers_x_1 = []
            for inlier in curr_inliers:
                inliers_x_0.append(x_0s[inlier])
                inliers_x_1.append(x_1s[inlier])

        inliers_x_0 = np.array(inliers_x_0)
        inliers_x_1 = np.array(inliers_x_1)
        best_F = best_model
        inliers_x_0 = np.array(inliers_x_0)
        inliers_x_1 = np.array(inliers_x_1)
    ##############################

    return best_F, inliers_x_0, inliers_x_1