Tutorials › Linear Algebra › Image Compression With Truncated SVD

Pseudoinverse and Low-Rank Approximation · Tutorial 822 of 1000

Image Compression With Truncated SVD

Represent a grayscale image with a matrix, choose a truncated SVD reconstruction, and compare its approximation error with its storage cost.

Advanced 11 min read

What You'll Learn

  • Represent grayscale pixel values as entries of a real matrix.
  • Construct a rank-k reconstruction from the leading singular triplets.
  • Compute Frobenius reconstruction error from the discarded singular values.
  • Compare the number of stored values in a matrix and its truncated SVD.
  • Decide when a low-rank representation saves storage.
  • Recognize how clipping and quantization affect a practical reconstruction.

From Pixels to a Matrix

A grayscale image assigns one intensity value to each pixel. If an image has \(m\) rows and \(n\) columns of pixels, place its intensities in a matrix \(\mathbf{A}\in\mathbb{R}^{m\times n}\), with \(a_{ij}\) the intensity at row \(i\), column \(j\). For example, an 8-bit image uses values from \(0\) to \(255\), where \(0\) is black and \(255\) is white. The matrix need not have any special algebraic form: the goal is to find a compact representation that preserves the important variation in its entries.

For a real matrix \(\mathbf{A}\), a singular value decomposition (SVD) writes it as a sum of rank-one matrices: \(\mathbf{A}=\sum_{i=1}^{r}\sigma_i\mathbf{u}_i\mathbf{v}_i^{\top}\). Here \(r=\operatorname{rank}\mathbf{A}\), the singular values satisfy \(\sigma_1\ge\cdots\ge\sigma_r>0\), and each of the lists \(\mathbf{u}_1,\dots,\mathbf{u}_r\) and \(\mathbf{v}_1,\dots,\mathbf{v}_r\) is orthonormal. A singular triplet consists of \((\sigma_i,\mathbf{u}_i,\mathbf{v}_i)\). Keeping the first \(k\) triplets gives a matrix that uses fewer factors than the full image matrix.

Definition: For an integer \(k\) with \(0\le k\le r\), the rank-\(k\) truncated SVD reconstruction is $$ \mathbf{A}_k=\sum_{i=1}^{k}\sigma_i\mathbf{u}_i\mathbf{v}_i^{\top}. $$ For \(k=0\), the sum is the zero matrix. For \(k=r\), \(\mathbf{A}_k=\mathbf{A}\). Each term \(\sigma_i\mathbf{u}_i\mathbf{v}_i^{\top}\) has rank one, so \(\operatorname{rank}\mathbf{A}_k\le k\).

Each retained triplet stores \(m\) entries of \(\mathbf{u}_i\), \(n\) entries of \(\mathbf{v}_i\), and one singular value. Thus a direct factor representation uses \(k(m+n+1)\) real numbers, before accounting for any additional metadata or encoding. By contrast, storing every pixel as a matrix entry uses \(mn\) numbers. This comparison counts real values, not the bits required to encode them; practical image formats also use quantization and other coding choices.

Measuring the Reconstruction Error

The Frobenius norm of a matrix is the square root of the sum of the squares of all its entries: \(\|\mathbf{B}\|_F=\sqrt{\sum_{i,j}b_{ij}^2}\). For images, it measures the aggregate squared pixel difference between an original matrix and a reconstruction. The relative Frobenius error divides this difference by \(\|\mathbf{A}\|_F\), provided \(\mathbf{A}\ne\mathbf{O}\). The operator norm, written \(\|\mathbf{B}\|_2\), measures the largest singular value of \(\mathbf{B}\).

Result (truncated SVD error): If \(\mathbf{A}\in\mathbb{R}^{m\times n}\) has singular values \(\sigma_1\ge\cdots\ge\sigma_r>0\), then for \(0\le k\le r\), $$ \|\mathbf{A}-\mathbf{A}_k\|_F =\sqrt{\sum_{i=k+1}^{r}\sigma_i^2}, \qquad \|\mathbf{A}-\mathbf{A}_k\|_2=\sigma_{k+1}\quad\text{if }k<r. $$ When \(k=r\), both errors are zero.

Proof. Write the discarded part as \(\mathbf{A}-\mathbf{A}_k=\sum_{i=k+1}^{r}\sigma_i\mathbf{u}_i\mathbf{v}_i^{\top}\). For matrices \(\mathbf{u}_i\mathbf{v}_i^{\top}\) and \(\mathbf{u}_j\mathbf{v}_j^{\top}\), their Frobenius inner product is \(\operatorname{tr}((\mathbf{u}_i\mathbf{v}_i^{\top})^{\top}\mathbf{u}_j\mathbf{v}_j^{\top})=(\mathbf{u}_i^{\top}\mathbf{u}_j)(\mathbf{v}_j^{\top}\mathbf{v}_i)\). Orthonormality makes this \(1\) when \(i=j\) and \(0\) otherwise. Therefore the squared Frobenius norm of the discarded sum is \(\sum_{i=k+1}^{r}\sigma_i^2\). Its nonzero singular values are precisely the discarded singular values, so its operator norm is the largest of them, \(\sigma_{k+1}\), when \(k<r\). If \(k=r\), the discarded sum is \(\mathbf{O}\), and both norms are zero. \(\square\)

