Skip to content

MustafaUsman1/digital-image-processing-suite

Repository files navigation

Digital Image Processing Suite

Mathematical Algorithm Implementation from Scratch

Python NumPy License

A comprehensive implementation of fundamental image processing algorithms built from mathematical first principles. This project demonstrates deep understanding of computational mathematics by implementing spatial filters, edge detection, morphological operations, and frequency domain transforms without relying on high-level libraries like OpenCV or scipy.

🎯 Project Motivation

This project bridges the gap between theoretical mathematics and practical computational science by:

  • Translating mathematical equations directly into code
  • Implementing numerical methods for signal processing
  • Understanding discrete approximations of continuous operations
  • Optimizing matrix-based computations

Perfect for demonstrating computational science skills, algorithm analysis, and mathematical programming proficiency. Edge Detection Demo Frequency Domain Demo Morphological Operations Demo

📚 Implemented Algorithms

1. Spatial Domain Filtering

  • Gaussian Blur: Custom 2D Gaussian kernel generation using G(x,y) = (1/2πσ²)exp(-(x²+y²)/2σ²)
  • Laplacian Filter: Second-order derivative approximation ∇²f = ∂²f/∂x² + ∂²f/∂y²
  • Median Filter: Non-linear noise reduction through neighborhood sorting
  • 2D Convolution: Manual implementation of discrete convolution operation

2. Edge Detection

  • Sobel Operator: Gradient magnitude computation |G| = √(Gx² + Gy²)
  • Canny Edge Detector: Complete 4-stage pipeline
    • Gaussian smoothing
    • Gradient calculation
    • Non-maximum suppression
    • Hysteresis thresholding
  • Gradient Direction: Angular computation θ = arctan(Gy/Gx)

3. Morphological Operations

  • Dilation: Expansion using (A⊕B)(x,y) = max{A(x-i,y-j) + B(i,j)}
  • Erosion: Shrinking using (A⊖B)(x,y) = min{A(x+i,y+j) - B(i,j)}
  • Opening: Erosion → Dilation (noise removal)
  • Closing: Dilation → Erosion (gap filling)
  • Morphological Gradient: Edge detection via dilation - erosion
  • Structuring Elements: Square, cross, and circular kernel generation

4. Frequency Domain Processing

  • DFT (Discrete Fourier Transform): Direct implementation X[k] = Σx[n]e^(-i2πkn/N)
  • IDFT (Inverse DFT): Transform back to spatial domain
  • FFT (Fast Fourier Transform): Cooley-Tukey O(N log N) algorithm
  • 2D FFT: Separable approach (row-wise then column-wise)
  • Ideal Lowpass Filter: Sharp frequency cutoff
  • Gaussian Lowpass Filter: Smooth frequency attenuation

🚀 Installation & Usage

Prerequisites

pip install numpy matplotlib pillow

Basic Usage

from image_processing_suite import (
    SpatialFilters, EdgeDetection, 
    MorphologicalOperations, FrequencyDomain
)
import numpy as np

# Load your image
image = load_image('path/to/image.jpg')

# Apply Gaussian blur
blurred = SpatialFilters.gaussian_blur(image, kernel_size=5, sigma=1.0)

# Detect edges with Canny
edges = EdgeDetection.canny_edge_detection(image, low_threshold=50, high_threshold=150)

# Morphological operations
kernel = MorphologicalOperations.create_structuring_element('square', 5)
dilated = MorphologicalOperations.dilate(image, kernel)

# Frequency domain filtering
def lowpass(spectrum, cutoff):
    return FrequencyDomain.ideal_lowpass_filter(spectrum, cutoff)

filtered = FrequencyDomain.frequency_domain_filter(image, lowpass, 30)

Run Complete Demo

python demo_script.py

This generates comprehensive visualizations:

  • spatial_filters_demo.png - Gaussian, Laplacian, Median filters
  • edge_detection_demo.png - Sobel and Canny edge detection
  • morphological_ops_demo.png - Dilation, erosion, opening, closing
  • frequency_domain_demo.png - FFT, frequency filters, spectrum analysis

📊 Performance Analysis

Algorithm Complexity

Algorithm Time Complexity Space Complexity Notes
2D Convolution O(M×N×k²) O(M×N) k = kernel size
Sobel Operator O(M×N) O(M×N) Fixed 3×3 kernel
Canny Detection O(M×N) O(M×N) Multi-stage pipeline
DFT 2D O(M²N²) O(M×N) Direct computation
FFT 2D O(MN log MN) O(M×N) Divide & conquer
Morphology O(M×N×k²) O(M×N) k = structuring element size

