Initial commit: new baseline
This commit is contained in:
@@ -0,0 +1,120 @@
|
||||
"""
|
||||
This module contains functions for computing pixel-based image quality metrics,
|
||||
such as MSE (Mean Squared Error), RMSE (Root Mean Squared Error), PSNR (Peak Signal-to-Noise Ratio),
|
||||
PSNR-B (Block Sensitive PSNR), SNR (Signal-to-Noise Ratio), and MAE (Mean Absolute Error).
|
||||
|
||||
These metrics evaluate the similarity between reference and distorted images
|
||||
by directly comparing pixel values.
|
||||
|
||||
Functions:
|
||||
- compute_all: Computes all pixel-based image quality metrics.
|
||||
- load_image: Loads an image and converts it to floating-point format.
|
||||
- mse: Computes the Mean Squared Error (MSE).
|
||||
- psnr: Computes the Peak Signal-to-Noise Ratio (PSNR).
|
||||
- snr: Computes the Signal-to-Noise Ratio (SNR).
|
||||
- mae: Computes the Mean Absolute Error (MAE).
|
||||
"""
|
||||
|
||||
from time import time as now
|
||||
|
||||
import numpy as np
|
||||
from skimage import io, img_as_float
|
||||
from sewar import rmse, psnrb
|
||||
|
||||
|
||||
def compute_all(ref_path, dist_path):
|
||||
"""
|
||||
Computes multiple pixel-based image quality metrics.
|
||||
"""
|
||||
ref_img = load_image(ref_path)
|
||||
dist_img = load_image(dist_path)
|
||||
|
||||
output = {}
|
||||
|
||||
metrics = [
|
||||
("MSE", lambda: mse(ref_img, dist_img)),
|
||||
("RMSE", lambda: rmse(ref_img, dist_img)),
|
||||
("PSNR", lambda: psnr(ref_img, dist_img)),
|
||||
("PSNR-B", lambda: psnrb(ref_img, dist_img)),
|
||||
("SNR", lambda: snr(ref_img, dist_img)),
|
||||
("MAE", lambda: mae(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
|
||||
|
||||
|
||||
# Mean Squared Error (MSE)
|
||||
def mse(ref_img, dist_img):
|
||||
"""
|
||||
Computes the Mean Squared Error (MSE) between the reference and distorted images.
|
||||
"""
|
||||
return np.mean((ref_img - dist_img) ** 2)
|
||||
|
||||
|
||||
# Peak Signal-to-Noise Ratio (PSNR)
|
||||
def psnr(ref_img, dist_img):
|
||||
"""
|
||||
Computes the Peak Signal-to-Noise Ratio (PSNR) by comparing the mean squared error
|
||||
between the reference and distorted images.
|
||||
"""
|
||||
mse_value = mse(ref_img, dist_img)
|
||||
if mse_value == 0:
|
||||
return float('inf') # Perfect match, no error
|
||||
max_pixel_value = 1.0 # Assuming the images are normalized to [0, 1]
|
||||
return 20 * np.log10(max_pixel_value / np.sqrt(mse_value))
|
||||
|
||||
|
||||
# Signal-to-Noise Ratio (SNR)
|
||||
def snr(ref_img, dist_img):
|
||||
"""
|
||||
Computes the Signal-to-Noise Ratio (SNR) 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)
|
||||
if noise_power == 0:
|
||||
return float('inf') # Perfect match, no noise
|
||||
return 10 * np.log10(signal_power / noise_power)
|
||||
|
||||
|
||||
# Mean Absolute Error (MAE)
|
||||
def mae(ref_img, dist_img):
|
||||
"""
|
||||
Computes the Mean Absolute Error (MAE) between the reference and distorted images.
|
||||
"""
|
||||
return np.mean(np.abs(ref_img - dist_img))
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
# Tested:
|
||||
# MSE (Mean Squared Error)
|
||||
# RMSE (Root Mean Squared Error)
|
||||
# PSNR (Peak Signal-to-Noise Ratio)
|
||||
# PSNR-B (Block Sensitive - Peak Signal-to-Noise Ratio)
|
||||
# SNR (Signal-to-Noise Ratio)
|
||||
# MAE (Mean Absolute Error)
|
||||
|
||||
# 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)")
|
||||
Reference in New Issue
Block a user