Files
iqa-study/metrics/objective/full_reference/frequency.py
T

128 lines
4.2 KiB
Python

"""
This module contains functions for computing frequency and spectral-based image quality metrics,
such as FIQ (Fourier-based Image Quality), SRSIM (Spectral Residual-based Similarity),
and several other metrics from the sewar library, including ERGAS, SCC, SAM, and D-lambda.
These metrics evaluate the similarity between images in the frequency or spectral domain.
Functions:
- compute_all: Computes all frequency and spectral-based image quality metrics.
- load_image: Loads an image and converts it to floating-point format.
- fiq: Computes the Fourier-based Image Quality (FIQ) metric.
- srsim: Computes the Spectral Residual-based Similarity (SRSIM).
"""
from time import time as now
import numpy as np
from skimage import io, img_as_float
from numpy.fft import fft2, fftshift
from sewar import ergas, scc, sam, d_lambda
def compute_all(ref_path, dist_path):
"""
Computes multiple frequency domain and spectral-based image quality metrics
between a reference and distorted image.
"""
ref_img = load_image(ref_path)
dist_img = load_image(dist_path)
output = {}
metrics = [
("FIQ", lambda: fiq(ref_img, dist_img)),
("SRSIM", lambda: srsim(ref_img, dist_img)),
("ERGAS", lambda: ergas(ref_img, dist_img)),
("SCC", lambda: scc(ref_img, dist_img)),
("SAM", lambda: sam(ref_img, dist_img)),
("D-lambda", lambda: d_lambda(ref_img, dist_img)),
]
for name, func in metrics:
start_time = now()
output[name] = (func(), now() - start_time)
return output
# Helper function to load images
def load_image(path):
"""
Loads an image from the given path and converts it to floating-point format.
"""
img = io.imread(path)
img = img_as_float(img)
return img
# FIQ (Fourier-based Image Quality Metric)
def fiq(ref_img, dist_img):
"""
Computes the Fourier-based Image Quality (FIQ) metric by comparing the magnitude spectra of
the reference and distorted images' Fourier transforms.
"""
# Compute the Fourier transforms of both images
ref_fft = fft2(ref_img)
dist_fft = fft2(dist_img)
# Shift zero frequency to the center
ref_fft_shift = fftshift(ref_fft)
dist_fft_shift = fftshift(dist_fft)
# Compute the magnitude spectrum
ref_magnitude = np.abs(ref_fft_shift)
dist_magnitude = np.abs(dist_fft_shift)
# Compute the similarity in the frequency domain
fiq_value = np.mean(np.abs(ref_magnitude - dist_magnitude))
return 1 / (1 + fiq_value) # Inverted for similarity
# SRSIM (Spectral Residual-based Similarity)
def srsim(ref_img, dist_img):
"""
Computes the Spectral Residual-based Similarity (SRSIM) metric by comparing the
spectral residuals of the reference and distorted images.
"""
# Compute the Fourier transform
ref_fft = fft2(ref_img)
dist_fft = fft2(dist_img)
# Compute the magnitude and log of the magnitude
ref_magnitude = np.abs(ref_fft)
dist_magnitude = np.abs(dist_fft)
ref_log_magnitude = np.log(ref_magnitude + 1e-10)
dist_log_magnitude = np.log(dist_magnitude + 1e-10)
# Compute the spectral residuals
ref_residual = ref_log_magnitude - np.mean(ref_log_magnitude)
dist_residual = dist_log_magnitude - np.mean(dist_log_magnitude)
# Compute the similarity between spectral residuals
srsim_value = np.mean(np.abs(ref_residual - dist_residual))
return 1 / (1 + srsim_value) # Inverted for similarity
if __name__ == "__main__":
# Tested:
# FIQ (Fourier-based Image Quality)
# SRSIM (Spectral Residual-based Similarity)
# ERGAS (Erreur Relative Globale Adimensionnelle de Synthèse)
# SCC (Spatial Correlation Coefficient)
# RASE (Relative Average Spectral Error)
# SAM (Spectral Angle Mapper)
# D-lambda (Spectral Distortion Index)
# Paths to reference and distorted images
REF_IMG_PATH = "test_reference/001.jpg"
DIST_IMG_PATH = "test_distorted/001_CodeFace_1.4.jpg"
# Compute all frequency-based metrics
results = compute_all(REF_IMG_PATH, DIST_IMG_PATH)
# Print results for each metric along with the time taken
for metric, (value, duration) in results.items():
print(f"{metric}: {value:.8g} (computed in {duration:.6f} seconds)")