Quadrilateral to rectangle transform
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)
________ 开发者_JS百科 _________
/ .(X,Y) \ => | .(x,y) |
/__________\ |_________|
(X3,Y3) (X4,Y4) (x2,y1) (x2,y2)
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
.
精彩评论