PIL透视变换,求出(a,b,c,d,e,f,g,h)

2024-06-27 09:19:25 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图用PIL对图像进行透视变换,我有图像的角点坐标和图像角点的结束位置的坐标。我不知道如何获得'data'参数的(a,b,c,d,e,f,g,h)。在

我知道这和这有关: http://bishopw.loni.ucla.edu/AIR5/2Dperspective.html 但我不知道这页是什么意思。在


Tags: 图像httpdata参数pilhtmledu试图用
1条回答
网友
1楼 · 发布于 2024-06-27 09:19:25

您可以通过求解方程得到参数:T.x1+v=x2,其中x1是坐标系1(原始图片)中的点坐标,x2是新坐标系(倾斜、旋转或三维)。x1,x2,v是2乘1向量,T是2乘2矩阵。例如x1=(x1x,x1y),x2=(x2x,x2y),v=(c,f)和

T = a b
    d e

如果你不懂矩阵代数,你可以通过消除变量来解决这个问题。对于每一点,可以得到两个等式:

^{pr2}$

如果你现在插入一个角点。假设x1=(0,1)和x2=(0,4),得到:

a*0   + b*1   + c = 0
d*0   + e*1   + f = 4

从中你可以得到:

b = -c
e = 4-f

现在,如果你对其他角点重复这个步骤(并使用b=-c的知识)。您可以求解所有变量的数值。在

提示,在计算转换之前,请将原始图片坐标缩放到单位平方(0,0),(0,1),(1,0)和(1,1)。这样你就有很多个1和0。数学方法称为高斯消去法(使用谷歌或维基百科->高斯消去->算法示例)。在

请注意im.tranform公司有六个参数(二维->二维变换):

Data is a 6-tuple (a, b, c, d, e, f) which contain the first two rows from an affine transform matrix. For each pixel (x, y) in the output image, the new value is taken from a position (a x + b y + c, d x + e y + f) in the input image, rounded to nearest pixel.

编辑:Ups,以上是仿射变换。你问的是视角转换。函数相同,但参数不同。数据应该是:

Data is a 8-tuple (a, b, c, d, e, f, g, h) which contains the coefficients for a perspective transform. For each pixel (x, y) in the output image, the new value is taken from a position (a x + b y + c)/(g x + h y + 1), (d x + e y + f)/(g x + h y + 1) in the input image, rounded to nearest pixel.

所以方程是Q.x3=x4,其中原始坐标x3是(x3x,x3y,1),变换后的坐标x4是(x4x,x4y,1),对于Q:

Q = a b c
    d e f
    g h 1

与仿射方法相比,我们将常数v嵌入到矩阵中。现在你的方程式变成:

a*x3x + b*x3y + c*1 = x4x    
d*x3x + e*x3y + f*1 = x4y   
g*x3x + h*x3y + 1*1 = 1

用高斯消去法作为仿射变换求解。在

相关问题 更多 >