""" This module contains functions for computing wavelet-based image quality metrics, such as W-SSIM (Wavelet-based Weighted SSIM), CW-SSIM (Complex Wavelet SSIM), and SPIQ (Steerable Pyramid-based Image Quality Index). These metrics evaluate the similarity between reference and distorted images using wavelet and pyramid-based decompositions. Functions: - compute_all: Computes all wavelet-based image quality metrics. - load_image: Loads an image and converts it to floating-point format. - w_ssim: Computes the Wavelet-based Weighted SSIM (W-SSIM). - cw_ssim: Computes the Complex Wavelet SSIM (CW-SSIM). - spiq: Computes the Steerable Pyramid-based Image Quality Index (SPIQ). """ from time import time as now import numpy as np import pywt 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 # Function to compute all wavelet-based metrics def compute_all(ref_path, dist_path): """ Computes multiple wavelet-based image quality metrics between a reference and distorted image. """ ref_img = load_image(ref_path) dist_img = load_image(dist_path) metrics = [ ("W-SSIM", lambda: w_ssim(ref_img, dist_img)), ("CW-SSIM", lambda: cw_ssim(ref_img, dist_img)), ("SPIQ", lambda: spiq(ref_img, dist_img)), ] 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): """ 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 # W-SSIM (Wavelet-based Weighted SSIM) def w_ssim(ref_img, dist_img): """ Computes the Wavelet-based Weighted SSIM (W-SSIM) by applying a wavelet decomposition and computing SSIM on the wavelet subbands. """ # Apply 2D discrete wavelet transform using 'haar' ref_coeffs = pywt.dwtn(ref_img, 'haar') dist_coeffs = pywt.dwtn(dist_img, 'haar') # Compute SSIM for each level of the wavelet decomposition ssim_values = [] for key in ref_coeffs.keys(): ref_band = ref_coeffs[key] dist_band = dist_coeffs[key] # Skip subbands that are too small for SSIM computation if ref_band.shape[0] < 7 or ref_band.shape[1] < 7: continue min_dim = min(ref_band.shape) win_size = min(7, min_dim) # Set window size to a smaller value if subband is small if win_size % 2 == 0: win_size -= 1 # Ensure win_size is odd min_dim = min(ref_band.shape) win_size = min(7, min_dim) # Set window size if win_size % 2 == 0: win_size -= 1 # Ensure win_size is odd try: # Compute SSIM for the subband with the specified win_size ssimband = structural_similarity(ref_band, dist_band, win_size=win_size, data_range=1.0) ssim_values.append(ssimband) except ZeroDivisionError: # In case of a division by zero error, skip this subband continue return np.mean(ssim_values) if ssim_values else 0 # CW-SSIM (Complex Wavelet SSIM) def cw_ssim(ref_img, dist_img): """ Computes the Complex Wavelet SSIM (CW-SSIM) by performing a wavelet decomposition and computing SSIM on the real and imaginary parts of the subbands. """ 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 # SPIQ (Steerable Pyramid-based Image Quality Index) def spiq(ref_img, dist_img): """ Computes the Steerable Pyramid-based Image Quality Index (SPIQ) by approximating the steerable pyramid decomposition with image gradients. """ # Apply steerable pyramid decomposition (simplified with gradients) ref_grad = sobel(ref_img) dist_grad = sobel(dist_img) # Compute SSIM between gradient maps score = structural_similarity(ref_grad, dist_grad, data_range=1.0) return score if __name__ == "__main__": # Tested: # W-SSIM (Wavelet-based Weighted SSIM) # CW-SSIM (Complex Wavelet SSIM) # SPIQ (Steerable Pyramid-based Image Quality 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 wavelet-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)")