【问题标题】:How to obtain affine transform with some points fixed? [duplicate]如何获得一些固定点的仿射变换? [复制]
【发布时间】:2018-12-07 18:08:18
【问题描述】:

例如,我在一个坐标系中有 4 个点,在另一个坐标系中有 4 个点,如果我以天真的方式估计仿射变换,角点 [points 1,4] 将不会精确地扭曲到另一个坐标系中的相应角点。

如何获得仿射变换,限制某些点应该在另一个坐标系中的对应点上翘曲?

【问题讨论】:

  • 我推荐你使用 NumPy。例如,如果您在 R2 中,您可以在 NumPy 中拥有一个原生矩阵 [[a,b], [c,d]],它可以在 (x,y) 上执行仿射变换而不会出现任何问题。 docs.scipy.org/doc/numpy/index.html
  • 您能否提供一些有关当前结果和预期结果的示例。

标签: python opencv computer-vision linear-algebra affinetransform


【解决方案1】:

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

【讨论】:

  • 请不要多次发布相同的答案。相反,将问题标记为重复。这个答案与stackoverflow.com/a/56228667/7328782 几乎相同
  • 非常感谢!我是stackowerflow的新手,我实际上不知道如何标记重复项(找不到任何按钮或其他任何东西)。可能这需要声誉高于某个值,因此我无法使用此功能。
  • 你说得对,举报需要15分。你现在应该有足够的代表了。 :)
  • 谢谢 :) 现在我标记了它,我应该删除答案还是什么都不做,只将所有内容留给版主?
猜你喜欢
  • 2014-05-27
  • 2014-05-22
  • 1970-01-01
  • 2020-01-03
  • 2017-05-10
  • 1970-01-01
  • 2020-02-11
  • 1970-01-01
  • 2010-12-17
相关资源
最近更新 更多