-
Notifications
You must be signed in to change notification settings - Fork 0
/
calcTransformationMatrix.py
58 lines (42 loc) · 1.76 KB
/
calcTransformationMatrix.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import numpy as np
def calcTransformationMatrix(model, input):
# Zoek 2D affine transformatie matrix om scaling, rotatatie en translatie te beschrijven tussen model en input
# 2x2 matrix werkt niet voor translaties
# Pad the data with ones, so that our transformation can do translations too
n = model.shape[0]
pad = lambda x: np.hstack([x, np.ones((x.shape[0], 1))]) # horizontaal stacken
unpad = lambda x: x[:, :-1]
# padden:
# naar vorm [ x x 0 1]
X = pad(model)
Y = pad(input)
# print(X)
# print(Y)
# Solve the least squares problem X * A = Y
# to find our transformation matrix A
A, res, rank, s = np.linalg.lstsq(X, Y)
transform = lambda x: unpad(np.dot(pad(x), A))
modelTransform = transform(model)
A[np.abs(A) < 1e-10] = 0 # set really small values to zero
return (modelTransform, A)
def calcTransformationMatrix_fixed_points(model, input, secondary):
# Zoek 2D affine transformatie matrix om scaling, rotatatie en translatie te beschrijven tussen model en input
# 2x2 matrix werkt niet voor translaties
# Pad the data with ones, so that our transformation can do translations too
n = model.shape[0]
pad = lambda x: np.hstack([x, np.ones((x.shape[0], 1))]) # horizontaal stacken
unpad = lambda x: x[:, :-1]
# padden:
# naar vorm [ x x 0 1]
X = pad(model)
Y = pad(input)
# print(X)
# print(Y)
# Solve the least squares problem X * A = Y
# to find our transformation matrix A
A, res, rank, s = np.linalg.lstsq(X, Y)
transform = lambda x: unpad(np.dot(pad(x), A))
#modelTransform = transform(model)
modelTransform = transform(secondary)
A[np.abs(A) < 1e-10] = 0 # set really small values to zero
return (modelTransform, A)