# Is perspective transform affine? If it is, why it's impossible to perspective a square by an affine transform, given by matrix and shift vector?

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: 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.

• 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). – darij grinberg Jun 7 '13 at 15:06
• @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! – Boris Burkov Jun 7 '13 at 15:14
• @Bob I've edited my answer to include the actual transformation. – Fly by Night Jun 7 '13 at 15:33
• @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. – Boris Burkov Jun 12 '13 at 13:19

## 1 Answer

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) .$$

• 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. – Boris Burkov Jun 12 '13 at 13:18
• @Bob You're welcome. Please take care when using the $\arctan$ function. It has a nasty habit of biting! – Fly by Night Jun 12 '13 at 17:29