""" This module contains functions for computing region-based image quality metrics, such as R-SSIM (Region-based SSIM) and L-SSIM (Local SSIM). It uses structural similarity indices to evaluate the similarity between reference and distorted images. Functions: - compute_all: Computes all region-based image quality metrics. - load_image: Loads an image and converts it to grayscale. - r_ssim: Computes the Region-based SSIM for a specific region of the image. - l_ssim: Computes the Local SSIM by sliding a window across the image. """ from time import time as now import numpy as np from skimage import io, img_as_float from skimage.metrics import structural_similarity # pylint: disable=E0611 from skimage.color import rgb2gray # pylint: disable=E0611 def compute_all(ref_path, dist_path): """ Computes multiple region-based image quality metrics. """ ref_img = load_image(ref_path) dist_img = load_image(dist_path) output = {} metrics = [ ("R-SSIM", lambda: r_ssim(ref_img, dist_img)), ("L-SSIM", lambda: l_ssim(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 # R-SSIM (Region-based SSIM) def r_ssim(ref_img, dist_img): """ Computes the SSIM (Structural Similarity Index) over the central region of the image. """ height, width = ref_img.shape # Define a central region of the image (taking the central half in both directions) 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] # Compute SSIM for the central region only score = structural_similarity(center_ref, center_dist, data_range=1.0) return score # L-SSIM (Local SSIM) def l_ssim(ref_img, dist_img, window_size=8): """ Computes the SSIM (Structural Similarity Index) locally by sliding a window over the images. """ height, width = ref_img.shape # List to hold SSIM values for each window local_ssim_values = [] # Slide the window across the image for i in range(0, height - window_size + 1, window_size): for j in range(0, width - window_size + 1, window_size): # Extract local windows from the reference and distorted images ref_win = ref_img[i:i + window_size, j:j + window_size] dist_win = dist_img[i:i + window_size, j:j + window_size] # Compute SSIM for the local region (window) local_ssim = structural_similarity(ref_win, dist_win, data_range=1.0) local_ssim_values.append(local_ssim) # Return the mean SSIM value across all windows return np.mean(local_ssim_values) if __name__ == "__main__": # Tested: # R-SSIM (Region-based SSIM) # L-SSIM (Local SSIM) # 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)")