The Eckart–Young theorem, established in the earlier material on low-rank approximation, says that \(\mathbf{A}_k\) achieves the smallest possible Frobenius error among all matrices of rank at most \(k\); it also minimizes the operator-norm error. The formula above quantifies the error for this optimal choice: the omitted singular values determine how much is lost. A small retained rank is useful when the tail of the singular-value list is small, not merely because the matrix has many entries.

A Reconstruction With Corrected Pixel Errors

Worked Example: A Rank-Two Reconstruction of a 4-by-4 Image Matrix

Consider the grayscale matrix $$ \mathbf{A}=\frac14 \begin{bmatrix} 23&17&13&11\\ 17&23&11&13\\ 13&11&23&17\\ 11&13&17&23 \end{bmatrix}. $$ To identify its singular triplets, use the orthonormal vectors \(\mathbf{p}=(1,1,1,1)^{\top}/2\), \(\mathbf{q}=(1,1,-1,-1)^{\top}/2\), \(\mathbf{r}=(1,-1,1,-1)^{\top}/2\), and \(\mathbf{s}=(1,-1,-1,1)^{\top}/2\). They are mutually orthogonal and each has norm \(1\).

Direct multiplication gives \(\mathbf{A}\mathbf{p}=16\mathbf{p}\), \(\mathbf{A}\mathbf{q}=4\mathbf{q}\), \(\mathbf{A}\mathbf{r}=2\mathbf{r}\), and \(\mathbf{A}\mathbf{s}=\mathbf{s}\). Since \(\mathbf{A}\) is symmetric and these eigenvalues are positive, these are also singular triplets with left and right vectors equal. The singular values are \(16,4,2,1\), in that order. Retaining the first two gives \(\mathbf{A}_2=16\mathbf{p}\mathbf{p}^{\top}+4\mathbf{q}\mathbf{q}^{\top}\), or $$ \mathbf{A}_2= \begin{bmatrix} 5&5&3&3\\ 5&5&3&3\\ 3&3&5&5\\ 3&3&5&5 \end{bmatrix}. $$

Subtracting entry by entry gives $$ \mathbf{A}-\mathbf{A}_2= \begin{bmatrix} 3/4&-3/4&1/4&-1/4\\ -3/4&3/4&-1/4&1/4\\ 1/4&-1/4&3/4&-3/4\\ -1/4&1/4&-3/4&3/4 \end{bmatrix}. $$ There are four diagonal errors of \(3/4\). The off-diagonal errors at positions \(12,13,14,23,24,34\) are respectively \(-3/4,1/4,-1/4,-1/4,1/4,-3/4\), and each occurs twice in the full matrix. Thus $$ \|\mathbf{A}-\mathbf{A}_2\|_F^2 =4(3/4)^2+2\bigl[2(3/4)^2+4(1/4)^2\bigr] =5. $$ The error is \(\sqrt{5}\). The full matrix has squared Frobenius norm \(16^2+4^2+2^2+1^2=277\), so its relative Frobenius error is \(\sqrt{5/277}\). The singular-value error formula checks the direct computation: the discarded squared singular values are \(2^2+1^2=5\).

Storage check: the \(4\times4\) pixel matrix uses \(16\) numbers. The rank-two factor representation uses \(2(4+4+1)=18\) numbers. Although it gives an optimal rank-two approximation, in this small example it does not reduce the number of stored real values.

When Does the Factor Representation Save Values?

Storage criterion: Under the direct-counting convention above, a rank-\(k\) factor representation uses fewer real numbers than an \(m\times n\) matrix exactly when \(k(m+n+1)<mn\).

Proof. The original matrix has \(m\) rows and \(n\) columns, hence \(mn\) entries. For each of the \(k\) triplets, the left vector has \(m\) entries, the right vector has \(n\) entries, and the singular value contributes one more. The factor storage count is therefore \(k(m+n+1)\). Comparing the two counts gives a storage reduction precisely when \(k(m+n+1)<mn\); equality means no reduction, and a larger factor count means the factor representation uses more values. \(\square\)

This count treats all values equally and excludes storage for dimensions, file headers, and the precision or coding of each value. It is a useful first test, not a guarantee about the size of a finished image file. The truncated SVD is most attractive when \(k\) is small relative to both image dimensions and the discarded singular values are small enough for the desired visual quality.

