四边形到矩形变换

发布于 2024-11-14 13:12:56 字数 555 浏览 2 评论 0 原文

我想将四边形图像转换为我知道这些顶点的矩形图像。例如,在下图中,我知道坐标 (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) 

I want to transform a quadrilateral image to a rectangular image which I know those vertices. for example in the image below, I know the coordinate (X1,Y1) ~ (X4,Y4) and (x1,y1) ~ (x2,y2) and I want to transform it into rectangle. how can I obtain (x,y) coordinate in rectangular image which is correspond to (X,Y) coordinate in quadrilateral image?

 ____> 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) 

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

慢慢从新开始 2024-11-21 13:12:56

如果这应该是透视变换,那么您要查找的术语是单应性
也许这些链接中的 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 的定义即可得到 x,yx=M*(x2-x1)+x1y=N*(y2-y1)+y1

If this is supposed to be a perspective transformation, the term you look for is homography.
Maybe the Matlab functions in these links cando what you want:

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

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

Edited after comments:
Ok, so I solved the equations with Mathematica. If you define (for readability)

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

and

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

then the pair of solutions is the rather unwieldy

{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)))} 

and

{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)))}

Note that the only difference is the sign before the Srqt.

Now you only have to re-form the definitions of M and N above to get x,y. x=M*(x2-x1)+x1, y=N*(y2-y1)+y1.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文