8 # call with 2x4 vector (4 points of x, y)
10 # see https://math.stackexchange.com/questions/296794/finding-the-transform-matrix-from-4-projected-points-with-javascript
11 A = numpy.ones((3, 4), numpy.double)
13 return A[:, :3] * numpy.linalg.solve(A[:, :3], A[:, 3])[numpy.newaxis, :]
15 def calc_transform(p, q):
25 #print ('xxx', y[:2, :] / y[2:, :], p)
36 #print ('yyy', y[:2, :] / y[2:, :], q)
38 #return B @ numpy.linalg.inv(A)
39 return numpy.linalg.solve(A.transpose(), B.transpose()).transpose()
41 # call with 2xN vector (N points of x, y), computes 2xN vector
42 def apply_transform_multi(A, p):
43 x = numpy.ones((3, p.shape[1]), numpy.double)
46 return y[:2, :] / y[2:, :]
48 # call with 2-vector (x, y), computes 2-vector
49 def apply_transform(A, p):
50 return apply_transform_multi(A, p[:, numpy.newaxis])[:, 0]
52 def remap_image(A, image):
53 ys, xs, cs = image.shape
54 coords = numpy.zeros((2, ys, xs), numpy.double)
55 coords[0, :, :] = numpy.arange(ys, dtype = numpy.double)[:, numpy.newaxis]
56 coords[1, :, :] = numpy.arange(xs, dtype = numpy.double)[numpy.newaxis, :]
57 mapped_coords = apply_transform_multi(
59 coords[::-1, :, :].reshape((2, ys * xs))
60 ).reshape(2, ys, xs)[::-1, :, :]
63 scipy.ndimage.map_coordinates(image[:, :, j], mapped_coords)
69 if __name__ == '__main__':
70 x = numpy.array([[11., 52., 23., 74.], [15., 36., 27., 58.]], numpy.double)
71 y = numpy.array([[31., 92., 73., 24.], [65., 26., 87., 18.]], numpy.double)
72 A = calc_transform(x, y)
73 print(apply_transform_multi(A, x), y)