This project provides tools for image compression using different variants of Singular Value Decomposition (SVD):
- Truncated SVD (TSVD)
- Compressed SVD
- Randomized SVD (RSVD), with optional power iterations to improve accuracy.
The code supports both grayscale and colored image compression while measuring computational time, Mean Squared Error (MSE), and compression ratios for different numbers of singular values.
- Setup and Installation
- Dependencies
- Code Overview
- How to Use
- Grayscale Image Compression
- Colored Image Compression
- Results and Outputs
- Evaluation Metrics
Clone the repository and install required Python libraries:
# Clone the repository
git clone https://github.com/your-repository/svd-compression.git
cd svd-compression
# Install dependencies
pip install -r requirements.txtThe following libraries are required for the project:
numpy
matplotlib
pandas
opencv-python
PillowInstall them with:
pip install numpy matplotlib pandas opencv-python PillowThe project consists of:
This module implements different variants of SVD:
trunc_svd: Truncated SVD for grayscale imagescompressed_svd: Compressed SVD for grayscale imagesrandomized_svd: Randomized SVD with power iteration for grayscale imagestrunc_svd_colored: Truncated SVD for colored imagescompressed_svd_colored: Compressed SVD for colored imagesrandomized_svd_colored: Randomized SVD with power iteration for colored images
This module provides tools to evaluate the compression results:
measure_computational_time: Measure execution time of compression algorithmsmse_frobenius: Compute the Mean Squared Error (MSE) for grayscale imagesmse_frobenius_colored: Compute MSE for colored imagescalculate_compression_ratio: Calculate the compression ratiosave_compressed_image: Save the compressed image to file
The main script performs compression experiments for both grayscale and colored images, evaluates performance, and visualizes results.
The script compresses a grayscale image using Truncated SVD, Compressed SVD, and Randomized SVD (with varying power iterations). Results include:
- Computational time
- MSE error
- Compression ratio
- Place your grayscale image in the working directory (e.g.,
alone.jpg). - Run the main script:
python main.py- Outputs:
- Compressed images saved under
image_results/ - Performance metrics saved under
evaluation_results/ - Plots generated for time taken, MSE, and compression ratios
- Compressed images saved under
from PIL import Image
import matplotlib.pyplot as plt
import os
import numpy as np
import pandas as pd
import svd
evaluation
image = Image.open("alone.jpg")
gray_image = image.convert('L')
image_matrix = np.array(gray_image)/255
singular_values = [1, 5, 10, 50, 100, 200, 300, 400, 500]
ts_results = []
for k in singular_values:
comp_time, comp_image = evaluation.measure_computational_time(svd.trunc_svd, image_matrix, k)
mse = evaluation.mse_frobenius(image_matrix, comp_image)
print(f"k={k}, Time={comp_time}, MSE={mse}")The script compresses colored images using Truncated SVD, Compressed SVD, and Randomized SVD with power iterations.
- Place your colored image in the working directory (e.g.,
alone.jpg). - Run the main script:
python main.py- Outputs:
- Compressed images saved under
image_results/ - Evaluation results stored under
evaluation_results/ - Plots for computational time, MSE, and compression ratio
- Compressed images saved under
image_bgr = cv2.imread('alone.jpg')
image = cv2.cvtColor(image_bgr, cv2.COLOR_BGR2RGB) / 255.0
singular_values = [1, 5, 10, 50, 100, 200]
ts_results = []
for k in singular_values:
comp_time, comp_image = evaluation.measure_computational_time(svd.trunc_svd_colored, image, k)
mse = evaluation.mse_frobenius_colored(image, comp_image)
print(f"k={k}, Time={comp_time}, MSE={mse}")- Compressed Images: Stored in the
image_results/directory - Evaluation Metrics: Saved as CSV or plots in
evaluation_results/ - Plots: Performance comparisons:
- Computational Time vs Singular Values
- MSE Error vs Singular Values
- Compression Ratio vs Singular Values
- Computational Time
- MSE (Mean Squared Error)
- Compression Ratio
- Computational Time: Measures how long it takes to perform the compression for different SVD variants.
- MSE (Mean Squared Error): Measures reconstruction error between the original and compressed images.
- Compression Ratio: Compression efficiency based on the size of the original and compressed images.
This is the start of the truncated svd
Running the truncated svd for 50 singular value
Time=0.123s, MSE=0.0023, Compression Ratio=0.25
This is the start of the randomized svd with q=2
Running for k=50 singular value
Time=0.089s, MSE=0.0021, Compression Ratio=0.22
project/
|-- image_results/
| |-- gray_alone.jpg
| |-- tsvd_50.jpg
| |-- rsvd_q=2_50.jpg
|-- evaluation_results/
| |-- time_taken_plot.png
| |-- mse_plot.png
| |-- compression_ratio_plot.png
Desmond Kofi Boateng
This project is licensed under the MIT License. Feel free to use and modify it!