Mercurial > hg > orthanc-stone
comparison OrthancStone/Resources/Computations/ComputeWarp.py @ 1512:244ad1e4e76a
reorganization of folders
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Tue, 07 Jul 2020 16:21:02 +0200 |
parents | Resources/Computations/ComputeWarp.py@4abddd083374 |
children | 8c5f9864545f |
comparison
equal
deleted
inserted
replaced
1511:9dfeee74c1e6 | 1512:244ad1e4e76a |
---|---|
1 #!/usr/bin/python | |
2 | |
3 from sympy import * | |
4 from sympy.solvers import solve | |
5 import pprint | |
6 import sys | |
7 | |
8 init_printing(use_unicode=True) | |
9 | |
10 | |
11 # Create a test 3D vector using homogeneous coordinates | |
12 x, y, z, w = symbols('x y z w') | |
13 p = Matrix([ x, y, z, w ]) | |
14 | |
15 | |
16 # Create a shear matrix, and a scale/shift "T * S" transform as in | |
17 # Lacroute's thesis (Equation A.16, page 209) | |
18 ex, ey, ew = symbols('ex ey ew') | |
19 sx, sy, tx, ty = symbols('sx sy tx ty') | |
20 | |
21 TS = Matrix([[ sx, 0, 0, tx ], | |
22 [ 0, sy, 0, ty ], | |
23 [ 0, 0, 1, 0 ], | |
24 [ 0, 0, 0, 1 ]]) | |
25 | |
26 pureShear = Matrix([[ 1, 0, ex, 0 ], | |
27 [ 0, 1, ey, 0 ], | |
28 [ 0, 0, 1, 0 ], | |
29 [ 0, 0, ew, 1 ]]) | |
30 | |
31 | |
32 # Create a general warp matrix, that corresponds to "M_warp" in | |
33 # Equation (A.17) of Lacroute's thesis: | |
34 ww11, ww12, ww13, ww14, ww21, ww22, ww23, ww24, ww31, ww32, ww33, ww34, ww41, ww42, ww43, ww44 = symbols('ww11 ww12 ww13 ww14 ww21 ww22 ww23 ww24 ww31 ww32 ww33 ww34 ww41 ww42 ww43 ww44') | |
35 | |
36 WW = Matrix([[ ww11, ww12, ww13, ww14 ], | |
37 [ ww21, ww22, ww23, ww24 ], | |
38 [ ww31, ww32, ww33, ww34 ], | |
39 [ ww41, ww43, ww43, ww44 ]]) | |
40 | |
41 | |
42 # Create the matrix of intrinsic parameters of the camera | |
43 k11, k22, k14, k24 = symbols('k11 k22 k14 k24') | |
44 K = Matrix([[ k11, 0, 0, k14 ], | |
45 [ 0, k22, 0, k24 ], | |
46 [ 0, 0, 0, 1 ]]) | |
47 | |
48 | |
49 # The full decomposition is: | |
50 M_shear = TS * pureShear | |
51 M_warp = K * WW * TS.inv() | |
52 AA = M_warp * M_shear | |
53 | |
54 # Check that the central component "M_warp == K * WW * TS.inv()" that | |
55 # is the left part of "A" is another general warp matrix (i.e. no | |
56 # exception is thrown about incompatible matrix sizes): | |
57 M_warp * p | |
58 | |
59 if (M_warp.cols != 4 or | |
60 M_warp.rows != 3): | |
61 raise Exception('Invalid matrix size') | |
62 | |
63 | |
64 # We've just shown that "M_warp" is a general 3x4 matrix. Let's call | |
65 # it W: | |
66 w11, w12, w13, w14, w21, w22, w23, w24, w41, w42, w43, w44 = symbols('w11 w12 w13 w14 w21 w22 w23 w24 w41 w42 w43 w44') | |
67 | |
68 W = Matrix([[ w11, w12, w13, w14 ], | |
69 [ w21, w22, w23, w24 ], | |
70 [ w41, w43, w43, w44 ]]) | |
71 | |
72 # This shows that it is sufficient to study a decomposition of the | |
73 # following form: | |
74 A = W * M_shear | |
75 print('\nA = W * M_shear =') | |
76 pprint.pprint(A) | |
77 | |
78 sys.stdout.write('\nW = ') | |
79 pprint.pprint(W) | |
80 | |
81 sys.stdout.write('\nM_shear = ') | |
82 pprint.pprint(M_shear) | |
83 | |
84 | |
85 | |
86 # Let's consider one fixed 2D point (i,j) in the intermediate | |
87 # image. The 3D points (x,y,z,1) that are mapped to (i,j) must satisfy | |
88 # the equation "(i,j) == M_shear * (x,y,z,w)". As "M_shear" is | |
89 # invertible, we solve "(x,y,z,w) == inv(M_shear) * (i,j,k,1)". | |
90 | |
91 i, j, k = symbols('i j k') | |
92 l = M_shear.inv() * Matrix([ i, j, k, 1 ]) | |
93 | |
94 print('\nLocus for points imaged to some fixed (i,j,k,l) point in the intermediate image:') | |
95 print('x = %s' % l[0]) | |
96 print('y = %s' % l[1]) | |
97 print('z = %s' % l[2]) | |
98 print('w = %s' % l[3]) | |
99 | |
100 | |
101 # By inspecting the 4 equations above, we see that the locus entirely | |
102 # depends upon the "k" value that encodes the Z-axis | |
103 | |
104 print('\nGlobal effect of the shear-warp transform on this locus:') | |
105 q = expand(A * l) | |
106 pprint.pprint(q) | |
107 | |
108 print("\nWe can arbitrarily fix the value of 'k', so let's choose 'k=0':") | |
109 pprint.pprint(q.subs(k, 0)) | |
110 | |
111 print("\nThis gives the warp transform.") | |
112 print("QED: line after Equation (A.17) on page 209.\n") |