I'm a bit confused. I want to program a perspective transformation and thought that it is an affine one, but seemingly it is not. As an example, I want to perspective a square into a quadrilateral (as shown below), but it seems impossible to represent such a transform as a matrix multiplication+shift:

enter image description here

1) What I can't understand is that by definition affine transform is the one, that preserves all the staight lines. Can you provide an example of straight line, which is not preserved in this case?

2) How do I represent perspective transforms as this one numerically?

Thank you.

  • $\begingroup$ Any transformation that takes some two parallel lines into two non-parallel lines is non-affine. "Preserving straight lines" describes the projective transformations (under appropriate algebraicity conditions, perhaps -- I don't remember), while affine transformations also need to "fix the line at infinity" (i. e., preserve parallelism). $\endgroup$ Jun 7 '13 at 15:06
  • $\begingroup$ @darijgrinberg Hm, interesting enough, in russian tradition only preservation of straight lines is required for a transformation to be called affine :) unlike english: en.wikipedia.org/wiki/Homography. Thanks! $\endgroup$ Jun 7 '13 at 15:14
  • $\begingroup$ @Bob I've edited my answer to include the actual transformation. $\endgroup$ Jun 7 '13 at 15:33
  • $\begingroup$ @darijgrinberg Sorry, I was wrong about russian tradition. Affine transforms in russian are the same as in english. It was just a wrong definition in russian Wikipedia. $\endgroup$ Jun 12 '13 at 13:19

There is no affine transformation that will do what you want. If two lines are parallel before an affine transformation then they will be parallel afterwards. You start with a square and want a trapezium. This is not possible. The best you can get is a parallelogram.

You will need to move up a level and look at projective transformations. Affine transformations form a subset of the projective transformations. They are the ones that fix the line at infinity. (Parallel lines are thought to cross at infinity.)

In local coordinates, a projective transformation is given by: $$(x,y) \longmapsto \left(\frac{ax+by+c}{gx+hy+k},\frac{dx+ey+f}{gx+hy+k}\right) $$

It is possible to find all of the constants by substituting and solving. I get:

$$T : (x,y) \longmapsto \left( \frac{12x+3y}{4y+16} , \frac{3y}{y+4} \right) . $$

  • $\begingroup$ Ok, thank you, Fly. The affine transform in 3D is just application of 3D rotation matrix+shift vector to the initial (x,y,z) and then x_projection, y_projection = arctan(x/z), arctan(y/z). Thank you. I'm now trying to cut the initial shape into triangles and approximate perspective projection with affine transforms, applied to those triangles. $\endgroup$ Jun 12 '13 at 13:18
  • $\begingroup$ @Bob You're welcome. Please take care when using the $\arctan$ function. It has a nasty habit of biting! $\endgroup$ Jun 12 '13 at 17:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.