Linear Algebra, Live
Strang §7.1 · Image processing by linear algebra

Compressing an image with the SVD

Slide 3 split a matrix into rank-one pieces, loudest first. Now we use that on photos. A grey photo is just a big table of numbers, one per pixel, so it is a matrix. Think of a painter. First she blocks in the big areas, a dark sky and a light field. Then she adds the smaller shapes, and the fine detail comes last. The SVD splits a photo into layers in the same order: the first pieces carry the big shapes and the last ones carry tiny detail. Keep the first few pieces and throw the rest away. The picture still looks right, and you store far fewer numbers. That is image compression.

Lesson 8 · slide 4Image compression
A ≈ A_k = σ₁u₁v₁ᵀ + … + σₖuₖvₖᵀ    error set by the dropped σ's
Key ideas
  1. A grey image with m rows and n columns of pixels is an m by n matrix.
  2. A_k, the first k SVD pieces, is the closest matrix of rank k to A (the Eckart–Young theorem): A − A_k stretches every unit vector by at most σₖ₊₁, and no other rank-k matrix does better.
  3. Storing A_k needs k(m + n + 1) numbers instead of mn.
  4. Fast-falling singular values mean the image compresses well.

Pictures are matrices

Give each pixel a number from 0 (white) to 1 (black). A small flag of three stripes, black, grey and white, with 4 pixels in each row, is a 3 by 4 matrix. Each row repeats one number, so the whole flag is a column of stripe values times a row of ones: one rank-one piece.

10.50 1111 = 11110.50.50.50.50000
12 pixels, but only 3 + 4 = 7 numbers to store (σ is already folded into the column here)
A column of stripe values times a row of ones paints the whole flag. Every pixel is one product.

A 1000 by 1000 photo is a million numbers. Real photos are not one piece, but they are close to a sum of not too many pieces.

Keep k pieces

A_k = σ₁u₁v₁ᵀ + σ₂u₂v₂ᵀ + … + σₖuₖvₖᵀ
Each piece costs m + n + 1 numbers: u, v and σ. (You could fold σ into u and store m + n, like the flag above. We count σ separately; the difference is tiny.)
1000 by 1000 photo, k = 50: 50 · 2001 ≈ 100,000 numbers instead of 1,000,000

No matrix of rank k is closer to A than A_k. The error A − A_k is exactly the dropped pieces, so for every unit vector x, ‖(A − A_k)x‖ ≤ σₖ₊₁, the largest σ dropped. Small dropped σ's mean a good copy.

There is also a quick way to see how much each piece holds. Add up the squares of all the pixel values. That total equals σ₁² + σ₂² + σ₃² + …, in two steps:

Step 1: each diagonal entry of AᵀA is a column of A dotted with itself, its squared length. So the diagonal of AᵀA adds up to the sum of all squared entries of A
Step 2: the sum of the diagonal (the trace) equals the sum of the eigenvalues (lesson 7), and the eigenvalues of AᵀA are σ₁², σ₂², … (slide 2)

So σ₁² divided by that total is the share piece 1 carries. The share tells you how much a piece holds; σₖ₊₁ bounds the worst error.

A worked example: a 4 by 4 block

Here is a tiny 4 by 4 corner of a picture: a black top half and a black block at the bottom left.

B = 1111111111001100:   only two different columns, 1111 and 1100, so rank 2
Sum of squares: 12 black pixels, so σ₁² + σ₂² = 12
A computer gives σ₁ ≈ 3.24 and σ₂ ≈ 1.24. (Exactly, σ₁ = 1 + √5 and σ₂ = √5 − 1.)   Check: σ₁² + σ₂² = (6 + 2√5) + (6 − 2√5) = 12 ✓
Piece 1 carries σ₁²/12 = 10.47/12 ≈ 87% of the sum of squares
Piece 1 already shows dark on the top left and light on the bottom right. Piece 2 sharpens the edges, and together they give B back.

For a block this small, two pieces cost 2 · (4 + 4 + 1) = 18 numbers, more than the 16 pixels. The saving only shows up for big pictures, where k is tiny compared with m and n.

Keep reading with the full course

Finish lesson 8 and you will be able to: compress an image and keep only what matters.

Still locked here:

  • The rest of this explanation
  • 4 more examples with step-by-step solutions
  • Five extra members-only problems with worked solutions

$20 once, lifetime access. Lessons 1 to 3 stay free.

Examples

Example 1 · A flag with stripes

Three horizontal stripes: each row is one number repeated: a number times the all-ones row.

Rank 1: one piece (a column of stripe values times a row of ones) rebuilds it exactly.

Exercise Choose k, the number of pieces to keep, so that the copy matches the letter F exactly. Edit the code and press Run.
slide_4.py Loading Python…
1
Output · Ctrl+Enter runs
Press Run to execute this slide's code. Ctrl+Enter works too.

Questions and comments

Stuck on something, or found a mistake? Ask here and I'll answer. You sign in with GitHub.

The full course

From Ax = b to PCA, by running it

Lessons 1 to 3 are free. Finish all eight and you will be able to:

You get: all 8 lessons with Python labs · every worked example and solution · 5 extra problems per lesson · your progress tracked · answers to your questions · every future update.

$20 once · lifetime access
Continue to checkout →

Already a member? Log in to the course