-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathperspective_lab.py
57 lines (49 loc) · 2.03 KB
/
perspective_lab.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
from image_mat_util import *
from mat import Mat
from vec import Vec
import matutil as mu
from solver import solve
## Task 1
def move2board(v):
'''
Input:
- v: a vector with domain {'y1','y2','y3'}, the coordinate representation of a point q.
Output:
- A {'y1','y2','y3'}-vector z, the coordinate representation
in whiteboard coordinates of the point p such that the line through the
origin and q intersects the whiteboard plane at p.
'''
return Vec({'y1','y2','y3'}, {'y1':v['y1']/v['y3'], 'y2':v['y2']/v['y3'], 'y3':v['y3']/v['y3']})
## Task 2
def make_equations(x1, x2, w1, w2):
'''
Input:
- x1 & x2: photo coordinates of a point on the board
- y1 & y2: whiteboard coordinates of a point on the board
Output:
- List [u,v] where u*h = 0 and v*h = 0
'''
domain = {(a, b) for a in {'y1', 'y2', 'y3'} for b in {'x1', 'x2', 'x3'}}
u = Vec(domain, {('y3','x1'):w1*x1, ('y3','x2'):w1*x2, ('y3','x3'):w1, ('y1','x1'):-x1, ('y1','x2'):-x2, ('y1','x3'):-1})
v = Vec(domain, {('y3','x1'):w2*x1, ('y3','x2'):w2*x2, ('y3','x3'):w2, ('y2','x1'):-x1, ('y2','x2'):-x2, ('y2','x3'):-1})
return [u, v]
## Task 3
L = make_equations(358,36,0,0)+make_equations(329,597,0,1)+make_equations(592,157,1,0)+make_equations(580,483,1,1)
domain = {(a, b) for a in {'y1', 'y2', 'y3'} for b in {'x1', 'x2', 'x3'}}
L.append(Vec(domain, {('y1','x1'):1}))
b = Vec({0,1,2,3,4,5,6,7,8}, {8:1})
mL = mu.rowdict2mat(L)
h = solve(mL,b)
H = Mat(({'y1','y2','y3'},{'x1','x2','x3'}), h.f)
## Task 4
def mat_move2board(Y):
'''
Input:
- Y: Mat instance, each column of which is a 'y1', 'y2', 'y3' vector
giving the whiteboard coordinates of a point q.
Output:
- Mat instance, each column of which is the corresponding point in the
whiteboard plane (the point of intersection with the whiteboard plane
of the line through the origin and q).
'''
return mu.coldict2mat({key:move2board(val) for (key, val) in mu.mat2coldict(Y).items()})