Implement image compression using Truncated Singular Value Decomposition (SVD).
SVD decomposes an image matrix A (m×n) into: A=UΣVT
Where:
Truncated SVD keeps only the top k singular values, achieving compression: Ak=UkΣkVkT
The compression ratio is: k(m+n+1)m×n
Quality measure: The retained energy/variance is: energy=∑i=1rσi2∑i=1kσi2
where r is the rank of the original matrix.
image = [[100, 100, 100],
[100, 100, 100],
[100, 100, 100]]
k = 1{'compressed': [[100, 100, 100], [100, 100, 100], [100, 100, 100]], 'compression_ratio': 1.29}Step 1: SVD decomposition For a constant image, there's only 1 non-zero singular value. σ1=300 (sum of all values in the dominant direction)
Step 2: Truncated reconstruction with k=1 Using only the first singular value reconstructs the image exactly (since rank=1).
Step 3: Compression ratio Original: 3×3 = 9 values Compressed storage: k×(m + n + 1) = 1×(3+3+1) = 7 values Ratio = 9/7 ≈ 1.29
The compressed representation stores less data while preserving the image.