-
Notifications
You must be signed in to change notification settings - Fork 1
/
imqual_utils.py
77 lines (61 loc) · 2.05 KB
/
imqual_utils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
"""
# > Implementation of the classic paper by Zhou Wang et. al.:
# - Image quality assessment: from error visibility to structural similarity
# - https://ieeexplore.ieee.org/stamp/stamp.jsp?arnumber=1284395
# > Maintainer: https://github.com/xahidbuffon
"""
from __future__ import division
import numpy as np
import math
from scipy.ndimage import gaussian_filter
def getSSIM(X, Y):
"""
Computes the mean structural similarity between two images.
"""
assert (X.shape == Y.shape), "Image-patche provided have different dimensions"
nch = 1 if X.ndim==2 else X.shape[-1]
mssim = []
for ch in range(nch):
Xc, Yc = X[...,ch].astype(np.float64), Y[...,ch].astype(np.float64)
mssim.append(compute_ssim(Xc, Yc))
return np.mean(mssim)
def compute_ssim(X, Y):
"""
Compute the structural similarity per single channel (given two images)
"""
# variables are initialized as suggested in the paper
K1 = 0.01
K2 = 0.03
sigma = 1.5
win_size = 5
# means
ux = gaussian_filter(X, sigma)
uy = gaussian_filter(Y, sigma)
# variances and covariances
uxx = gaussian_filter(X * X, sigma)
uyy = gaussian_filter(Y * Y, sigma)
uxy = gaussian_filter(X * Y, sigma)
# normalize by unbiased estimate of std dev
N = win_size ** X.ndim
unbiased_norm = N / (N - 1) # eq. 4 of the paper
vx = (uxx - ux * ux) * unbiased_norm
vy = (uyy - uy * uy) * unbiased_norm
vxy = (uxy - ux * uy) * unbiased_norm
R = 255
C1 = (K1 * R) ** 2
C2 = (K2 * R) ** 2
# compute SSIM (eq. 13 of the paper)
sim = (2 * ux * uy + C1) * (2 * vxy + C2)
D = (ux ** 2 + uy ** 2 + C1) * (vx + vy + C2)
SSIM = sim/D
mssim = SSIM.mean()
return mssim
def getPSNR(X, Y):
#assume RGB image
target_data = np.array(X, dtype=np.float64)
ref_data = np.array(Y, dtype=np.float64)
diff = ref_data - target_data
diff = diff.flatten('C')
rmse = math.sqrt(np.mean(diff ** 2.) )
if rmse == 0: return 100
else: return 20*math.log10(255.0/rmse)