Files

159 lines
5.0 KiB
Python

"""
This module contains functions for computing Peak Signal-to-Noise Ratio (PSNR)-based
image quality metrics, such as standard PSNR, weighted PSNR, PSNR-HVS, PSNR-HVS-M,
and logarithmic PSNR.
These metrics evaluate the similarity between a reference and a distorted image
by analyzing pixel intensity differences.
Functions:
- compute_all: Computes multiple PSNR-based image quality metrics.
- load_image: Loads an image and converts it to the specified color scheme.
- psnr: Computes the standard Peak Signal-to-Noise Ratio (PSNR).
- w_psnr: Computes the Weighted PSNR (W-PSNR).
- psnr_hvs: Computes PSNR based on Human Visual System (HVS) properties.
- psnr_hvs_m: Computes PSNR with HVS modifications (PSNR-HVS-M).
- log_psnr: Computes logarithmic PSNR (Log-PSNR).
"""
from time import time as now
import numpy as np
from skimage import io, img_as_float
from skimage.metrics import peak_signal_noise_ratio # pylint: disable=E0611
from skimage.color import rgb2gray, rgb2yuv # pylint: disable=E0611
def compute_all(ref_path, dist_path):
"""
Computes multiple PSNR-based image quality metrics.
"""
ref_img_rgb = load_image(ref_path, 'rgb')
dist_img_rgb = load_image(dist_path, 'rgb')
ref_img_yuv = load_image(ref_path, 'yuv')
dist_img_yuv = load_image(dist_path, 'yuv')
output = {}
metrics = [
("cPSNR", lambda: cpsnr(ref_img_rgb, dist_img_rgb)),
("W-PSNR", lambda: w_psnr(ref_img_rgb, dist_img_rgb)),
("PSNR-HVS", lambda: psnr_hvs(ref_img_yuv, dist_img_yuv)),
("PSNR-HVS-M", lambda: psnr_hvs_m(ref_img_yuv, dist_img_yuv)),
("Log-PSNR", lambda: log_psnr(ref_img_rgb, dist_img_rgb)),
]
for name, func in metrics:
start_time = now()
output[name] = (func(), now() - start_time)
return output
def load_image(path, scheme):
"""
Loads an image and converts it to the specified color scheme.
Parameters:
path (str): Path to the image file.
scheme (str): The color scheme to convert to ('rgb', 'gray', 'yuv').
Returns:
np.ndarray: The loaded image in the specified format.
"""
img = io.imread(path)
img = img_as_float(img)
if scheme == 'gray':
img = rgb2gray(img)
elif scheme == 'yuv':
img = rgb2yuv(img)
return img
def cpsnr(ref_img, dist_img):
"""
Computes the standard Peak Signal-to-Noise Ratio (PSNR).
"""
psnr_r = peak_signal_noise_ratio(ref_img[:, :, 0], dist_img[:, :, 0])
psnr_g = peak_signal_noise_ratio(ref_img[:, :, 1], dist_img[:, :, 1])
psnr_b = peak_signal_noise_ratio(ref_img[:, :, 2], dist_img[:, :, 2])
return (psnr_r + psnr_g + psnr_b) / 3
def w_psnr(ref_img, dist_img):
"""
Computes the Weighted PSNR (W-PSNR).
"""
height, width, _ = ref_img.shape
weight_matrix = np.ones((height, width))
weight_matrix[height // 4:3 * height // 4, width // 4:3 * width // 4] = 2.0
mse_r = np.mean(weight_matrix * (ref_img[:, :, 0] - dist_img[:, :, 0]) ** 2)
mse_g = np.mean(weight_matrix * (ref_img[:, :, 1] - dist_img[:, :, 1]) ** 2)
mse_b = np.mean(weight_matrix * (ref_img[:, :, 2] - dist_img[:, :, 2]) ** 2)
if mse_r == 0 or mse_g == 0 or mse_b == 0:
return np.inf
psnr_weighted_r = 10 * np.log10(1 / mse_r)
psnr_weighted_g = 10 * np.log10(1 / mse_g)
psnr_weighted_b = 10 * np.log10(1 / mse_b)
return (psnr_weighted_r + psnr_weighted_g + psnr_weighted_b) / 3
def psnr_hvs(ref_img, dist_img):
"""
Computes PSNR based on Human Visual System (HVS) properties.
"""
contrast_weight = np.ones(ref_img[:, :, 0].shape) * 0.9
contrast_weight[ref_img[:, :, 0] > 0.5] = 0.8
mse = np.mean(contrast_weight * (ref_img[:, :, 0] - dist_img[:, :, 0]) ** 2)
if mse == 0:
return np.inf
return 10 * np.log10(1 / mse)
def psnr_hvs_m(ref_img, dist_img):
"""
Computes PSNR with Human Visual System modifications (PSNR-HVS-M).
"""
mask_weight = np.ones(ref_img.shape) * 0.7
mask_weight[ref_img > 0.7] = 0.6
mse = np.mean(mask_weight * (ref_img - dist_img) ** 2)
if mse == 0:
return np.inf
return 10 * np.log10(1 / mse)
def log_psnr(ref_img, dist_img):
"""
Computes logarithmic PSNR (Log-PSNR).
"""
psnr_score = cpsnr(ref_img, dist_img)
return np.log1p(psnr_score)
if __name__ == "__main__":
# Tested:
# cPSNR (color Peak Signal-to-Noise Ratio)
# W-PSNR (Weighted Peak Signal-to-Noise Ratio)
# PSNR-HVS (Human Visual System-based PSNR)
# PSNR-HVS-M (Modified Human Visual System PSNR)
# Log-PSNR (Logarithmic cPSNR)
# 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 pixel-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)")