Initial commit: new baseline
This commit is contained in:
@@ -0,0 +1,121 @@
|
||||
"""
|
||||
This module contains functions for computing gradient and edge-based image quality metrics:
|
||||
GMSD (Gradient Magnitude Similarity Deviation), G-SSIM (Gradient-based Structural Similarity),
|
||||
and ESIM (Edge Similarity Index).
|
||||
|
||||
These metrics evaluate the similarity between images based on their gradients
|
||||
or edge information.
|
||||
|
||||
Functions:
|
||||
- compute_all: Computes all gradient and edge-based image quality metrics.
|
||||
- load_image: Loads an image and converts it to grayscale.
|
||||
- gmsd: Computes the Gradient Magnitude Similarity Deviation (GMSD).
|
||||
- g_ssim: Computes the Gradient-based Structural Similarity (G-SSIM).
|
||||
- esim: Computes the Edge Similarity Index (ESIM).
|
||||
"""
|
||||
|
||||
from time import time as now
|
||||
|
||||
import numpy as np
|
||||
from skimage import io, img_as_float
|
||||
from skimage.color import rgb2gray # pylint: disable=E0611
|
||||
from skimage.metrics import structural_similarity # pylint: disable=E0611
|
||||
from skimage.filters import sobel # pylint: disable=E0611
|
||||
|
||||
|
||||
def compute_all(ref_path, dist_path):
|
||||
"""
|
||||
Computes gradient and edge-based image quality metrics between images.
|
||||
"""
|
||||
ref_img = load_image(ref_path)
|
||||
dist_img = load_image(dist_path)
|
||||
|
||||
output = {}
|
||||
|
||||
metrics = [
|
||||
("GMSD", lambda: gmsd(ref_img, dist_img)),
|
||||
("G-SSIM", lambda: g_ssim(ref_img, dist_img)),
|
||||
("ESIM", lambda: esim(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, converts it to floating-point, and transforms it to grayscale if necessary.
|
||||
"""
|
||||
img = io.imread(path) # Load the image using skimage's imread
|
||||
img = img_as_float(img) # Convert image to float values in range [0, 1]
|
||||
|
||||
# Convert to grayscale if image is in RGB format
|
||||
if img.ndim == 3:
|
||||
img = rgb2gray(img)
|
||||
return img
|
||||
|
||||
|
||||
# Gradient Magnitude Similarity Deviation (GMSD)
|
||||
def gmsd(ref_img, dist_img):
|
||||
"""
|
||||
Computes the Gradient Magnitude Similarity Deviation (GMSD) by comparing
|
||||
the gradient magnitudes of the reference and distorted images.
|
||||
"""
|
||||
ref_grad = sobel(ref_img)
|
||||
dist_grad = sobel(dist_img)
|
||||
|
||||
# GMSD is the standard deviation of gradient magnitude differences
|
||||
gmsd_value = np.sqrt(np.mean((ref_grad - dist_grad) ** 2))
|
||||
return gmsd_value
|
||||
|
||||
|
||||
# Gradient-based SSIM (G-SSIM)
|
||||
def g_ssim(ref_img, dist_img):
|
||||
"""
|
||||
Computes the Gradient-based Structural Similarity (G-SSIM) by comparing
|
||||
the gradients of the reference and distorted images using SSIM.
|
||||
"""
|
||||
ref_grad = sobel(ref_img)
|
||||
dist_grad = sobel(dist_img)
|
||||
|
||||
# SSIM between gradients
|
||||
score = structural_similarity(ref_grad, dist_grad, data_range=1.0)
|
||||
return score
|
||||
|
||||
|
||||
# Edge Similarity Index (ESIM)
|
||||
def esim(ref_img, dist_img):
|
||||
"""
|
||||
Computes the Edge Similarity Index (ESIM) by comparing the edges of the
|
||||
reference and distorted images using the Sobel filter.
|
||||
"""
|
||||
# Apply Sobel filter to extract edges
|
||||
ref_edges = sobel(ref_img)
|
||||
dist_edges = sobel(dist_img)
|
||||
|
||||
# Calculate the absolute difference between edge maps
|
||||
esim_value = np.mean(np.abs(ref_edges - dist_edges))
|
||||
return 1 - esim_value # ESIM is often 1 minus the edge difference for similarity
|
||||
|
||||
|
||||
# TESTED
|
||||
if __name__ == "__main__":
|
||||
# Tested:
|
||||
# GMSD (Gradient Magnitude Similarity Deviation)
|
||||
# G-SSIM (Gradient-based Structural Similarity)
|
||||
# ESIM (Edge Similarity 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 gradient and edge-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