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 """ ############################## M = fundamental_matrix.signed_point_line_errors(x_0s, F, x_1s) inliers = [] for point_line in range(0, len(M), 2): if M[point_line] < threshold and M[point_line + 1] < threshold: inliers.append(point_line // 2) inliers = np.asarray(inliers) ############################## return inliers
def test_signed_point_line_errors(self): """Check line-point errors.""" errors = signed_point_line_errors(self.x_0s, self.F, self.x_1s) actual_errors = [-0.010742, -0.020253, -0.110908, -0.2068 , 0.8076 , 1.453266, -0.638929, -1.066478, 0.431201, 0.55922 , -0.193313, -0.249138, 0.536447, 0.891095, -0.065045, -0.10857 , -0.07294 , -0.139982, -0.711676, -1.175722] np.testing.assert_array_almost_equal(errors, actual_errors, decimal=1)
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 # raise NotImplementedError inliers_raw = [] # errors is a 1d array with alternating d(Fx_1,x_0) and d(FTx_0,x_1) errors = fundamental_matrix.signed_point_line_errors(x_0s, F, x_1s) for i in range(0, len(errors), 2): error = errors[i] + errors[i + 1] if abs(errors[i]) <= threshold and abs(errors[i + 1]) <= threshold: inliers_raw.append(i // 2) inliers = np.asarray(inliers_raw) ############################## return inliers
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)
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
def objective_function(p, x_0s, x_1s): """Objective with new parameterization.""" F = np.reshape(p, (3, 3)) return signed_point_line_errors(x_0s, F, x_1s)
def test_signed_point_line_errors(self): """ Check line-point errors.""" errors = signed_point_line_errors(self.x_0s, self.F, self.x_1s) self.assertEqual(errors, [0.0]*18)
def test_signed_point_line_errors(self): """ Check line-point errors.""" errors = signed_point_line_errors(self.x_0s, self.F, self.x_1s) np.testing.assert_array_almost_equal(errors, [0.0]*18)