0% found this document useful (0 votes)
0 views8 pages

Image Compressison

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
0 views8 pages

Image Compressison

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

Python Code:

First we take any image , then convert it into


grayscale, and get Image Matrix
Getting the Image Matrix:
# Load an example grayscale image
image_path = "path_to_your_image.jpg"
#Replace with your image path
image = io.imread(image_path)
if image.ndim == 3:
# Convert to grayscale if the image is RGB
image = color.rgb2gray(image)

Calculating the SVD of the Image Matrix


# Perform Singular Value Decomposition
U, S, Vt = np.linalg.svd(image_matrix,
full_matrices=False)

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).

Singular Value Matrix:


Will be 1 D array of length min(m,n)

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.

Keeping only the top Singular Values and their


corresponding singular vectors (left Uk and right VTk):

Dimensions: Uk will be m×k, where m is the number of


rows in the original matrix M

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

# Load an example grayscale image


image_path = "path_to_your_image.jpg"
#Replace with your image path
image = io.imread(image_path)
if image.ndim == 3:
# Convert to grayscale if the image is RGB
image = color.rgb2gray(image)
# Normalize image values (optional)
image = image / 255.0

# Set the rank for approximation


k = 50 # Example rank value, adjust as needed

# Compute the k-rank approximation


approximated_image =
compute_k_rank_approximation(image, k)

# Plot the original and approximated images


plt.figure(figsize=(10, 5))

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()

It is natural to interpret as approximating the raw


data A in terms of k

You might also like