View Question
 Question
 Subject: 2D Perspective Transform Category: Computers > Algorithms Asked by: ryandawson-ga 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.``` Clarification of Question by ryandawson-ga on 29 Apr 2005 15:14 PDT ```To help get you started, I have found some more stuff. I just don't know this well enough to do it myself. Gimp (which is like Photoshop for open source): http://www.gimp.org/source/ If you do a full text search on the downloaded code for 'gimp_drawable_transform_tiles_affine' that is pretty much the algorithm I need. They just have done a lot of absraction that I don't understand, and don't need. Please keep in mind that all I care about is taking an image, doing the perspective transform, and having a resulting image. If the list price is not enough, let me know and I will change it. This is my first time to post, some I am new to pricing.```
 There is no answer at this time.

 ```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 SX-1,SY-1. 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, ((SY-y)*x0 + (y)*x3)/SY, ((SY-y)*y0 + y*y3)/SY, ((SY-y)*x1 + (y)*x2)/SY, ((SY-y)*y1 + y*y2)/SY, ((SX-x)*x0 + (x)*x1)/SX, ((SX-x)*y0 + x*y1)/SX, ((SX-x)*x3 + (x)*x2)/SX, ((SX-x)*y3 + x*y2)/SX); } It uses two functions I defined: int det(int a, int b, int c, int d) { return a*d-b*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(x1-x2,y1-y2,x3-x4,y3-y4); if (d==0) d = 1; *px = det(det(x1,y1,x2,y2),x1-x2,det(x3,y3,x4,y4),x3-x4)/d; *py = det(det(x1,y1,x2,y2),y1-y2,det(x3,y3,x4,y4),y3-y4)/d; }```
 ```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...```
 ```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.```
 ```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/java-media/jai/downloads/download-1_1_2.html You can find info on how to run perspective transform in: http://java.sun.com/products/java-media/jai/forDevelopers/jai1_0_1guide-unc/ 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 same-sized 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.```