Convert quadrangle to rectangle?

I have a scene consisting of one arbitrary quadrilateral. I need to be able to convert this quad to a rectangle. Each square is in 2d coordinates, so they have 4 vertices (x_i, y_i) .

The transformation should have the opposite, because the idea is to return to the original square after manipulating the rectangle.

What would be the easiest way to perform this operation? I heard this is called a perspective conversion, but I found some small tips that make me think it can be pretty easy to do.

+7
computer-vision graphics linear-algebra opengl
source share
3 answers

Do you know what size of the desired rectangle is? You can map any convex quadrangle to a rectangle with a reversible transform with a perspective transform, if so. All you have to do is get 4 corresponding points (between the quad and the rectangle), say, (X 1 , Y 1 ), (X 2 , Y 2 ), (X 3 , Y 3 ), (X 4 , Y 4 ) for a quadrangle and (x 1 , y 1 ), (x 2 , y 2 ), (x 3 , y 3 ), (x 4 , y 4 )) for a rectangle, respectively. Then connect it to the final equation in the Borealid link and you will set:

alt text

The solution to the above equation (where n = 4) will give you the elements (a, b, c, d, e, ..., h) of the matrix of reversible perspective transformations,

alt text

This will allow you to convert points on a rectangle to points on quadrangles. For the inverse transform, just invert the transform matrix.

Also note that as soon as you get the transformed coordinates of the vector [XW YW W] T, you need to normalize it so that W = 1. That is, Your final answer will be [XW ​​/ W YW / WW / W] T which equals [XY 1] T the desired answer.

+11
source share

Not all quadrangles are rectangles. For this reason, there is no reversible transformation from square to rectangle; there are more quads than rectangles, so you cannot create a reversible mapping from squares to rectangles.

However, you can create a reversible transformation for a specific quad. As you guessed, this concerns the rotation of perspective, so the quadrangle β€œappears” as a rectangle in the new coordinate space. See http://alumni.media.mit.edu/~cwren/interpolator/ which contains the Matlab source code for this problem.

+1
source share

This solution uses the Java Advance Image (JAI) API all the magic in the QuadToQuad method. Here is a sample code.

 try { BufferedImage img = UtilImageIO.loadImage(picName); ParameterBlock params = new ParameterBlock(); params.addSource(img); //source is the input image int w = img.getWidth(); //Set to the original width of the image int h = img.getHeight(); //Set to the original height of image Point tl = new Point(x,y); //The new top left corner Point tr = new Point((x1,y1); //The new top right corner Point bl = new Point(x2,y2); //The new bottom left corner Point br = new Point(x3,y3); //The new bottom right corner PerspectiveTransform p = PerspectiveTransform.getQuadToQuad(0,0, 0, h, w, h, w, 0, tl.x, tl.y, bl.x, bl.y, br.x, br.y, tr.x, tr.y).createInverse(); WarpPerspective wa = new WarpPerspective(p); params.add(wa); params.add(Interpolation.getInstance(Interpolation.INTERP_BICUBIC)); //Change the interpolation if you need more speed RenderedOp dest = JAI.create("warp", params); //dest is now the output File outputfile = new File(picName); ImageIO.write(dest, "jpg", outputfile); } catch(Exception e){} 

Hope this helps you. :)

0
source share

All Articles