Sparse perspective transformations
Th ere is a special function, cvPerspectiveTransform(), that performs perspective transformations
on lists of points; we cannot use cvTransform(), which is limited to linear operations.
As such, it cannot handle perspective transforms because they require division
by the third coordinate of the homogeneous representation (x = f ∗ X/Z, y = f ∗ Y/Z). Th e
special function cvPerspectiveTransform() takes care of this for us
As usual, the src and dst arguments are (respectively) the array of source points to be
transformed and the array of destination points; these arrays should be of three-channel,
fl oating-point type. Th e matrix mat can be either a 3-by-3 or a 4-by-4 matrix. If it is
3-by-3 then the projection is from two dimensions to two; if the matrix is 4-by-4, then
the projection is from four dimensions to three.
In the current context we are transforming a set of points in an image to another set of
points in an image, which sounds like a mapping from two dimensions to two dimensions.
But this is not exactly correct, because the perspective transformation is actually
mapping points on a two-dimensional plane embedded in a three-dimensional space
back down to a (diff erent) two-dimensional subspace. Th ink of this as being just what
a camera does (we will return to this topic in greater detail when discussing cameras
in later chapters). Th e camera takes points in three dimensions and maps them to the
two dimensions of the camera imager. Th is is essentially what is meant when the source
points are taken to be in “homogeneous coordinates”. We are adding an additional
dimension to those points by introducing the Z dimension and then setting all of the
Z values to 1. Th e projective transformation is then projecting back out of that space
onto the two-dimensional space of our output. Th is is a rather long-winded way of explaining
why, when mapping points in one image to points in another, you will need a
3-by-3 matrix.
Output of the code in Example 6-3 is shown in Figure 6-14 for affi ne and perspective
transformations. Compare this with the diagrams of Figure 6-13 to see how this works
with real images. In Figure 6-14, we transformed the whole image. Th is isn’t necessary;
we could have used the src_pts to defi ne a smaller (or larger!) region in the source image
to be transformed. We could also have used ROIs in the source or destination image
in order to limit the transformation