SVD for Image Compression
Singular Value Decomposition (SVD) is a powerful matrix factorization technique with numerous applications, including dimensionality reduction, noise reduction, and data compression. Any real matrix can be decomposed into , where is an orthogonal matrix, is an diagonal matrix containing singular values, and is an orthogonal matrix.
By keeping only the top largest singular values and their corresponding singular vectors, we can obtain a low-rank approximation of the original matrix . This approximation can be used for compression.
Your task is to implement a function that performs SVD-based image compression.
Implementation Details:
1. Load Image: Load a grayscale image (e.g., using PIL or matplotlib.image). Convert it to a NumPy array.
2. SVD: Apply np.linalg.svd to the image matrix to obtain , (as a 1D array of singular values), and .
3. Truncation: For a given number of components k, reconstruct the image using only the first k singular values and vectors: .
4. Compression Ratio: Calculate the compression ratio (original size / compressed size) and the percentage of variance explained by the chosen k components.
* The original image has elements.
* The compressed image (using components) requires storing (), ( or just values), and (). The number of elements to store is approximately .
Implement a function compress_image_svd(image_path, k_components) that:
* Loads the image.
* Performs SVD.
* Reconstructs the image with k_components.
* Returns the original image, compressed image, and the compression ratio.
Verification:
1. Choose a sample grayscale image (you can find one online or create a simple one programmatically).
2. Test your function with different values of k (e.g., 10, 50, 100).
3. Display the original and compressed images side-by-side using matplotlib.pyplot.imshow. Observe how the image quality changes with k.
4. Verify that your compression ratio calculation is correct.
5. (Optional, advanced) Compare the reconstruction error (e.g., Frobenius norm of the difference between original and reconstructed) for different k values.