""" 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), MS-FSIM (Multi-Scale FSIM), 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 floating-point format. - ssim: Computes the Structural Similarity Index (SSIM). - ms_ssim: Computes the Multi-Scale SSIM (MS-SSIM). - g_ssim: Computes the Gradient Structural Similarity Index (G-SSIM). - iw_ssim: Computes the Information Weighted SSIM (IW-SSIM). - fsim: Computes the Feature Similarity Index (FSIM). - ms_fsim: Computes the Multi-Scale Feature Similarity Index (MS-FSIM). - dssim: Computes the Difference SSIM (DSSIM). - gms_cms: Computes the combined Gradient Magnitude and Color Similarity (GMS+CMS). """ from time import time as now import numpy as np 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 gradient-based image quality metrics between a reference and distorted image. """ ref_img = load_image(ref_path) dist_img = load_image(dist_path) ref_img_rgb = load_image(ref_path, 'rgb') dist_img_rgb = load_image(dist_path, 'rgb') metrics = [ ("SSIM", lambda: ssim(ref_img, dist_img)), ("MS-SSIM", lambda: ms_ssim(ref_img, dist_img)), ("G-SSIM", lambda: g_ssim(ref_img, dist_img)), ("IW-SSIM", lambda: iw_ssim(ref_img, dist_img)), ("FSIM", lambda: fsim(ref_img, dist_img)), ("MS-FSIM", lambda: ms_fsim(ref_img, dist_img)), ("DSSIM", lambda: dssim(ref_img, dist_img)), ("GMS+CMS", lambda: gms_cms(ref_img_rgb, dist_img_rgb)), ] output = {} 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='gray'): """ Loads an image from the given path and converts it to floating-point format. If the image is in RGB format, it is converted to grayscale. """ img = io.imread(path) img = img_as_float(img) if scheme == 'gray': img = rgb2gray(img) return img # SSIM (Structural Similarity Index) def ssim(ref_img, dist_img): """ Computes the Structural Similarity Index (SSIM) between the reference and distorted images. """ score = structural_similarity(ref_img, dist_img, data_range=1.0) return score # MS-SSIM (Multi-Scale SSIM) def ms_ssim(ref_img, dist_img): """ Computes the Multi-Scale Structural Similarity Index (MS-SSIM) between images. """ ref_img = img_as_ubyte(ref_img) dist_img = img_as_ubyte(dist_img) score = abs(multiscale_ssim(ref_img, dist_img)) return score # G-SSIM (Gradient Structural Similarity Index) def g_ssim(ref_img, dist_img): """ Computes the Gradient Structural Similarity Index (G-SSIM) by comparing the gradients of the reference and distorted images. """ ref_grad = sobel(ref_img) dist_grad = sobel(dist_img) score = structural_similarity(ref_grad, dist_grad, data_range=1.0) return score # IW-SSIM (Information Weighted SSIM) def iw_ssim(ref_img, dist_img): """ Computes the Information Weighted Structural Similarity Index (IW-SSIM) by weighting the SSIM value with local entropy information. """ entropy_ref = -ref_img * np.log2(ref_img + 1e-10) entropy_dist = -dist_img * np.log2(dist_img + 1e-10) local_ssim = structural_similarity(ref_img, dist_img, data_range=1.0) score = np.mean(entropy_ref * local_ssim + entropy_dist * local_ssim) return score # FSIM (Feature Similarity Index) def fsim(ref_img, dist_img): """ Computes the Feature Similarity Index (FSIM) by comparing the gradients of the reference and distorted images. """ ref_grad = sobel(ref_img) dist_grad = sobel(dist_img) score = structural_similarity(ref_grad, dist_grad, data_range=1.0) return score # MS-FSIM (Multi-Scale Feature Similarity Index) def ms_fsim(ref_img, dist_img): """ Computes the Multi-Scale Feature Similarity Index (MS-FSIM) by combining structural and gradient similarity across multiple scales. """ ref_grad = sobel(ref_img) dist_grad = sobel(dist_img) # Multi-scale SSIM for structural similarity structural_similarity_value = structural_similarity(ref_img, dist_img, data_range=1.0) # FSIM based on gradient magnitude similarity gradient_similarity = np.mean(1 - np.abs(ref_grad - dist_grad)) # Combine structural similarity and feature similarity (FSIM) score = 0.5 * structural_similarity_value + 0.5 * gradient_similarity return score # DSSIM (Difference SSIM) def dssim(ref_img, dist_img): """ Computes the Difference SSIM (DSSIM) as the dissimilarity measure derived from SSIM. """ score = structural_similarity(ref_img, dist_img, data_range=1.0) score = (1 - score) / 2 return score # GMS + CMS (Gradient Magnitude and Color Similarity) def gms_cms(ref_img, dist_img): """ Computes the combined Gradient Magnitude Similarity (GMS) and Color Similarity (CMS) between the reference and distorted images. """ ref_grad = sobel(ref_img) dist_grad = sobel(dist_img) # Gradient Magnitude Similarity (GMS) gms_value = np.mean(1 - np.abs(ref_grad - dist_grad)) # Color Similarity (CMS) ref_yuv = rgb2yuv(ref_img) dist_yuv = rgb2yuv(dist_img) y_similarity = structural_similarity(ref_yuv[:, :, 0], dist_yuv[:, :, 0], data_range=1.0) u_similarity = structural_similarity(ref_yuv[:, :, 1], dist_yuv[:, :, 1], data_range=1.0) v_similarity = structural_similarity(ref_yuv[:, :, 2], dist_yuv[:, :, 2], data_range=1.0) cms_value = (y_similarity + u_similarity + v_similarity) / 3 # Combine GMS and CMS score = 0.5 * gms_value + 0.5 * cms_value return score if __name__ == "__main__": # Tested: # SSIM (Structural Similarity Index) # MS-SSIM (Multi-Scale SSIM) # G-SSIM (Gradient SSIM) # IW-SSIM (Information Weighted SSIM) # FSIM (Feature Similarity Index) # MS-FSIM (Multi-Scale FSIM) # DSSIM (Difference SSIM) # GMS + CMS (Gradient Magnitude and Color Similarity) # 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 structural-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)")