做双线性转换的代码不起作用

时间:2015-08-06 05:48:40

标签: python linear-algebra

我想尝试将矩形的双线性变换转换为此处描述的四边形 http://www.fmwconcepts.com/imagemagick/bilinearwarp/FourCornerImageWarp2.pdf 在第4页。

我有广场

(500,900)(599,900)(599,999)(500,999)

和四元组

(454,945)(558,951)(598,999)(499,999)

上面四边形和方形坐标的第i个条目是相应的角。

有了这个,我可以制作以下矩阵

[1 500 900 450000][a0] [454] [1 599 900 539100][a1]=[558] [1 599 999 598401][a2] [598] [1 500 999 499500][a3] [499]

[1 500 900 450000][b0] [945] [1 599 900 539100][b1]=[951] [1 599 999 598401][b2] [999] [1 500 999 499500][b3] [999]

如果我解决了他们,我会

a0=-709.911845730028 a1=1.50964187327824 a2=0.709621467197225 a3=-0.000510152025303541 b0=148.305785123967 b1=0.611570247933884 b2=0.85154576063667 b3=-0.000612182430364249

但是当我运行这个python脚本时(我想手动模拟,将四边形上的点(454,945)转换为方形上的点(500,900)),我得到了不同的答案。我得到(442.90822654,1024.0)......

X = 454
Y = 945

a0=-709.911845730028
a1=1.50964187327824
a2=0.709621467197225
a3=-0.000510152025303541


b0=148.305785123967 
b1=0.611570247933884 
b2=0.85154576063667 
b3=-0.000612182430364249

A = b2*a3 - b3*a2
C_one = (b0*a1 - b1*a0)
C = C_one + (b1*X - a1*Y)
B_one = (b0*a3 - b3*a0) + (b2*a1 - b1*a2)
B = B_one + (b3*X - a3*Y)

V = (-B + (B*B - 4*A*C)**0.5 ) / (2*A)
U = (X - a0 - a2*V) / (a1 + a3*V)

print U,V

有谁知道什么是错的?

1 个答案:

答案 0 :(得分:0)

如果我理解你的线性代数符号,那么我不同意你的ab

sage: A = Matrix([[1,500,900,450000],[1, 599, 900, 539100],
                  [1, 599, 999 ,5984],[1, 500, 999, 499500]])
sage: v = vector([945, 951, 999, 999])
sage: w = vector([454, 558, 598, 499])
sage: a = A.solve_right(w)
sage: b = A.solve_right(v)
sage: print(a); print(N(a))
(-3435371408/7209873, 15036641/14419746, 721395/1602194, 5/582616)
(-476.481542462676, 1.04278126674353, 0.450254463566834, 8.58198195724113e-6)
sage: print(b); print(N(b))
(1029623095/2403291, 123379/2403291, 432837/801097, 3/291308)
(428.422149044789, 0.0513375200922402, 0.540305356280201, 0.0000102983783486894)
sage: 
sage: A * a
(454, 558, 598, 499)
sage: A * b
(945, 951, 999, 999)

计算

from math import sqrt

X = 454
Y = 945

a = (-476.481542462676, 1.04278126674353,
     0.450254463566834, 8.58198195724113e-6)

b = (428.422149044789, 0.0513375200922402,
     0.540305356280201, 0.0000102983783486894)

A = b[2]*a[3] - b[3]*a[2]
C_one = (b[0]*a[1] - b[1]*a[0])
C = C_one + (b[1]*X - a[1]*Y)
B_one = (b[0]*a[3] - b[3]*a[0]) + (b[2]*a[1] - b[1]*a[2])
B = B_one + (b[3]*X - a[3]*Y)
V = (-B + sqrt((pow(B,2) - 4*A*C))) / (2*A)
U = (X - a[0] - a[2]*V) / (a[1] + a[3]*V)

print( U,V )

我得到了

892.3074973991894 -0.0

那是不同的......不知道它是否正确。