Korean, Edit

Chapter 5-1. Image Similarity Comparison: SSIM

Recommended Article: 【Statistics】 Chapter 5. Statistic


1. Overview

2. Theory

3. Code


a. Distance Function and Similarity



1. Overview

⑴ SSIM (structural similarity index measure)

⑵ First Introduced: Image Quality Assessment: From Error Visibility to Structural Similarity (2004, IEEE)

⑶ Conventional methods only investigated mean squared error



2. Theory

⑴ When comparing two images (or windows) x and y:

⑵ Components of an Image

① Luminance: Represents the brightness of light


image


② Contrast: The property of drastic changes in brightness within an image


image


③ Structure: The property created by the relative positions of pixels


image


⑶ Comparison Function

① Conditions of the final comparison function: Let the final comparison function be S(x, y),

○ Condition 1. Symmetry: S(x, y) = S(y, x)

○ Condition 2. S(x , y) ≤ 1

○ Condition 3. Unique maximum: S(x , y) = 1 ⇔ x = y

② Luminance comparison function

○ Consistent with Weber’s Law


image


③ Contrast comparison function


image


④ Structure comparison function

○ Statistic: σxx = var(x), σyy = var(y), σxy = cov(x, y)


image


⑷ Mean Structural Similarity Index (SSIM)

① Formulation


image


② Default


image


③ More effective when used regionally rather than globally

○ Reason 1. Statistical properties of an image (e.g., mean, variance) vary depending on the ROI (Region of Interest)

○ Reason 2. Image distortion is not uniform across the entire image

○ Reason 3. The human visual system also focuses on specific parts of the image rather than the whole

○ Reason 4. Regional analysis allows for more diverse and richer analysis through various combinations of ROIs

⑸ (Note) Multiscale Spectral Similarity Index (MSSI)



3. Code


## Method 1

def SSIM(x, y):
    # assumption : x and y are grayscale images with the same dimension

    import numpy as np
    
    def mean(img):
        return np.mean(img)
        
    def sigma(img):
        return np.std(img)
    
    def cov(img1, img2):
        img1_ = np.array(img1[:,:], dtype=np.float64)
        img2_ = np.array(img2[:,:], dtype=np.float64)
                        
        return np.mean(img1_ * img2_) - mean(img1) * mean(img2)
    
    K1 = 0.01
    K2 = 0.03
    L = 256 # when each pixel spans 0 to 255
   
    C1 = K1 * K1 * L * L
    C2 = K2 * K2 * L * L
    C3 = C2 / 2
        
    l = (2 * mean(x) * mean(y) + C1) / (mean(x)**2 + mean(y)**2 + C1)
    c = (2 * sigma(x) * sigma(y) + C2) / (sigma(x)**2 + sigma(y)**2 + C2)
    s = (cov(x, y) + C3) / (sigma(x) * sigma(y) + C3)
        
    return l * c * s
    
    
    
 ## Method 2 
 
from skimage.metrics import structural_similarity as ssim
import cv2

x = cv2.imread('x.png', cv2.IMREAD_GRAYSCALE)
y = cv2.imread('y.png', cv2.IMREAD_GRAYSCALE)

score, diff = ssim(x, y, full=True)
print("SSIM:", score)


⑴ How to Use the Function in Python



Input: 2021.02.19 21:04

Modified: 2024.10.07 13:23

results matching ""

    No results matching ""