def affine_matrix(self, kp_s, kp_t, fit_pos): ''' AFFINE_MATRIX Compute affine transformation matrix by corresponding points. Input arguments: - kp_s : key points from source image - kp_t : key points from target image - fit_pos : index of corresponding points Output: - M : the affine transformation matrix whose dimension is 2 by 3 ''' # Extract corresponding points from all key points kp_s = kp_s[:, fit_pos[:, 0]] kp_t = kp_t[:, fit_pos[:, 1]] # Apply RANSAC to find most inliers _, _, inliers = Ransac(self.K, self.threshold).ransac_fit(kp_s, kp_t) # Extract all inliers from all key points kp_s = kp_s[:, inliers[0]] kp_t = kp_t[:, inliers[0]] # Use all inliers to estimate transform matrix A, t = Affine().estimate_affine(kp_s, kp_t) M = np.hstack((A, t)) return M
def affine_matrix(kp_master, kp_slave, fit_pos): # Extract corresponding points from all key points kp_master = kp_master[:, fit_pos[:, 0]] kp_slave = kp_slave[:, fit_pos[:, 1]] # Apply RANSAC to find most inliers _, _, inliers = Ransac(3, 1).ransac_fit(kp_master, kp_slave) # Extract all inliers from all key points kp_master = kp_master[:, inliers[0]] kp_slave = kp_slave[:, inliers[0]] # Use all inliers to estimate transform matrix A, t = Affine().estimate_affine(kp_master, kp_slave) M = np.hstack((A, t)) return M
def match_with_mask(img_path, valid_pos, mask_p, mask_d, result_path): img = cv2.imread(img_path) print('gen label feature') img_p, img_d = gen_label_feature(img) temp1 = [[], []] temp2 = [] print('original img pos count is ', len(img_p[0])) for i, x in enumerate(img_p[0]): if i % 100 == 0: print(i) pos = [int(img_p[0][i]), int(img_p[1][i])] if pos in valid_pos: temp1[0].append(img_p[0][i]) temp1[1].append(img_p[1][i]) temp2.append(img_d[i]) img_p = np.array(temp1) img_d = np.array(temp2) print('valie img pos count is ', len(img_p[0])) fit_pos = match_SIFT(img_d, mask_d) print('fit count is ', len(fit_pos)) img_p = img_p[:, fit_pos[:, 0]] mask_p = mask_p[:, fit_pos[:, 1]] start = time.time() _, _, inliers = Ransac(3, 1).ransac_fit(img_p, mask_p) print('inliners count is ', len(inliers[0])) img_p = img_p[:, inliers[0]] mask_p = mask_p[:, inliers[0]] A, t = Affine().estimate_affine(img_p, mask_p) M = np.hstack((A, t)) end = time.time() rows, cols, _ = img.shape warp = cv2.warpAffine(img, M, (cols, rows)) print(result_path + img_path[9:]) cv2.imwrite(result_path + img_path[9:], warp)
# Randomly select 3 pairs of points to do estimation idx = np.random.randint(0, pts_s.shape[1], (K, 1)) A_test, t_test = af.estimate_affine(pts_s[:, idx], pts_t[:, idx]) # Display known parameters with estimations # They should be same when outlier_rate equals to 0, # otherwise, they are totally different in some cases print(A_true, '\n', t_true) print(A_test, '\n', t_test) # ------------------------------------------------------------- # Test Class Ransac # ------------------------------------------------------------- # Create instance rs = Ransac(K=3, threshold=1) residual = rs.residual_lengths(A_test, t_test, pts_s, pts_t) # Run RANSAC to estimate affine tansformation when # too many outliers in points set A_rsc, t_rsc, inliers = rs.ransac_fit(pts_s, pts_t) print(A_rsc, '\n', t_rsc) # ------------------------------------------------------------- # Test Class Align # ------------------------------------------------------------- # Load source image and target image source_path = 'Images/mona_source.png' target_path = 'Images/mona_target.jpg'
def imageRegistration(source_path, target_path): # Affine Transform # |x'| = |a, b| * |x| + |tx| # |y'| |c, d| |y| |ty| # pts_t = A * pts_s + t # ------------------------------------------------------------- # Test Class Affine # ------------------------------------------------------------- # Create instance af = Affine() # Generate a test case as validation with # a rate of outliers outlier_rate = 0.9 A_true, t_true, pts_s, pts_t = af.create_test_case(outlier_rate) # At least 3 corresponding points to # estimate affine transformation K = 3 # Randomly select 3 pairs of points to do estimation idx = np.random.randint(0, pts_s.shape[1], (K, 1)) A_test, t_test = af.estimate_affine(pts_s[:, idx], pts_t[:, idx]) # Display known parameters with estimations # They should be same when outlier_rate equals to 0, # otherwise, they are totally different in some cases print(A_true, '\n', t_true) print(A_test, '\n', t_test) # ------------------------------------------------------------- # Test Class Ransac # ------------------------------------------------------------- # Create instance rs = Ransac(K=3, threshold=1) residual = rs.residual_lengths(A_test, t_test, pts_s, pts_t) # Run RANSAC to estimate affine tansformation when # too many outliers in points set A_rsc, t_rsc, inliers = rs.ransac_fit(pts_s, pts_t) print(A_rsc, '\n', t_rsc) # ------------------------------------------------------------- # Test Class Align # ------------------------------------------------------------- # parser = argparse.ArgumentParser(description = "Image file name") # parser.add_argument('--source',type=str,default='',help="Image source source_path") #takes in template # parser.add_argument('--input',type=str,default='',help="Image input path") #takes in filled in form # args=parser.parse_args() # # Load source image and target image # source_path = args.input # target_path = args.source # Create instance al = Align(source_path, target_path, threshold=1) # Image transformation al.align_image()