据我了解,当已知 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”,其中包含许多此类实际示例。