Skip to content

Instantly share code, notes, and snippets.

@hq-jiang
Created April 4, 2022 20:11
Show Gist options
  • Save hq-jiang/dbd60777a9956b411fbd7458fdb89cf7 to your computer and use it in GitHub Desktop.
Save hq-jiang/dbd60777a9956b411fbd7458fdb89cf7 to your computer and use it in GitHub Desktop.
def find_homography(points_source, points_target):
A = construct_A(points_source, points_target)
u, s, vh = np.linalg.svd(A, full_matrices=True)
# Solution to H is the last column of V, or last row of V transpose
homography = vh[-1].reshape((3,3))
return homography/homography[2,2]
def construct_A(points_source, points_target):
assert points_source.shape == points_target.shape, "Shape does not match"
num_points = points_source.shape[0]
matrices = []
for i in range(num_points):
partial_A = construct_A_partial(points_source[i], points_target[i])
matrices.append(partial_A)
return np.concatenate(matrices, axis=0)
def construct_A_partial(point_source, point_target):
x, y, z = point_source[0], point_source[1], 1
x_t, y_t, z_t = point_target[0], point_target[1], 1
A_partial = np.array([
[0, 0, 0, -z_t*x, -z_t*y, -z_t*z, y_t*x, y_t*y, y_t*z],
[z_t*x, z_t*y, z_t*z, 0, 0, 0, -x_t*x, -x_t*y, -x_t*z]
])
return A_partial
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment