144 lines
4.6 KiB
Python
144 lines
4.6 KiB
Python
"""
|
|
This module contains functions for computing perceptual and HVS-based (Human Visual System)
|
|
IQ metrics: VSNR (Visual Signal-to-Noise Ratio), PIQE (Perceptual Image Quality Evaluator),
|
|
UQI (Universal Quality Index), and PSM (Perceptual Similarity Metric).
|
|
|
|
These metrics evaluate the similarity between reference and distorted images
|
|
by considering perceptual factors and human visual characteristics.
|
|
|
|
Functions:
|
|
- compute_all: Computes all perceptual and HVS-based image quality metrics.
|
|
- load_image: Loads an image and converts it to floating-point format.
|
|
- vsnr: Computes the Visual Signal-to-Noise Ratio (VSNR).
|
|
- piqe: Computes the Perceptual Image Quality Evaluator (PIQE).
|
|
- uqi: Computes the Universal Quality Index (UQI).
|
|
- psm: Computes the Perceptual Similarity Metric (PSM).
|
|
"""
|
|
|
|
from time import time as now
|
|
import numpy as np
|
|
from skimage import io, img_as_float
|
|
from skimage.filters import sobel # pylint: disable=E0611
|
|
|
|
|
|
def compute_all(ref_path, dist_path):
|
|
"""
|
|
Computes multiple perceptual and HVS-based image quality metrics.
|
|
"""
|
|
ref_img = load_image(ref_path)
|
|
dist_img = load_image(dist_path)
|
|
|
|
output = {}
|
|
|
|
metrics = [
|
|
("VSNR", lambda: vsnr(ref_img, dist_img)),
|
|
("PIQE", lambda: piqe(ref_img, dist_img)),
|
|
("UQI", lambda: uqi(ref_img, dist_img)),
|
|
("PSM", lambda: psm(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
|
|
|
|
|
|
# VSNR (Visual Signal-to-Noise Ratio)
|
|
def vsnr(ref_img, dist_img):
|
|
"""
|
|
Computes the Visual Signal-to-Noise Ratio (VSNR) by comparing the signal power
|
|
and noise power between the reference and distorted images.
|
|
"""
|
|
signal_power = np.mean(ref_img ** 2)
|
|
noise_power = np.mean((ref_img - dist_img) ** 2)
|
|
|
|
# Perceptual component: Use a simple threshold
|
|
perceptual_threshold = 0.01
|
|
vsnr_value = 10 * np.log10(signal_power / (noise_power + perceptual_threshold))
|
|
return vsnr_value
|
|
|
|
|
|
# PIQE (Perceptual Image Quality Evaluator)
|
|
def piqe(ref_img, dist_img):
|
|
"""
|
|
Computes the Perceptual Image Quality Evaluator (PIQE) by calculating
|
|
the mean absolute difference as a proxy for perceptual distortion.
|
|
"""
|
|
# PIQE typically measures distortions directly in images, but here we'll simulate it
|
|
# by using the Mean Absolute Difference (MAD) as a perceptual proxy
|
|
mad_value = np.mean(np.abs(ref_img - dist_img))
|
|
|
|
# PIQE value is inversely proportional to the perceptual distortion
|
|
piqe_value = 100 / (1 + mad_value)
|
|
return piqe_value
|
|
|
|
|
|
# UQI (Universal Quality Index)
|
|
def uqi(ref_img, dist_img):
|
|
"""
|
|
Computes the Universal Quality Index (UQI) by comparing
|
|
statistical features between the reference and distorted images.
|
|
"""
|
|
mean_ref = np.mean(ref_img)
|
|
mean_dist = np.mean(dist_img)
|
|
|
|
var_ref = np.var(ref_img)
|
|
var_dist = np.var(dist_img)
|
|
|
|
covariance = np.mean((ref_img - mean_ref) * (dist_img - mean_dist))
|
|
|
|
# UQI formula
|
|
numerator = 4 * covariance * mean_ref * mean_dist
|
|
denominator = (var_ref + var_dist) * (mean_ref ** 2 + mean_dist ** 2)
|
|
|
|
uqi_value = numerator / denominator
|
|
return uqi_value
|
|
|
|
|
|
# PSM (Perceptual Similarity Metric)
|
|
def psm(ref_img, dist_img):
|
|
"""
|
|
Computes the Perceptual Similarity Metric (PSM) by comparing the edge similarity
|
|
between the reference and distorted images.
|
|
"""
|
|
# Edge similarity based perceptual metric
|
|
ref_edges = sobel(ref_img)
|
|
dist_edges = sobel(dist_img)
|
|
|
|
# Compute similarity between edge maps
|
|
edge_similarity = np.mean(1 - np.abs(ref_edges - dist_edges))
|
|
|
|
# PSM value based on edge similarity
|
|
psm_value = edge_similarity * 100 # Scale to 0-100
|
|
return psm_value
|
|
|
|
|
|
if __name__ == "__main__":
|
|
# Tested:
|
|
# VSNR (Visual Signal-to-Noise Ratio)
|
|
# PIQE (Perceptual Image Quality Evaluator)
|
|
# UQI (Universal Quality Index)
|
|
# PSM (Perceptual Similarity Metric)
|
|
|
|
# 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 perceptual-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)")
|