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

251 lines
8.5 KiB
Python

"""
This module contains functions for computing SSIM-based and gradient-based image quality metrics,
such as SSIM (Structural Similarity Index), MS-SSIM (Multi-Scale SSIM), G-SSIM (Gradient SSIM),
IW-SSIM (Information Weighted SSIM), FSIM (Feature Similarity Index), DSSIM (Difference SSIM),
and GMS+CMS (Gradient Magnitude and Color Similarity).
These metrics evaluate the similarity between reference and distorted images
by considering structural, gradient, and feature-based aspects.
Functions:
- compute_all: Computes all SSIM-based and gradient-based image quality metrics.
- load_image: Loads an image and converts it to a specified format.
- ssim: Computes the Structural Similarity Index (SSIM).
- ms_ssim: Computes the Multi-Scale SSIM (MS-SSIM).
- cw_ssim: Computes the Complex Wavelet SSIM (CW-SSIM).
- g_ssim: Computes the Gradient Structural Similarity Index (G-SSIM).
- w_ssim: Computes the Weighted SSIM (W-SSIM).
- iw_ssim: Computes the Information Weighted SSIM (IW-SSIM).
- fsim: Computes the Feature Similarity Index (FSIM).
- r_ssim: Computes the Region-Based SSIM (R-SSIM).
- dssim: Computes the Difference SSIM (DSSIM).
- gmsd: Computes the Gradient Magnitude Similarity Deviation (GMSD).
- c_ssim: Computes the Color SSIM (C-SSIM).
"""
from time import time as now
import numpy as np
import pywt
from skimage import io, img_as_float, img_as_ubyte
from skimage.metrics import structural_similarity # pylint: disable=E0611
from skimage.color import rgb2gray, rgb2yuv # pylint: disable=E0611
from skimage.filters import sobel # pylint: disable=E0611
from sewar import msssim as multiscale_ssim
def compute_all(ref_path, dist_path):
"""
Computes multiple SSIM-based and FSIM-based image quality metrics.
"""
ref_img_rgb = load_image(ref_path, 'rgb')
dist_img_rgb = load_image(dist_path, 'rgb')
ref_img_gray = load_image(ref_path, 'gray')
dist_img_gray = load_image(dist_path, 'gray')
ref_img_yuv = load_image(ref_path, 'yuv')
dist_img_yuv = load_image(dist_path, 'yuv')
output = {}
metrics = [
("SSIM", lambda: ssim(ref_img_rgb, dist_img_rgb)),
("MS-SSIM", lambda: ms_ssim(ref_img_rgb, dist_img_rgb)),
("CW-SSIM", lambda: cw_ssim(ref_img_gray, dist_img_gray)),
("G-SSIM", lambda: g_ssim(ref_img_rgb, dist_img_rgb)),
("W-SSIM", lambda: w_ssim(ref_img_rgb, dist_img_rgb)),
("IW-SSIM", lambda: iw_ssim(ref_img_rgb, dist_img_rgb)),
("FSIM", lambda: fsim(ref_img_gray, dist_img_gray)),
("R-SSIM", lambda: r_ssim(ref_img_gray, dist_img_gray)),
("DSSIM", lambda: dssim(ref_img_rgb, dist_img_rgb)),
("GMSD", lambda: gmsd(ref_img_gray, dist_img_gray)),
("C-SSIM", lambda: c_ssim(ref_img_yuv, dist_img_yuv)),
]
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, scheme):
"""
Loads an image and converts it to the specified color scheme.
"""
img = io.imread(path)
img = img_as_float(img)
if scheme == 'gray':
img = rgb2gray(img)
elif scheme == 'yuv':
img = rgb2yuv(img)
return img
# Standard SSIM
def ssim(ref_img, dist_img):
"""
Computes the Structural Similarity Index (SSIM).
"""
if ref_img.ndim == 3 and dist_img.ndim == 3:
# Color images: Compute SSIM for each channel and average the results
ssim_r = structural_similarity(ref_img[:, :, 0], dist_img[:, :, 0], data_range=1.0)
ssim_g = structural_similarity(ref_img[:, :, 1], dist_img[:, :, 1], data_range=1.0)
ssim_b = structural_similarity(ref_img[:, :, 2], dist_img[:, :, 2], data_range=1.0)
return (ssim_r + ssim_g + ssim_b) / 3
elif ref_img.ndim == 2 and dist_img.ndim == 2:
# Grayscale images: Compute SSIM directly
return structural_similarity(ref_img, dist_img, data_range=1.0)
# Multi-Scale SSIM (MS-SSIM)
def ms_ssim(ref_img, dist_img):
"""
Computes the Multi-Scale Structural Similarity Index (MS-SSIM).
"""
ref_img = img_as_ubyte(ref_img)
dist_img = img_as_ubyte(dist_img)
return multiscale_ssim(ref_img, dist_img)
# Complex Wavelet SSIM (CW-SSIM)
def cw_ssim(ref_img, dist_img):
"""
Computes the Complex Wavelet Structural Similarity Index (CW-SSIM).
"""
ref_coeffs = pywt.dwtn(ref_img, 'haar')
dist_coeffs = pywt.dwtn(dist_img, 'haar')
# Compute the SSIM on the real and imaginary parts of the complex wavelet
score = np.mean([
structural_similarity(ref_coeffs[k].real, dist_coeffs[k].real, data_range=1.0) +
structural_similarity(ref_coeffs[k].imag, dist_coeffs[k].imag, data_range=1.0)
for k in ref_coeffs.keys()
]) / 2.0
return score
# Gradient-based SSIM (G-SSIM)
def g_ssim(ref_img, dist_img):
"""
Computes Gradient-based SSIM (G-SSIM).
"""
ref_grad = sobel(ref_img)
dist_grad = sobel(dist_img)
return ssim(ref_grad, dist_grad)
# Weighted SSIM (W-SSIM) - Using a simple weighted region-based SSIM
def w_ssim(ref_img, dist_img, weight=0.7):
"""
Computes Weighted SSIM (W-SSIM) with a focus on center regions.
"""
height, width, _ = ref_img.shape
center_region = ref_img[height // 4: 3 * height // 4, width // 4: 3 * width // 4, :]
center_dist = dist_img[height // 4: 3 * height // 4, width // 4: 3 * width // 4, :]
outer_region = np.copy(ref_img)
outer_region[height // 4: 3 * height // 4, width // 4: 3 * width // 4, :] = 0
center_ssim = ssim(center_region, center_dist)
outer_ssim = ssim(outer_region, dist_img)
weighted_score = weight * center_ssim + (1 - weight) * outer_ssim
return weighted_score
# Information Content Weighted SSIM (IW-SSIM)
def iw_ssim(ref_img, dist_img):
"""
Computes Information Content Weighted SSIM (IW-SSIM).
"""
entropy_ref = -ref_img * np.log2(ref_img + 1e-10)
entropy_dist = -dist_img * np.log2(dist_img + 1e-10)
local_ssim = ssim(ref_img, dist_img)
score = np.mean(entropy_ref * local_ssim + entropy_dist * local_ssim)
return score
# Feature SIMilarity Index (FSIM)
def fsim(ref_img, dist_img):
"""
Computes the Feature SIMilarity Index (FSIM).
"""
ref_grad = sobel(ref_img)
dist_grad = sobel(dist_img)
score = structural_similarity(ref_grad, dist_grad, data_range=1.0)
return score
# Region-Based SSIM (R-SSIM)
def r_ssim(ref_img, dist_img):
"""
Computes the Region-Based SSIM (R-SSIM).
"""
height, width = ref_img.shape
center_ref = ref_img[height // 4: 3 * height // 4, width // 4: 3 * width // 4]
center_dist = dist_img[height // 4: 3 * height // 4, width // 4: 3 * width // 4]
score = structural_similarity(center_ref, center_dist, data_range=1.0)
return score
# Difference SSIM (DSSIM)
def dssim(ref_img, dist_img):
"""
Computes the Difference Structural Similarity Index (DSSIM).
"""
score = ssim(ref_img, dist_img)
return (1 - score) / 2
# Color SSIM (C-SSIM)
def c_ssim(ref_img, dist_img):
"""
Computes Color SSIM (C-SSIM) on the YUV color space.
"""
y_ssim = structural_similarity(ref_img[:, :, 0], dist_img[:, :, 0], data_range=1.0)
u_ssim = structural_similarity(ref_img[:, :, 1], dist_img[:, :, 1], data_range=1.0)
v_ssim = structural_similarity(ref_img[:, :, 2], dist_img[:, :, 2], data_range=1.0)
return (y_ssim + u_ssim + v_ssim) / 3
# Gradient Magnitude Similarity Deviation (GMSD)
def gmsd(ref_img, dist_img):
"""
Computes Gradient Magnitude Similarity Deviation (GMSD)
"""
ref_grad = sobel(ref_img)
dist_grad = sobel(dist_img)
return np.sqrt(np.mean((ref_grad - dist_grad) ** 2))
if __name__ == "__main__":
# Tested:
# SSIM (Structural Similarity Index)
# MS-SSIM (Multi-Scale SSIM)
# CW-SSIM (Complex Wavelet SSIM)
# G-SSIM (Gradient-based SSIM)
# W-SSIM (Weighted SSIM)
# IW-SSIM (Information Content Weighted SSIM)
# FSIM (Feature SIMilarity Index)
# R-SSIM (Region-based SSIM)
# DSSIM (Difference SSIM)
# C-SSIM (Color SSIM)
# GMSD (Gradient Magnitude Similarity Deviation)
# 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 region-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)")