例如,我在一个坐标系中有4个点,在另一个坐标系中有4个点,如果我以天真的方式估计仿射变换拐角点[点1,4]将不会精确地翘曲到另一个坐标系中的相应拐角点。
如何获得仿射变换并限制某些点应在另一个坐标系中的对应点上弯曲?
答案 0 :(得分:0)
据我了解,当已知4个特定点的图像时,您需要恢复仿射变换。 我认为,以下代码可能会对您有所帮助(对不起,不好的代码风格-我是数学家,而不是程序员)
import numpy as np
# input data
ins = np.array([[1, 1, 2], [2, 3, 0], [3, 2, -2], [-2, 2, 3]]) # <- primary system
out = np.array([[0, 2, 1], [1, 2, 2], [-2, -1, 6], [4, 1, -3]]) # <- secondary system
p = np.array([1, 2, 3]) #<- transform this point
# finding transformation
l = len(ins)
e = lambda r,d: np.linalg.det(np.delete(np.vstack([r, ins.T, np.ones(l)]), d, axis=0))
M = np.array([[(-1)**i * e(R, i) for R in out.T] for i in range(l+1)])
A, t = np.hsplit(M[1:].T/(-M[0])[:,None], [l-1])
t = np.transpose(t)[0]
# output transformation
print("Affine transformation matrix:\n", A)
print("Affine transformation translation vector:\n", t)
# unittests
print("TESTING:")
for p, P in zip(np.array(ins), np.array(out)):
image_p = np.dot(A, p) + t
result = "[OK]" if np.allclose(image_p, P) else "[ERROR]"
print(p, " mapped to: ", image_p, " ; expected: ", P, result)
# calculate points
print("CALCULATION:")
P = np.dot(A, p) + t
print(p, " mapped to: ", P)
此代码演示了如何将仿射变换恢复为矩阵+向量,并测试了初始点已映射到其应有的位置。您可以使用Google colab测试此代码,因此无需安装任何程序。
关于此代码背后的理论:它基于“ Beginner's guide to mapping simplexes affinely”中给出的等式,在“规范表示法的恢复”部分描述了矩阵恢复,并在其中讨论了确定精确仿射变换所需的点数。 “我们需要多少点?”部分。同一作者发表了“ Workbook on mapping simplexes affinely”,其中包含许多此类实际示例。