This documentation covers two Python modules: error-metrics.py and reconstruction.py. Both modules provide essential tools for image analysis and restoration. Read on for detailed explanations, workflows, and usage.
This module provides utilities for quantitatively comparing two images. It computes standard error metrics commonly used in image processing and computer vision.
- Image loading & conversion to grayscale
- Calculation of image error metrics:
- Mean Squared Error (MSE)
- Root Mean Squared Error (RMSE)
- Peak Signal-to-Noise Ratio (PSNR)
- Structural Similarity Index (SSIM)
- Automatic resizing for metric compatibility
- Command-line interface for quick metric evaluation
| Function | Description |
|---|---|
load_img |
Loads an image, converts to grayscale, normalizes to float [0,1] |
calc_mse |
Computes the Mean Squared Error between two images |
calc_rmse |
Computes RMSE between two images |
calc_psnr |
Computes the PSNR between two images |
eval_ssim |
Computes the SSIM between two images |
run_img_comp_metrics |
Loads two images and prints all error metrics |
- Loads a grayscale, float-precision image from disk.
- Handles RGB and grayscale files.
- Prints shape and dtype info.
def load_img(img_path):
...- Computes pixel-wise Mean Squared Error.
- Resizes images if shapes differ.
def calc_mse(img1, img2):
...- Calculates RMSE using the MSE function.
def calc_rmse(img_a, img_b):
...- Calculates the Peak Signal-to-Noise Ratio in dB.
- Handles identical images as a special case.
def calc_psnr(img_one, img_two):
...- Uses the SSIM metric from scikit-image.
- Resizes if dimensions do not match.
def eval_ssim(img_src, img_tgt):
...- Reads two images and prints all metrics.
To use this module:
- Set the paths in
orig_img_locandcomp_img_locinsiderun_img_comp_metrics(). - Run the script.
Example Output:
--- Image Comparison Results ---
MSE ('original.png' vs 'compressed.png'): 0.0032
RMSE: 0.0566
PSNR: 32.12 dB
SSIM: 0.92
-----------------------------------
| Metric | Description | Output Range |
|---|---|---|
| MSE | Mean squared pixel error | 0 (identical) + |
| RMSE | Square root of MSE | 0 (identical) + |
| PSNR | Ratio of max signal to noise (in dB) | 0 - inf; higher=better |
| SSIM | Structural similarity (perceptual) | -1 to 1; 1=identical |
This module provides a pipeline for image restoration and enhancement. It applies several advanced techniques to denoise, deblur, sharpen, and upscale grayscale images.
- Image loading & normalization
- Fourier-based denoising
- Radon streak removal (sinogram smoothing)
- Unsharp masking for sharpening
- Upscaling + Wiener deconvolution to enhance resolution
- Batch pipeline for automated enhancement
| Function | Description |
|---|---|
load_image |
Load and normalize image |
denoise_fourier_transform |
Denoise using a low-pass mask in the frequency domain |
remove_radon_streaks |
Remove streak artifacts using Radon transform and median filtering |
sharpen_image_unsharp |
Enhance edges via unsharp masking |
upscale_and_deconvolve_image |
Upscale and restore sharpness via Wiener deconvolution |
run_image_enhancement_pipeline |
Orchestrates the full pipeline; saves result |
flowchart TD
A[Input image file] --> B[load_image]
B --> C[denoise_fourier_transform]
C --> D[remove_radon_streaks]
D --> E[sharpen_image_unsharp]
E --> F[upscale_and_deconvolve_image]
F --> G[Save enhanced result]
- Reads an image, converts to grayscale, and normalizes to [0,1].
def load_image(file_path):
...- Applies a low-pass circular mask in the frequency domain to suppress high-frequency noise.
- Uses FFT for fast computation.
def denoise_fourier_transform(input_img):
...- Computes the Radon transform (sinogram).
- Median-filters the sinogram to suppress streak artifacts.
- Reconstructs the image using the inverse Radon transform.
def remove_radon_streaks(input_img):
...- Enhances edges using unsharp masking (subtracts a Gaussian-blurred version).
- Increases detail contrast.
def sharpen_image_unsharp(original_img):
...- Upscales the image using interpolation.
- Simulates a point spread function (PSF) and applies Wiener deconvolution to restore detail.
def upscale_and_deconvolve_image(low_res_img, scale_factor=2):
...- Executes the above steps in sequence.
- Saves the enhanced output image.
- Set
in_fileto your input image path inrun_image_enhancement_pipeline(). - Optionally, adjust
out_filefor the output location. - Run the script.
| Stage | Purpose | Techniques Used |
|---|---|---|
| Denoising | Reduce high-frequency noise | Fourier transform, masking |
| Radon streak removal | Remove linear artifacts, stripes | Radon transform, median filter |
| Sharpening | Enhance edges | Unsharp masking, Gaussian blur |
| Upscale & Deconvolution | Restore lost detail after upscaling | Wiener filter, PSF modeling |
sequenceDiagram
participant User
participant Pipeline as EnhancementPipeline
participant Disk
User->>Pipeline: Start with image file path
Pipeline->>Disk: Load image
Pipeline->>Pipeline: Denoise (Fourier mask)
Pipeline->>Pipeline: Remove Radon streaks
Pipeline->>Pipeline: Sharpen (Unsharp mask)
Pipeline->>Pipeline: Upscale & Deconvolve
Pipeline->>Disk: Save enhanced image