我正在尝试求解变换矩阵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