Image Compressison
Image Compressison
full_matrices=False
This tells the SVD function to compute a reduced form of
the decomposition.
S will have min(m,n) and will be 1D array containing
singular values of M
Original Matrix:
U Matrix
U will have dimensions m×min(m,n) (not m×m).
VT Matrix:
VT will have dimensions min(m,n)×n(not n×n).
The rank of the matrix M is typically much smaller than
min(m,n).We only care about the non-zero singular values
and their corresponding singular vectors.
The reduced form captures the exact same information as
the full form, but without the unnecessary zero components.
import numpy as np
import matplotlib.pyplot as plt
from skimage import io, color
def compute_k_rank_approximation(image_matrix, k):
# Perform Singular Value Decomposition
U, S, Vt = np.linalg.svd(image_matrix,
full_matrices=False)
# Keep only the first k components
U_k = U[:, :k]
S_k = np.diag(S[:k])
Vt_k = Vt[:k, :]
# Reconstruct the k-rank approximation
M_k = np.dot(U_k, np.dot(S_k, Vt_k))
return M_k
plt.subplot(1, 2, 1)
plt.title("Original Image")
plt.imshow(image, cmap="gray")
plt.axis("off")
plt.subplot(1, 2, 2)
plt.title(f"k-Rank Approximation (k={k})")
plt.imshow(approximated_image, cmap="gray")
plt.axis("off")
plt.tight_layout()
plt.show()