Matrix multiplication is fundamental to image manipulation. It is a mapping function. We "map" a vector from one frame of reference to another. Looking back to the first article in our series, the multiplication of two matrices was shown to be
Let's look at how this works for a 2-by-2 matrix multiplied by a 2-by-1 vector. Here is how we will reference the values in these matrices:
The result is a 2-by-1 vector transformed by matrix A. Expanding out the math below, notice the interaction between the terms: each value of the result is a combination of several values of the input.
Matrix A is a transformation matrix, and its values, when mapped to vector b, transform it. The correct choice of our transformation matrix is fundamental to creating the right effect for our 2-by-13 house matrix.
While we could manipulate each of the 2-by-1 vectors in our set of 13 individually, that would be sloppy. Linear algebra rescues us from the tedium. The matrix multiplication problem we looked at before readily extends to larger matrices. A 2-by-2 matrix multiplied by a 2-by-1 vector results in a 2-by-1 vector, but a 2-by-2 matrix multiplied by a 2-by-13 matrix results in another 2-by-13 matrix.
In essence each column of the original matrix is transformed by the matrix multiplication into the corresponding column of the result matrix. With matrix mathematics, we do the operation in one step!
Let's add a transformation matrix to our example code before we generate the plot. To keep things simple, for the moment we'll use an identity matrix. Using an identity matrix is the equivalent of multiplying everything by 1 -- a kind of "do nothing" operation. However, it does provide us a way to make sure "the math works," and the identity matrix is the basis for most transformation matrices.
from pxDislin import * from Numeric import * plot = dPlot() trans = array([[1,0], [0,1]]) axis = dAxis(-20,20,-20,20) plot.add_axis(axis) house_pts_orig = array([[5,5,-5,-5,5,4,4,2,2,5,5,0,-5], [-5,5,5,-5,-5,-5,-1,-1,-5,-5,5,8,5]]) house_pts = matrixmultiply(trans,house_pts_orig) house = dMultiLine(house_pts,house_pts) plot.add_object(house) plot.show()
The plot generated is the same as the first one shown.