E. *_*las 2 numpy image-processing linear-algebra affinetransform
我正在尝试使用Kloss和Kloss在“使用NumPy进行N维线性向量场回归”(2010,Python论文源代码2)中给出的解决方案,给出给定两点的二维仿射变换。
他们提供了一种方法来查找连接两组点y和x的仿射变换, 其中该变换由矩阵A和向量b表示(即矩阵方程y = Ax + b)。
在二维中,您有6个未知数,其中四个定义2x2 A矩阵,另外2个定义b。
但是,在示例脚本和描述它的论文中,它们具有未知数t = n ^ 2 + n,其中n是点的数量,这意味着您需要六个点,对于2D情况,它实际上是12个已知值(即x和图像上每个点的y值)。
他们通过以下方式对此进行了测试:
def solve(point_list):
"""
This function solves the linear equation system involved in the n
dimensional linear extrapolation of a vector field to an arbitrary point.
f(x) = x * A + b
with:
A - The "slope" of the affine function in an n x n matrix.
b - The "offset" value for the n dimensional zero vector.
The function takes a list of n+1 point-value tuples (x, f(x)) and returns
the matrix A and the vector b. In case anything goes wrong, the function
returns the tuple (None, None).
These can then be used to compute directly any value in the linear
vector field.
"""dimensions = len(point_list[0][0])
unknowns = dimensions ** 2 + dimensions
number_points = len(point_list[0])
# Bail out if we do not have enough data.
if number_points < unknowns:
print ’For a %d dimensional problem I need at least %d data points.’ \ % (dimensions, unknowns)
print ’Only %d data points were given.’ % number_points return None, None.
Run Code Online (Sandbox Code Playgroud)
...
问题:
他们为什么说您需要6点才能获得2D仿射变换?
opencv getAffineTransform只需要3个数据点即可在2D中找到一个点,这是直观的数字,因为3个点定义了一个平面。而且,当我从Kloss和Kloss代码中取出上述条件测试时,它在2D模式下可得到3分。
他们为什么说您需要6点才能获得2D仿射变换?
对于此类转换,使用引入第三个坐标的同构坐标很方便w,即:
(x, y)变(x, y, w),x'/w' = x/w和等于两点y'/w'= y/w。因此,您通常可以使用w = 1。
使用此系统,您可以使用矩阵乘法来表示2D转换(平移,旋转等):
[x'] [x]
[y'] = A . [y]
[1 ] [1]
Run Code Online (Sandbox Code Playgroud)
仿射变换是平移,缩放和旋转变换的组合,可以表示为:
[1 0 tx] [Sx 0 0] [cos(a) -sin(a) 0] [a b c]
A = [0 1 ty] . [0 Sy 0] . [sin(a) cos(a) 0] = [d e f]
[0 0 1 ] [0 0 1] [0 0 1] [0 0 1]
Run Code Online (Sandbox Code Playgroud)
因此,您有6个参数(又称未知数),因此您需要3对点来求解系统,因为每对点都会给您2个方程式。
换句话说,您需要(至少)6个点(= 3对)来计算转换。
注意:至少需要6个点,如果您获得的更多,则系统是超定的,这意味着您可以找到一个近似的解决方案,例如,用最小二乘作为点。