Benchmarks (on 128×128 image)

Convolution (5×5):      45.23ms
Sobel Edge Detection:   23.15ms
Canny (complete):       89.47ms
DFT 2D:                 2847.32ms
FFT 2D:                 12.58ms (226× faster than DFT!)
Morphological Dilation: 38.91ms

🧮 Mathematical Foundations

Convolution Operation

(f * g)[m,n] = ΣΣ f[i,j] · g[m-i, n-j]

Fourier Transform Pair

Forward:  F(u,v) = ΣΣ f(x,y)e^(-i2π(ux/M + vy/N))
Inverse:  f(x,y) = (1/MN)ΣΣ F(u,v)e^(i2π(ux/M + vy/N))

Gaussian Function

G(x,y) = (1/(2πσ²)) · exp(-(x² + y²)/(2σ²))

Gradient Magnitude

|∇f| = √((∂f/∂x)² + (∂f/∂y)²)

🏗️ Project Structure

digital-image-processing/
│
├── image_processing_suite.py    # Core algorithm implementations (~800 LOC)
├── demo_script.py               # Comprehensive demonstrations
├── README.md                    # This file
├── requirements.txt             # Dependencies
│
├── examples/                    # Sample images and outputs
│   ├── spatial_filters_demo.png
│   ├── edge_detection_demo.png
│   ├── morphological_ops_demo.png
│   └── frequency_domain_demo.png
│
├── tests/                       # Unit tests
│   ├── test_spatial_filters.py
│   ├── test_edge_detection.py
│   ├── test_morphology.py
│   └── test_frequency_domain.py
│
└── notebooks/                   # Jupyter notebooks
    ├── tutorial_spatial_domain.ipynb
    ├── tutorial_edge_detection.ipynb
    └── tutorial_frequency_domain.ipynb

🎓 Educational Value

This project demonstrates:

Mathematical Translation: Converting continuous equations to discrete implementations
Numerical Methods: Implementing approximations of derivatives and integrals
Algorithm Optimization: Understanding computational complexity and efficiency
Matrix Operations: Efficient manipulation of 2D arrays and kernels
Signal Processing: Spatial and frequency domain transformations
Computer Vision Fundamentals: Edge detection, filtering, and morphology

Relevant to Computational Science Because:

  • Demonstrates ability to implement numerical algorithms from mathematical foundations
  • Shows understanding of discretization of continuous signals
  • Proves competency in matrix-based computations and optimization
  • Exhibits knowledge of Fourier analysis and frequency domain processing
  • Illustrates practical application of calculus and linear algebra

📖 Theory References

  1. Digital Image Processing - Gonzalez & Woods (Chapter 3: Spatial Filtering, Chapter 4: Frequency Domain)
  2. Computer Vision: Algorithms and Applications - Szeliski (Chapter 3: Image Processing)
  3. The Scientist and Engineer's Guide to Digital Signal Processing - Smith
  4. Numerical Recipes - Press et al. (FFT Implementation)

🔬 Future Enhancements

  • GPU acceleration using CuPy
  • Hough Transform for line/circle detection
  • Wavelet transforms for multi-resolution analysis
  • Image restoration algorithms (Wiener filter, Richardson-Lucy)
  • Feature extraction (SIFT, SURF concepts)
  • Benchmark against OpenCV implementations
  • Add comprehensive unit tests with coverage > 90%

📝 Code Quality

  • Lines of Code: ~800-1000 (excluding comments and tests)
  • Documentation: Full docstrings with mathematical equations
  • Type Hints: Comprehensive type annotations
  • Comments: Detailed explanations of mathematical operations
  • Testing: Unit tests for each algorithm module

🤝 Contributing

Contributions are welcome! Please:

  1. Fork the repository
  2. Create a feature branch (git checkout -b feature/new-algorithm)
  3. Implement with detailed documentation
  4. Add unit tests
  5. Submit a pull request

📄 License

MIT License - see LICENSE file for details.


🌟 Acknowledgments

  • Mathematical foundations from classical image processing texts
  • Algorithm implementations inspired by academic papers
  • Performance optimization techniques from numerical computing literature

📊 Repository Statistics

GitHub stars GitHub forks GitHub issues Code size


⭐ If you find this project helpful, please consider giving it a star!

Releases

No releases published

Packages

 
 
 

Contributors

Languages