【Python】Coding the Matrix:Week 5 Perspective Lab

这个Lab的内容光是说明就有7页之巨,我反复看了很久才看懂一点点,Lab主要完成的是从不同坐标系表示之间变换的方法。

【Python】Coding the Matrix:Week 5 Perspective Lab_第1张图片

原始的图片,从Camera basis的表示转换成WhiteBoard basis的表示

【Python】Coding the Matrix:Week 5 Perspective Lab_第2张图片

里面的Problem 3是难点,Problem 4我没有完成,因为还缺少之前的代码,暂时不写。

注意Problem 3中的vector h不能通过print(h)来获得,因为print会对浮点数进行四舍五入,导致答案错误。

#from image_mat_util import *

from mat import Mat
from vec import Vec

from solver import solve
from matutil import *
## 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'}, {k:v[k]/v['y3'] for k in v.D})

## 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'):-1*x1,('y1','x2'):-x2,('y1','x3'):-1})
    v = Vec(domain, {('y3','x1'):w2*x1,('y3','x2'):w2*x2,('y3','x3'):w2,('y2','x1'):-1*x1,('y2','x2'):-x2,('y2','x3'):-1})
    return [u, v]


## Task 3
x11=358
x21=36
w11=0
w21=0
x12=329
x22=597
w12=0
w22=1
x13=592
x23=157
w13=1
w23=0
x14=580
x24=483
w14=1
w24=1
[u1,v1]=make_equations(x11, x21, w11, w21)
[u2,v2]=make_equations(x12, x22, w12, w22)
[u3,v3]=make_equations(x13, x23, w13, w23)
[u4,v4]=make_equations(x14, x24, w14, w24)
w=Vec(u1.D,{('y1','x1'):1})
b=Vec(range(9),{8:1})
L=rowdict2mat([u1,v1,u2,v2,u3,v3,u4,v4,w])
h=solve(L,b)
H = Mat(({'y2', 'y3', 'y1'}, {'x1', 'x3', 'x2'}),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).
    '''
    pass


你可能感兴趣的:(【Python】Coding the Matrix:Week 5 Perspective Lab)