现在我有 N 张图像,选取第 1 张图像作为 reference image,剩下的图像都要跟第 1 张图像做 feature matching。一共做 N-1 次 feature matching。每次 feature matching 之后,都能得到两个输出,分别是 参与 匹配的两张图像对应的匹配点kp0_numpy, kp1_numpy。
N-1 次 feature matching,就会得到两个 list,input1 跟 input2,分别对应N-1个kp0_numpy跟N-1个kp1_numpy。
现在我需要找到 N-1 个 kp0_numpy 共有的 keypoints,以及这些keypoints 在 每个 kp1_numpy 对应的 keypoints。 下面的 FindCommonUV
函数可以做到。
CombineFeaturePointArrays
函数则是实现将 N-1 个 kp0_numpy 共有的 keypoints,以及这些keypoints 在 每个 kp1_numpy 对应的 keypoints 拼凑成 一个 [2*N, num_feature ] 形状的 np.darray。其中 num_feature 是 公共 keypoints的数目,N 是 图像数目。
函数 FindCommonUV
的输入 input1 跟 input2 是两个 keypoints 的 list。 input1 对应 reference image
python
def CombineFeaturePointArrays(common_points, matched_points_list):
# 将common_points和matched_points_list中的数组转置
transposed_common_points = common_points.T
transposed_matched_points = [feature_array.T for feature_array in matched_points_list]
result_array = np.vstack([transposed_common_points, *transposed_matched_points])
return result_array
def FindCommonUV(input1, input2):
common_points = None
matched_points_list = []
# Find common points in the first pair of arrays
common_points = np.array([point for point in input1[0] if all( np.any(np.all(point == sublist, axis=1)) for sublist in input1[1:] )])
# Iterate over input1 and input2 simultaneously
for i, (kp0, kp1) in enumerate(zip(input1, input2)):
# Find indices of common points in input1[i]
indices = [np.where((kp0 == point).all(axis=1))[0][0] for point in common_points]
# Extract corresponding points from input2[i]
matched_points = kp1[indices]
# Append to matched_points_list
matched_points_list.append(matched_points)
return common_points, matched_points_list
def FilterKeypoints(kp0_numpy, kp1_numpy, H_original, W_original):
# 找到超出范围的索引
invalid_indices_kp0 = np.where((kp0_numpy[:, 0] < 0.) | (kp0_numpy[:, 0] >= W_original) |
(kp0_numpy[:, 1] < 0.) | (kp0_numpy[:, 1] >= H_original))[0]
#print(invalid_indices_kp0)
invalid_indices_kp1 = np.where((kp1_numpy[:, 0] < 0.) | (kp1_numpy[:, 0] >= W_original) |
(kp1_numpy[:, 1] < 0.) | (kp1_numpy[:, 1] >= H_original))[0]
#print(invalid_indices_kp1)
# 合并两个数组,去重
invalid_indices = np.unique(np.concatenate((invalid_indices_kp0, invalid_indices_kp1)))
# 删除超出范围的点
kp0_filtered = np.delete(kp0_numpy, invalid_indices, axis=0)
kp1_filtered = np.delete(kp1_numpy, invalid_indices, axis=0)
return kp0_filtered, kp1_filtered