Change of Basis; Image Compression
Image compression is a change of basis. An image is a vector of pixel values in the standard basis, and a better basis — Fourier for JPEG, or wavelets — lets you throw away most of the coefficients without the eye noticing. Then the math behind it: the matrix of a linear transformation depends on the basis, matrices of the same transformation in different bases are similar, and the perfect basis is the eigenvector basis, where the matrix is diagonal.
An image is a vector
Coming near the end of the course, this lecture mixes the linear algebra of changing basis with an application you meet every day: compression. Compressing a signal, compressing an image — “that’s exactly change-of-basis.” Strang’s own example is the video of this very lecture: the full signal sits on the videotape, but what goes out on the web is compressed, which is why his motion looks jerky while the writing on the board comes through clearly — his motion “needs a whole lot of bits.”
Take a still image, black and white, pixels — by . Each pixel carries a grayscale value from to , so eight bits per pixel ( possibilities). The image is then a vector
one component per pixel. A color image needs three times that. This is an enormous amount of information — the lectures could not be sent out without compressing it. Some compression is lossless, exploiting redundancies exactly; this lecture is about lossy compression, where information is deliberately thrown away.
Why the standard basis is lousy
As it arrives, the image is written in the standard basis: the vectors , , and so on — every pixel gets its own value, maybe here and next door. But neighboring pixels are highly correlated: lots of nearly identical values for the blue shirt, a whole lot more for the blackboard. The extreme case is a blank blackboard before lecture — every gray level practically the same — and it would make no sense to report that image pixel by pixel. The standard basis makes no use of the fact that neighbors tend to agree.
So choose a better basis. One vector that would be extremely nice to have is the vector of all ones: that single vector completely carries a solid image. At the other extreme, the checkerboard vector would carry the whole signal if the image alternated pixel by pixel — but a smooth image contains very little of that highest frequency. In between, a vector of half ones and half minus ones captures an image that is darker on one half and lighter on the other.
We are free to choose the basis, and “it’s a billion-dollar decision what we choose.” TV people prefer one basis based on how the signal is scanned, movie people prefer another — giant politics that reduces to a linear algebra problem. The best-known choice, the one JPEG uses, is the Fourier basis: the constant vector of all ones (the DC vector, to electrical engineers) together with the columns of the Fourier matrix, powers of a complex number , from the FFT lecture. JPEG doesn’t transform the whole image at once — that’s too much. It breaks the image into blocks, pixels each, and changes basis in block by block.
The compression pipeline: lossless, then lossy
Here is the whole scheme, and it’s important to see which step loses information.
Sixty-four pixels come in; sixty-four coefficients come out. The change of basis is lossless — has plenty of bases and we’ve simply chosen one and rewritten the signal in it. Then comes the compression step, and that’s lossy: thresholding. Set a threshold and throw away every coefficient below it — every basis vector present in an amount the eye can’t see, or can hardly see. The coefficient of the all-ones vector is usually large and is very seldom thrown away; the coefficient of the fast-alternating checkerboard vector is probably tiny in any smooth signal (that’s noise, and “very little noise in this lecture”). The result is a compressed set of coefficients with many zeros. Reconstruct by summing coefficients times basis vectors — but the sum no longer has terms; maybe two or three. From down to is compression of about to , and “that’s the kind of compression you’re looking for.”
Strang’s favorite still-image story is the FBI. Fingerprints used to go to Washington as inky marks in filing cabinets — thirty million of them, with no way to retrieve one in under a week. Digitizing helped, but a raw fingerprint is still too many pixels to search. So the FBI had to choose a basis for compressing fingerprints, built a facility in West Virginia, and now a match takes minutes.
Video adds one more idea. You could compress each frame as a still image, but successive frames are extremely correlated. So use prediction and correction: assume the next frame equals the current one, then code and compress only the small correction. The whole game of compression, in time or in space, is exploiting the fact that things change smoothly, so each value can be predicted from the previous one.
The wavelet basis
The competition for Fourier is called wavelets. In the case (Strang drops to one dimension — eight pixel values along a row), the simple wavelet basis for is:
plus three more like the last one, with the pair sliding along: in positions , positions , and positions . Coarse scales first, fine details last. And every vector in is some combination of these — for instance, the fast checkerboard is exactly the sum of the last four wavelets.
Now the linear algebra step: given the pixel values in the standard basis, find the coefficients with . But a combination of vectors is a matrix times a vector — “the step we’re constantly taking in this course”: put the basis vectors into the columns of a matrix . Then
That formula exposes the critical point: a good basis has a nice, fast inverse. Two requirements make a basis good:
- Fast. You must be able to multiply by and by quickly, or the transform costs more time than you can afford. Fourier passes because of the FFT; there is a fast wavelet transform too. The wavelet vectors are all ones, minus ones, and zeros — fast in binary. Better still, check the dot products: four s against four s gives zero; two against two gives zero. The basis is orthogonal. Divide by the lengths (, , , …) to make it orthonormal, and then — Section 4.4 — . The inverse is as fast as the forward transform.
- Good compression. The fastest thing of all is not to change basis — but the standard basis is poor for compression: throw away of the pixel values and the picture just goes dark. A good basis needs a few basis vectors to come close to the signal, so that discarding loses only little blips.
JPEG 2000, the next compression standard, includes wavelets — though the FBI uses a smoother wavelet, without the jump from to .
The matrix of a transformation — and what changing basis does to it
Now the math half of the lecture: the connection between a linear transformation , which needs no coordinates, and the matrix that describes it once coordinates are chosen. The matrix is the coordinate-based description of the transformation.
Suppose maps eight-dimensional space to eight-dimensional space — a rotation, say, or a projection. With respect to one basis it has a matrix ; with respect to a second basis it has a matrix . What’s the connection?
Strang admits this part of the lecture is, fittingly, “compressed” — the conclusion is what he wants you to spot: change the basis, and two things happen. Every vector gets new coordinates, by the rule . Every transformation gets a new matrix, related to the old one by similarity, with .
How do you build the matrix in the first place? Recall from the last lecture: if you know what does to the eight basis vectors, you know completely. Why? Because every input is a combination — that’s the whole point of a basis, every vector is a combination of the basis vectors in exactly one way — and linearity forces
So the recipe builds column by column: apply to the first basis vector, expand the output in the basis,
and those numbers are the first column. Do the same with for the second column, and so on: sixty-four numbers, and the matrix represents in that basis.
The perfect basis: eigenvectors
Close with the best example of all. Suppose the basis consists of eigenvectors of , so . What is ? Run the recipe, one minute flat. First input , output — and expanding that in the basis is already done: times the first basis vector, zero times the others. So the first column is and zeros. Second column: in the second slot, zeros elsewhere. In the eigenvector basis,
The matrix is diagonal.
Problems
Work these before revealing the solutions. Everything you need is in the lecture.
Take the wavelet basis for :
Write the pixel vector as a combination . (Hint: this basis is orthogonal — use that instead of elimination.)
Show solution
Because the basis is orthogonal, dot both sides with each : all cross terms vanish, so .
Check: . ✓ The big coefficient is the average brightness — the one you’d never threshold away.
For the basis of Problem 31.1, verify that all four vectors are mutually orthogonal. Then rescale them to unit length, put them into the columns of a matrix , and write down without doing elimination.
Show solution
Check the six dot products: ; ; ; ; ; . All orthogonal.
The lengths are and . Dividing each column by its length gives
an orthogonal matrix: orthonormal columns. Therefore — just flip it across the diagonal. That is exactly the “good basis has a nice, fast inverse” property: the inverse transform costs the same as the forward one.
Let be the projection of onto the line through . Using the basis , , follow the lecture’s recipe to find the matrix of in this basis. Why did this basis make the answer so clean?
Show solution
First column: input lies on the line, so projection leaves it alone: . First column: .
Second column: input is perpendicular to the line, so it projects to zero: . Second column: .
The answer is diagonal because and are eigenvectors of the projection, with eigenvalues and . This is the lecture’s closing point in miniature: in the eigenvector basis, the matrix of the transformation is diagonal.
The projection of Problem 31.3 has matrix in the standard basis. Verify the lecture’s claim that and are similar: with (the eigenvector basis in its columns), check that .
Show solution
First : since ,
Now multiply:
Same transformation, two bases, two matrices — and the change-of-basis matrix is exactly the similarity: , equivalently . Note the shared fingerprints of similarity: both matrices have eigenvalues and , and both have trace .
A grayscale image has pixels at bits each. (a) How many bits is the raw image? (b) JPEG-style compression splits it into blocks and, after the change of basis, keeps on average of the coefficients per block. What is the compression ratio? (c) Why can’t you get the same effect by simply keeping of every pixel values in the standard basis?
Show solution
(a) bits — about two million bits for one modest black-and-white image.
(b) Each block goes from coefficients to , so the ratio is (roughly — the surviving coefficients still cost bits to encode, but the count of numbers drops sixteenfold). The number of blocks is , but the ratio is the same for every block.
(c) In the standard basis, each kept number describes one pixel and says nothing about the others: keep pixels of every and the other are simply gone — the picture “goes dark,” as the lecture says of throwing away of pixel values. In a good basis, the first few coefficients describe the whole block at once (its average, its coarse light–dark structure), so the discarded coefficients were only carrying small, high-frequency blips the eye barely sees.