Worked Example: Choosing a Rank for a 5-by-4 Matrix

Take $$ \mathbf{A}= \begin{bmatrix} 8&0&0&0\\ 0&4&0&0\\ 0&0&2&0\\ 0&0&0&1\\ 0&0&0&0 \end{bmatrix}. $$ The first four standard coordinate vectors in \(\mathbb{R}^5\) are left singular vectors, and the four standard coordinate vectors in \(\mathbb{R}^4\) are right singular vectors. The singular values are \(8,4,2,1\), as can be checked by multiplying each corresponding coordinate vector by \(\mathbf{A}\) and obtaining the stated singular value times its left vector.

For \(k=1\), keeping the first triplet gives $$ \mathbf{A}_1= \begin{bmatrix} 8&0&0&0\\ 0&0&0&0\\ 0&0&0&0\\ 0&0&0&0\\ 0&0&0&0 \end{bmatrix}. $$ Subtracting this matrix from \(\mathbf{A}\) leaves diagonal entries \(4,2,1\) in rows 2, 3, and 4, with every other entry zero. Therefore \(\|\mathbf{A}-\mathbf{A}_1\|_F=\sqrt{4^2+2^2+1^2}=\sqrt{21}\). Also, \(\|\mathbf{A}\|_F=\sqrt{8^2+4^2+2^2+1^2}=\sqrt{85}\), so the relative error is \(\sqrt{21/85}\). The factor representation stores \(1(5+4+1)=10\) values, compared with \(5\cdot4=20\) matrix entries.

For \(k=2\), the reconstruction retains the diagonal entries \(8\) and \(4\), so the error is \(\sqrt{2^2+1^2}=\sqrt{5}\). But the factor count is \(2(5+4+1)=20\), equal to the full matrix count. This example shows the trade-off directly: increasing rank improves the approximation, while the storage comparison must be checked separately.

From the Mathematical Matrix to a Displayed Image

The SVD reconstruction is a real matrix, but a display or image file may require integer pixel values in a fixed range. A common practical step is to round each reconstructed entry and clip values below \(0\) to \(0\), and values above the allowed maximum to that maximum. Rounding and clipping can change the matrix and its error; the exact truncated-SVD error formulas apply to \(\mathbf{A}_k\) before these extra operations. If exact quality comparisons matter, calculate the pixel differences after the final rounding and clipping as well.

Another practical issue is that storing factors directly may not itself create a smaller file: factor entries may need many bits, and overhead can matter. The count \(k(m+n+1)\) answers a narrower, precise question—how many real values the two representations contain under the same counting convention. The error and storage measures together help select \(k\): compare the discarded singular-value energy with the number of values saved, rather than assuming that a lower rank is always a better compression choice.

Common Pitfall: Mixing Up Error Entries

When calculating \(\mathbf{A}-\mathbf{A}_k\), subtract each matrix entry from the entry in the same position, then count symmetric off-diagonal errors twice when summing squares over the whole matrix. In the \(4\times4\) example, the error at position \(12\) is \(17/4-5=-3/4\), not \(1/4\); the error at position \(14\) is \(11/4-3=-1/4\), not \(-5/4\). Using an incorrect error table can produce a plausible-looking but false norm. A reliable check is to form the full difference matrix and compare its squared Frobenius norm with the sum of the discarded squared singular values. Here both calculations give \(5\).

Check Your Understanding

Use the singular values and storage convention stated in each question; distinguish the factor count from the approximation error.

  1. An image matrix is \(300\times500\). How many real values are stored by its rank-\(40\) factor representation, and how many entries are in the full matrix?
  2. A matrix has singular values \(9,4,3,1\). What is the Frobenius error after retaining the first two triplets?
  3. For the matrix in the preceding question, what is its operator-norm error after retaining the first two triplets?
  4. Explain why keeping more singular triplets cannot increase the truncated-SVD Frobenius error.
  5. Why can the mathematical error formula differ from the displayed-image error after rounding and clipping?

Test yourself: Linear Algebra final exam practice

Ready to check what you know? Work a full-length practice final, then open the complete worked solutions.

Want all 1,114 practice problems?

This tutorial covers one topic. Calc Pass covers the full Calculus AB & BC curriculum (the core of Calculus I and II): full review, every practice question, and step-by-step solutions to each one.

QR code to download the Calc Pass app

Get the Calc Pass App

Scan to download and practice on the go, with instant scoring.

Calc Pass Volume 1: 1,114 Practice Questions book cover

Vol. 1: 1,114 Questions

The complete practice question bank, organized by AB & BC topic.

View on Amazon
Calc Pass Volume 2: Solutions Manual book cover

Vol. 2: Solutions Manual

Fully worked solutions to every question in Volume 1.

View on Amazon