gpt4 book ai didi

python - NumPy 的 : Finding the tranformation matrix given 2 sets of 4 points in 3D euclidian coordinates

转载 作者:太空宇宙 更新时间:2023-11-04 01:51:12 24 4
gpt4 key购买 nike

我正在尝试求解变换矩阵 M(以其数值形式)。源点已知(s1、s2、s3、s4),目标点也已知(d1、d2、d3、d4)。来自金字塔的点。该变换是任意的,并将每个点 sn 映射到其目标 dn。

显然 s1 到 s4 和 d1 到 d4 是已知的并且使用数值,为了清楚起见,这样呈现它们

s1  = np.matrix([[s1x],[s1y],[s1z],[0]])
s2 = np.matrix([[s2x],[s2y],[s2z],[0]])
s3 = np.matrix([[s3x],[s3y],[s3z],[0]])
s4 = np.matrix([[s4x],[s4y],[s4z],[0]])

d1 = np.matrix([[d1x],[d1y],[d1z],[0]])
d2 = np.matrix([[d2x],[d2y],[d2z],[0]])
d3 = np.matrix([[d3x],[d3y],[d3z],[0]])
d4 = np.matrix([[d4x],[d4y],[d4z],[0]])

对于每个点,dn = M x sn与:

M = np.matrix([4,4])

一般公式好像在这里:https://en.wikipedia.org/wiki/Transformation_matrix#Finding_the_matrix_of_a_transformation

我基本上是想把它放在代码形式中,如果可能的话降低复杂性以便使用 numpy linalg 求解器。

我检查了其他线程,我的问题实际上是关于一般情况的。 (最少需要4分)

最佳答案

也许我遗漏了什么,但这可以分两行完成:

# set up

# create example affine trafo in homogeneous coordinates
M = np.r_[np.random.normal(size=(3,4)),[[0,0,0,1]]]
n = 4 # or more
# create n points as the columns of s
# note that homogeneous coordinates have a "dummy" 1, not 0, as last element
s = np.r_[np.random.normal(size=(3,n)),np.ones((1,n))]
# apply trafo, transformed points are the columns of d
d = M@s

# solve

# solving is as simple as
M_rec,resid,rank,sing = np.linalg.lstsq(s.T,d.T)
M_rec = M_rec.T

# you may want to inspect resid (should be small),
# rank (should be 4) and sing (shouldn't spread too wide)

# check

np.allclose(M,M_rec)
# True

关于python - NumPy 的 : Finding the tranformation matrix given 2 sets of 4 points in 3D euclidian coordinates,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57991865/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com