Mathematical implementation, algorithmic benchmarking, and interactive GUI application for block-based image compression using the 2D Discrete Cosine Transform (DCT-II). Developed for the Scientific Computing Methods (Metodi del Calcolo Scientifico) Master's course at Università degli Studi di Milano - Bicocca.
- Project Overview
- Mathematical Foundation
- Software Components
- Quick Start
- Repository Structure
- Course Report & Authors
The Discrete Cosine Transform (DCT-II) is the fundamental mathematical transform powering modern lossy image and video compression standards (including JPEG and MPEG). Because natural images exhibit strong spatial correlation among neighboring pixels, transforming pixel blocks into the spatial frequency domain concentrates energy into a small number of low-frequency coefficients. High-frequency spatial variations (to which human vision is less sensitive) can then be discarded, achieving substantial data reduction with minimal perceptible degradation.
This repository features:
-
Mathematical implementation from scratch: Constructing orthonormal DCT matrices and evaluating tensor contractions (
$C = D f D^T$ ). -
Fast transform optimization: Leveraging
$O(N \log N)$ FFT-based algorithms viascipy.fftpack. -
Interactive Desktop GUI: A
CustomTkinterdesktop application enabling real-time experimentation with block size ($F$ ) and frequency cutoff threshold ($d$ ). - Computational benchmarks: Measuring runtime scaling across block dimensions.
The orthonormal DCT-II transformation matrix
where the normalization factor
Given a 1D vector
Because the 2D DCT-II kernel is separable, transforming an
- The input image is divided into disjoint
$F \times F$ pixel blocks (typically$F \in {8, 16, 32, 64}$ ). - For each block
$f$ , the 2D frequency matrix$C = \text{DCT2}(f)$ is computed. - Frequency cutoff criterion: Coefficients
$C_{k, l}$ satisfying$(k + l) \ge d$ are zeroed out (where$d \in [0, 2F - 2]$ ):$$C_{k, l} \leftarrow 0 \quad \text{if } k + l \ge d$$ - Reconstruct each block via inverse transform
$\hat{f} = \text{IDCT2}(C)$ and assemble the compressed image.
Built using modern CustomTkinter widgets:
- Load any standard
.bmpimage from disk. - Interactively adjust block size
$F$ and frequency cutoff threshold$d$ using dynamic sliders. - Instantaneous side-by-side visual comparison between original and reconstructed images.
- Real-time calculation of compression statistics (discarded coefficient percentage).
python3 app.py-
dct.py: Contains both naive$O(N^3)$ matrix-multiplication DCT (custom_dct2) and fast$O(N^2 \log N)$ FFT-based implementations (fast_dct2,fast_idct2). -
compressor.py: Modular block partitioner, vectorizer, and frequency thresholding pipeline.
Compares wall-clock execution times of custom matrix multiplication against optimized scipy.fftpack across variable matrix sizes (
python3 benchmark.py- Python 3.10 or higher
git clone https://github.com/mbroglio/dct-image-compression.git
cd dct-image-compression
# Create virtual environment
python3 -m venv venv
source venv/bin/activate
# Install dependencies
pip install -r requirements.txtpython3 app.py├── Assignment2_Report.pdf # Full academic report (PDF)
├── README.md # Project documentation (English)
├── requirements.txt # Python dependencies
├── app.py # CustomTkinter graphical user interface
├── compressor.py # Block-based image compression core
├── dct.py # 1D/2D DCT-II and IDCT-II algorithms
├── benchmark.py # Runtime benchmark comparing naive vs. fast DCT
├── test_matrices.py # Unit tests for DCT matrix orthonormality
├── validate_compression.py # Validation and numerical verification script
└── images/ # Sample test benchmark images (.bmp)
├── cathedral.bmp
├── deer.bmp
├── bridge.bmp
├── shoe.bmp
└── ...
Project developed for the Scientific Computing Methods (Metodi del Calcolo Scientifico) course (Academic Year 2025/2026), Master's Degree in Computer Science, Università degli Studi di Milano - Bicocca:
- Matteo Broglio - Matricola
899562 - Lorenzo Caputo - Matricola
894528 - Daniel Giuggioli - Matricola
894415
Full report available in Assignment2_Report.pdf.