def ransac_homography(pos1, pos2, num_iters, inlier_tol): """ Takes in two arrays of points, and perform a ransac proccess inorder to find the best homography matrix which transforms the most points in pos1 to their corrsponding points in pos2 (successful transformation is counted by inlier tol) :param pos1: An Array, with shape (N,2) containing n rows of [x,y] coordinates of matched points. :param pos2: see pos1 :param num_iters: Number of RANSAC iterations to perform. :param inlier_tol: inlier tolerance threshold. :return: H12 − A 3x3 normalized homography matrix. inliers − An Array with shape (S,) where S is the number of inliers, containing the indices in pos1/pos2 of the maximal set of inlier matches found. """ N = range(pos1.shape[0]) inliers = np.array([]) for n in range(num_iters): rnd_ind = np.random.choice(N, 4) H = sol4_add.least_squares_homography(pos1[rnd_ind, :], pos2[rnd_ind, :]) if H is None: continue sqdiff = np.power( np.linalg.norm(apply_homography(pos1, H) - pos2, axis=1), 2) inlierstemp = np.where(sqdiff < inlier_tol)[0] if inlierstemp.size > inliers.size: inliers = inlierstemp return sol4_add.least_squares_homography(pos1[inliers, :], pos2[inliers, :]), inliers
def ransac_homography(pos1: np.ndarray, pos2: np.ndarray, num_iters: int, inlier_tol: np.float32) -> tuple: """ apply RANSAC homography fitting :param pos1: an array with n rows of [x,y] coordinates of matched points of first image. :param pos2: an array with n rows of [x,y] coordinates of matched points of second image. :param num_iters: number of RANSAC iterations to perform. :param inlier_tol: inlier tolerance threshold. :return: A 3x3 normalized homography matrix and An Array with shape (S,) where S is the number of inliers, containing the indices in pos1/pos2 of the maximal set of inlier matches found. """ inliers = np.array([]) for i in range(num_iters): rand_idx = np.random.choice( pos1.shape[0], size=NUM_OF_POINTS_TO_TRANS) # choose 4 points pos1_smpl, pos2_smpl = pos1[rand_idx, :], pos2[rand_idx, :] h = sol4_add.least_squares_homography(pos1_smpl, pos2_smpl) if h is None: continue pos1_trans = apply_homography(pos1, h) e = np.linalg.norm(pos1_trans - pos2, axis=1)**2 curr_inliers = np.where( e < inlier_tol)[0] # indices of "good" points of pos2 if len(curr_inliers) > len(inliers): inliers = curr_inliers H12 = sol4_add.least_squares_homography(pos1[inliers, :], pos2[inliers, :]) return H12, inliers
def ransac_homography(pos1, pos2, num_iters, inlier_tol): from random import sample assert (pos1.shape == pos2.shape) # important! N = pos1.shape[0] maxInliers = np.ndarray((1,)) maxH = None for i in range(num_iters): J = sample(range(N), 4) J1 = np.take(pos1, J, axis=0) J2 = np.take(pos2, J, axis=0) H12 = least_squares_homography(J1, J2) if (H12 is None): num_iters += 1 # ignore this round continue T1 = apply_homography(pos1, H12) dist = np.sqrt(np.abs(T1 - pos2) ** 2) dist[dist < inlier_tol] = True dist[dist >= inlier_tol] = False whr = np.argwhere(dist == True) whr = whr.reshape(-1)[::2] S = np.delete(whr, np.unique(whr, return_index=True)) if (S.shape[0] > maxInliers.shape[0]): maxInliers = S maxH = H12 return maxH, maxInliers
def ransac_homography(pos1,pos2,num_iters,inlier_tol): max_inliers = 0 final_inliers_set = None for i in range(num_iters): choesn_points = np.random.randint(pos1.shape[0], size=4) match_index_im1 , match_index_im2 = pos1[choesn_points] , pos2[choesn_points] cur_H12 = add.least_squares_homography(match_index_im1, match_index_im2) if(cur_H12 != None): transformed_pos1 = apply_homography(np.copy(pos1), cur_H12) e_set = np.linalg.norm((pos2 - transformed_pos1), 2, axis=1) cur_inliers = np.argwhere(e_set < inlier_tol) if cur_inliers.size > max_inliers: max_inliers = cur_inliers.size final_inliers_set = np.copy(cur_inliers) return add.least_squares_homography(pos1[final_inliers_set.flatten()], pos2[final_inliers_set.flatten()]), final_inliers_set.flatten()
def ransac_homography(pos1, pos2, num_iter, inlier_tol): E = np.array([]) pointsArr = [] max_inliers = 0 for i in range(num_iter): points = random_points(pos1.shape[0]) pointsArr.append(points) H = sol4_add.least_squares_homography(pos1[points], pos2[points]) if H is None: continue inliers = apply_and_get_nunber_of_inliers(H, pos1, pos2, inlier_tol) if inliers.size > max_inliers: max_inliers = inliers.size E = inliers H = sol4_add.least_squares_homography(pos1[E], pos2[E]) inliers = apply_and_get_nunber_of_inliers(H, pos1, pos2, inlier_tol) return H, inliers
def ransac_homography(pos1, pos2, num_iters=10000, inlier_tol=6): maxInliers = 0 inliers = np.zeros(pos1.shape[0]) current_inliers = np.zeros(pos1.shape[0]) for j in range(num_iters): idx_sample = np.random.permutation(pos1.shape[0])[:4] H12 = sol4_add.least_squares_homography(pos1[idx_sample], pos2[idx_sample]) if H12 is None: continue P2_tag = apply_homography(pos1, H12) Ej = (P2_tag-pos2) summed_rows = np.abs(Ej[:,0]) + np.abs(Ej[:,1]) current_inliers = np.where(summed_rows < inlier_tol)[0] if np.size(current_inliers) > maxInliers: maxInliers = np.size(current_inliers) inliers = current_inliers H12 = sol4_add.least_squares_homography(pos1[inliers], pos2[inliers]) return H12, inliers
def ransac_homography(pos1, pos2, num_iters, inlier_tol): ############################################################# # applying RANSAC routine on the matches ############################################################# pos1, pos2 = np.array(pos1), np.array(pos2) best_inliers = [] for i in range(num_iters): # extract 4 random point and compute homography idx = np.random.random_integers(0, pos1.shape[0] - 1, size=4) points1, points2 = pos1[idx], pos2[idx] H12 = sad.least_squares_homography(points1, points2) # avoid unstable results if H12 is None: continue to_pos2 = np.array(apply_homography(pos1, H12)) # compute amount of inliers in_indices = np.where( np.array(map(np.sum, (to_pos2 - pos2)**2)) < inlier_tol)[0] best_inliers = in_indices if len(in_indices) > len( best_inliers) else best_inliers # recompute the homography points1, points2 = pos1[best_inliers], pos2[best_inliers] H12 = sad.least_squares_homography(points1, points2) return H12, best_inliers