SSIMIndex

MainPage

Description: This method computes the Structural Similarity Index between images of the same size.

Signature: ssim = SSIMIndex(X,Y, k1 = 0.01, k2 = 0.03)

Input: - X: 2 or 3d-array, uint8. Image 1. - Y: 2 or 3d-array, uint8. Image 2. - k1: 2 or 3d-array, bool. Coefficient of the method defined in the paper. - k2: 2 or 3d-array, bool. Coefficient of the method defined in the paper.

Output:

  • ssim, float. Structural Similarity Index value.

Python Implementation

 1 import numpy as np
 2 
 3 def SSIMIndex(X,Y, k1 = 0.01, k2 = 0.03):
 4     """
 5     Structural similarity index. Implementation based on the paper: Image Quality Assessment:
 6     From Error Visibility to Structural Similarity. k1 = 0.01, k2 = 0.03 are the
 7     default values used in the paper.
 8     """
 9 
10     X = X.astype(float)
11     Y = Y.astype(float)
12 
13     mu_x = X.mean()
14     mu_y = Y.mean()
15 
16     sigma_xy = 1.0/(X.size - 1)*((X.ravel() - mu_x)*(Y.ravel() - mu_y)).sum()
17     sigma_y = 1.0/(Y.size - 1)*((Y.ravel() - mu_y)** 2).sum()
18     sigma_y = np.sqrt(sigma_y)
19     sigma_x = 1.0/(X.size - 1)*((X.ravel() - mu_x)** 2).sum()
20     sigma_x = np.sqrt(sigma_x)
21 
22     C1 = (k1*255)*(k1*255)
23     C2 = (k2*255)*(k2*255)
24     ssim = (2*mu_x*mu_y + C1)*(2*sigma_xy + C2)/((mu_x*mu_x + mu_y*mu_y + C1)\
25            *(sigma_x*sigma_x + sigma_y*sigma_y + C2))
26     return ssim

Example

 1 from iamxt.ssim_index import  SSIMIndex
 2 from iamxt.max_tree_alpha import MaxTreeAlpha
 3 from iamxt.ssim_index import  SSIMIndex
 4 import numpy as np
 5 
 6 img = adreadgray('Attachments/iamxt/MainPage/cameraman.tif')
 7 Bc = np.ones((3,3),dtype = bool)
 8 mxt = MaxTreeAlpha(img,Bc)
 9 
10 n = 0.005
11 nleaves = (mxt.node_array[1,:] == 0).sum()
12 A = mxt.node_array[3,:]
13 Aext = mxt.computeExtinctionValues(A,"area")
14 mxt.extinctionFilter(Aext,int(n*nleaves))
15 
16 filtered_img = mxt.getImage()
17 
18 adshow(img,"(a) Original image")
19 adshow(filtered_img,"(b) Filtered image")
20 
21 ssim = SSIMIndex(img,filtered_img)
22 print "SSIM index:", ssim
SSIM index: 0.98340395003

(a) Original image

(b) Filtered image