6 # call with 2x4 vector (4 points of x, y)
8 # see https://math.stackexchange.com/questions/296794/finding-the-transform-matrix-from-4-projected-points-with-javascript
9 A = numpy.ones((3, 4), numpy.double)
11 return A[:, :3] * numpy.linalg.solve(A[:, :3], A[:, 3])[numpy.newaxis, :]
13 def calc_transform(p, q):
23 #print ('xxx', y[:2, :] / y[2:, :], p)
34 #print ('yyy', y[:2, :] / y[2:, :], q)
36 #return B @ numpy.linalg.inv(A)
37 return numpy.linalg.solve(A.transpose(), B.transpose()).transpose()
39 # call with 2xN vector (N points of x, y), computes 2xN vector
40 def apply_transform_multi(A, p):
41 x = numpy.ones((3, p.shape[1]), numpy.double)
44 return y[:2, :] / y[2:, :]
46 # call with 2-vector (x, y), computes 2-vector
47 def apply_transform(A, p):
48 return apply_transform_multi(A, p[:, numpy.newaxis])[:, 0]
50 if __name__ == '__main__':
51 x = numpy.array([[11., 52., 23., 74.], [15., 36., 27., 58.]], numpy.double)
52 y = numpy.array([[31., 92., 73., 24.], [65., 26., 87., 18.]], numpy.double)
53 A = calc_transform(x, y)
54 print(apply_transform_multi(A, x), y)