四边形到矩形变换

时间:2011-06-08 08:22:30

标签: matlab transformation

我想将四边形图像转换为矩形图像,我知道这些顶点。例如,在下面的图像中,我知道坐标(X1,Y1)〜(X4,Y4)和(x1,y1)〜(x2,y2),我想将其转换为矩形。如何在矩形图像中获得(x,y)坐标,该坐标对应于四边形图像中的(X,Y)坐标?

 ____> Y             ____> y            
|                   |                               
|                   |    
V                   V
X                   x               

(X1,Y1)   (X2,Y2)        (x1,y1)    (x1,y2)
    ________                 _________
   / .(X,Y) \   =>          |  .(x,y) |
  /__________\              |_________|
(X3,Y3)    (X4,Y4)       (x2,y1)    (x2,y2) 

1 个答案:

答案 0 :(得分:0)

如果这应该是透视变换,那么您要查找的术语是homography。 也许Matlab在这些链接中的功能就像你想要的那样:

http://www.csse.uwa.edu.au/~pk/research/matlabfns/#projective

http://www.robots.ox.ac.uk/~vgg/hzbook/code/

评论后编辑: 好的,所以我用Mathematica解决了方程式。如果您定义(为了便于阅读)

M=(x-x1)/(x2-x1)

N=(y-y1)/(y2-y1),

然后这对解决方案相当笨拙

{M -> -(X1 Y - X3 Y + X4 Y - X Y1 - X4 Y1 + X Y2 - 2 X1 Y2 + X3 Y2 + 
   X Y3 - X2 (Y - 2 Y1 + Y3) - X Y4 + 
   X1 Y4 + \[Sqrt](4 (X3 (-Y + Y1) + X1 (Y - Y3) + 
         X (-Y1 + Y3)) (-(X3 - X4) (Y1 - Y2) + (X1 - X2) (Y3 - 
            Y4)) + (X4 (-Y + Y1) + X3 (Y - 2 Y1 + Y2) + 
        X2 (Y - Y3) - X1 (Y - 2 Y3 + Y4) + 
        X (Y1 - Y2 - Y3 + Y4))^2))/(2 (-(X2 - X4) (Y1 - 
        Y3) + (X1 - X3) (Y2 - Y4))), 
N -> -(-X2 Y - X3 Y + X4 Y - X Y1 + 2 X3 Y1 - X4 Y1 + X Y2 - X3 Y2 +
    X Y3 + X2 Y3 - X Y4 + 
   X1 (Y - 2 Y3 + 
      Y4) - \[Sqrt](4 (X3 (-Y + Y1) + X1 (Y - Y3) + 
         X (-Y1 + Y3)) (-(X3 - X4) (Y1 - Y2) + (X1 - X2) (Y3 - 
            Y4)) + (X4 (-Y + Y1) + X3 (Y - 2 Y1 + Y2) + 
        X2 (Y - Y3) - X1 (Y - 2 Y3 + Y4) + 
        X (Y1 - Y2 - Y3 + Y4))^2))/(2 (-(X3 - X4) (Y1 - 
        Y2) + (X1 - X2) (Y3 - Y4)))} 

{M -> -(X1 Y - X3 Y + X4 Y - X Y1 - X4 Y1 + X Y2 - 2 X1 Y2 + X3 Y2 + 
   X Y3 - X2 (Y - 2 Y1 + Y3) - X Y4 + 
   X1 Y4 - \[Sqrt](4 (X3 (-Y + Y1) + X1 (Y - Y3) + 
         X (-Y1 + Y3)) (-(X3 - X4) (Y1 - Y2) + (X1 - X2) (Y3 - 
            Y4)) + (X4 (-Y + Y1) + X3 (Y - 2 Y1 + Y2) + 
        X2 (Y - Y3) - X1 (Y - 2 Y3 + Y4) + 
        X (Y1 - Y2 - Y3 + Y4))^2))/(2 (-(X2 - X4) (Y1 - 
        Y3) + (X1 - X3) (Y2 - Y4))), 
N -> -(-X2 Y - X3 Y + X4 Y - X Y1 + 2 X3 Y1 - X4 Y1 + X Y2 - X3 Y2 +
    X Y3 + X2 Y3 - X Y4 + 
   X1 (Y - 2 Y3 + 
      Y4) + \[Sqrt](4 (X3 (-Y + Y1) + X1 (Y - Y3) + 
         X (-Y1 + Y3)) (-(X3 - X4) (Y1 - Y2) + (X1 - X2) (Y3 - 
            Y4)) + (X4 (-Y + Y1) + X3 (Y - 2 Y1 + Y2) + 
        X2 (Y - Y3) - X1 (Y - 2 Y3 + Y4) + 
        X (Y1 - Y2 - Y3 + Y4))^2))/(2 (-(X3 - X4) (Y1 - 
        Y2) + (X1 - X2) (Y3 - Y4)))}

请注意,唯一的区别是Srqt之前的标志。

现在,您只需要重新定义上面MN的定义,即可获得xyx=M*(x2-x1)+x1y=N*(y2-y1)+y1