

Subject:
2D Perspective Transform
Category: Computers > Algorithms Asked by: ryandawsonga List Price: $20.00 
Posted:
29 Apr 2005 10:02 PDT
Expires: 29 May 2005 10:02 PDT Question ID: 515829 
I am looking to do a 2D perspective transform on an image in code (either c++, java, or c#, or an algorithm that is easy to port). In photoshop, you can get this effect by going to the Edit Menu>Trasform>Perspective.  


There is no answer at this time. 

Subject:
Re: 2D Perspective Transform
From: sumarega on 17 May 2005 17:41 PDT 
I recently wrote a function that you pass: x0,y0, x1,y1, x2,y2, x3,y3: Coordinates of four corners of a box, in clockwise order. x,y: coordinates of two points in a flat image from 0,0 to SX1,SY1. It writes the result on *px, *py. The result is the point which corresponds to x,y inside projection box. I used int because of performance. Floating point would probably give better results. The code is: void corPix(int x0, int y0, int x1, int y1, int x2, int y2, int x3, int y3, int x, int y, int *px, int *py) { intersectLines(px, py, ((SYy)*x0 + (y)*x3)/SY, ((SYy)*y0 + y*y3)/SY, ((SYy)*x1 + (y)*x2)/SY, ((SYy)*y1 + y*y2)/SY, ((SXx)*x0 + (x)*x1)/SX, ((SXx)*y0 + x*y1)/SX, ((SXx)*x3 + (x)*x2)/SX, ((SXx)*y3 + x*y2)/SX); } It uses two functions I defined: int det(int a, int b, int c, int d) { return a*db*c; } void intersectLines(int *px, int *py, int x1, int y1, int x2, int y2, int x3, int y3, int x4, int y4) { int d = det(x1x2,y1y2,x3x4,y3y4); if (d==0) d = 1; *px = det(det(x1,y1,x2,y2),x1x2,det(x3,y3,x4,y4),x3x4)/d; *py = det(det(x1,y1,x2,y2),y1y2,det(x3,y3,x4,y4),y3y4)/d; } 
Subject:
Re: 2D Perspective Transform
From: ryandawsonga on 18 May 2005 19:52 PDT 
Would you mind clarifying exactly what *px and *py are? I understand they are int pointers, but I don't exactly understand what they are for or what they do in relation to getting the new image. thanks... 
Subject:
Re: 2D Perspective Transform
From: directivega on 23 Dec 2005 10:26 PST 
I believe they are suggesting that you change any lines, pixels, etc that you pass the coordinates to this routine and they output the "transformed" coordinates through the pointers px and py. Unfortunately, if you are using routines that involve texturebrushes or drawimage, this won't help. I have been looking into alternatives for a project myself, all I can tell you so far is to check out the MSDN example for Matrix.Shear(x,y). It does some of the work involved in a perspective transform. Unless I find a better technique, I am probably going to use a loop to shear and scale the bitmap in slices onto a temporary image. Do post anything good you find about perspective warping a graphics object or an image in GDI+, please. 
Subject:
Re: 2D Perspective Transform
From: davidedcga on 27 Apr 2006 04:46 PDT 
The Java Advanced Imaging API allows you to easily perform perspective transform. As in Java2D and Java3D, these routines are optimised, they are not run in the usual java interpreted manner  so they are very fast as well. The JAI is downloadable from http://java.sun.com/products/javamedia/jai/downloads/download1_1_2.html You can find info on how to run perspective transform in: http://java.sun.com/products/javamedia/jai/forDevelopers/jai1_0_1guideunc/ Extract below. Regards, Davide Della Casa  Perspective distortions in images are sometimes introduced when the camera is at an angle to the subject. For an example, think of a camera in an aircraft above the earth. If the camera is aimed straight down, the resulting image will be a flat perspective image; that is, no distortion. All objects in the image appear in correct size relative to one another. However, if the camera is angled toward the Parameters: theta The angle of rotation in radians. Parameters: theta The angle of rotation in radians. x The x coordinate of the anchor point of the rotation. y The y coordinate of the anchor point of the rotation. Parameters: sx The factor by which coordinates are scaled along the x axis direction. sy The factor by which coordinates are scaled along the y axis direction. Parameters: shx The multiplier by which coordinates are shifted in the direction of the positive x axis as a factor of their y coordinate. shy The multiplier by which coordinates are shifted in the direction of the positive y axis as a factor of their x coordinate. 8.4 Perspective Transformation GEOMETRIC IMAGE MANIPULATION 276 Programming in Java Advanced Imaging earth horizon, perspective distortion is introduced. Objects closer to the camera appear larger than samesized objects farther away from the camera. Perspective distortion has reduced the scale of the objects farthest away. Perspective distortion can be corrected by applying a perspective transform. The perspective transform maps an arbitrary quadrilateral into another arbitrary quadrilateral, while preserving the straightness of lines. Unlike an affine transformation, the parallelism of lines in the source is not necessarily preserved in the output. 
If you feel that you have found inappropriate content, please let us know by emailing us at answerssupport@google.com with the question ID listed above. Thank you. 
Search Google Answers for 
Google Home  Answers FAQ  Terms of Service  Privacy Policy 