svd image compression

1
2
3
from skimage import data, filter
import matplotlib.pyplot as plt
%matplotlib inline
1
2
image = data.camera()
plt.imshow(image);

png

1
image.shape
(512, 512)
1
2
from scipy import linalg
import numpy as np
1
2
U, s, Vh = linalg.svd(image)
assert(x > x2 for x, x2 in zip(s, s[1:]))

SVD can be used to compress image (“Introduction to Linear Algebra”, G. Strang, 4ed, p.365).

Before the image is 512×512 pixels.
After the image can be stored as Rank×512×512 pixels

When Rank is something like 50 there is essentially no big difference from original image.

1
2
3
4
5
R = 50
Sigma = np.diag(s[:R])
compressed_image = U[:, :R] @ Sigma @ Vh[:R, :]
plt.imshow(compressed_image)

png