-
Notifications
You must be signed in to change notification settings - Fork 0
/
calculate_PSNR_SSIM.py
186 lines (160 loc) · 6.47 KB
/
calculate_PSNR_SSIM.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
'''
calculate the PSNR and SSIM.
same as MATLAB's results
'''
import math
import os
import sys
import cv2
import numpy as np
sys.path.append('../')
from matplotlib import pyplot as plt
def main(folder_GT,folder_Gen):
# Configurations
# GT - Ground-truth;
# Gen: Generated / Restored / Recovered images
# folder_GT = '/mnt/cv/code/IMDN/Test_Datasets/Set5'
# folder_Gen = '/mnt/cv/code/IMDN/results/Set5/x2'
test_Y =False
PSNR_all = []
SSIM_all = []
img_list = os.listdir(folder_GT)
imgreal_list = os.listdir(folder_Gen)
file_num = len(img_list)
for i in range(file_num):
path_GT=os.path.join(folder_GT,img_list[i])
im_GT = cv2.imread(path_GT,0) / 255.
path_Gen=os.path.join(folder_Gen,imgreal_list[i])
im_Gen = cv2.imread(path_Gen,0) / 255.
# calculate PSNR and SSIM
PSNR = calculate_psnr(im_GT,im_Gen)
SSIM = calculate_ssim(im_GT*255,im_Gen*255)
print('the {:3d}th - {:25}. \tPSNR: {:.6f} dB, \tSSIM: {:.6f}'.format(
i + 1, img_list[i], PSNR, SSIM))
PSNR_all.append(PSNR)
SSIM_all.append(SSIM)
print('Average: PSNR: {:.6f} dB, SSIM: {:.6f}'.format(
sum(PSNR_all) / len(PSNR_all),
sum(SSIM_all) / len(SSIM_all)))
#return [int(folder_Gen.split('/')[-1].split('_')[-1][2:]), sum(PSNR_all) / len(PSNR_all), sum(SSIM_all) / len(SSIM_all)]
'''
def calculate_psnr(img1, img2):
# img1 and img2 have range [0, 255]
img1 = img1.astype(np.float64)
img2 = img2.astype(np.float64)
mse = np.mean((img1 - img2)**2)
if mse == 0:
return float('inf')
return 20 * math.log10(255.0 / math.sqrt(mse))
'''
def calculate_psnr(img1, img2):
mse = np.mean((img1 - img2) ** 2)
if np.max(img1) > 128.:
PIXEL_MAX = 255.
else:
PIXEL_MAX = 1.
if mse < 1.0e-10:
return 100
return 20 * math.log10(PIXEL_MAX / math.sqrt(mse))
def ssim(img1, img2):
C1 = (0.01 * 255)**2
C2 = (0.03 * 255)**2
img1 = img1.astype(np.float64)
img2 = img2.astype(np.float64)
kernel = cv2.getGaussianKernel(11, 1.5)
window = np.outer(kernel, kernel.transpose())
mu1 = cv2.filter2D(img1, -1, window)[5:-5, 5:-5] # valid
mu2 = cv2.filter2D(img2, -1, window)[5:-5, 5:-5]
mu1_sq = mu1**2
mu2_sq = mu2**2
mu1_mu2 = mu1 * mu2
sigma1_sq = cv2.filter2D(img1**2, -1, window)[5:-5, 5:-5] - mu1_sq
sigma2_sq = cv2.filter2D(img2**2, -1, window)[5:-5, 5:-5] - mu2_sq
sigma12 = cv2.filter2D(img1 * img2, -1, window)[5:-5, 5:-5] - mu1_mu2
ssim_map = ((2 * mu1_mu2 + C1) * (2 * sigma12 + C2)) / ((mu1_sq + mu2_sq + C1) *
(sigma1_sq + sigma2_sq + C2))
return ssim_map.mean()
def calculate_ssim(img1, img2):
'''calculate SSIM
the same outputs as MATLAB's
img1, img2: [0, 255]
'''
if not img1.shape == img2.shape:
raise ValueError('Input images must have the same dimensions.')
if img1.ndim == 2:
return ssim(img1, img2)
elif img1.ndim == 3:
if img1.shape[2] == 3:
ssims = []
for i in range(3):
ssims.append(ssim(img1, img2))
return np.array(ssims).mean()
elif img1.shape[2] == 1:
return ssim(np.squeeze(img1), np.squeeze(img2))
else:
raise ValueError('Wrong input image dimensions.')
def bgr2ycbcr(img, only_y=True):
'''same as matlab rgb2ycbcr
only_y: only return Y channel
Input:
uint8, [0, 255]
float, [0, 1]
'''
in_img_type = img.dtype
img.astype(np.float32)
if in_img_type != np.uint8:
img *= 255.
# convert
if only_y:
rlt = np.dot(img, [24.966, 128.553, 65.481]) / 255.0 + 16.0
else:
rlt = np.matmul(img, [[24.966, 112.0, -18.214], [128.553, -74.203, -93.786],
[65.481, -37.797, 112.0]]) / 255.0 + [16, 128, 128]
if in_img_type == np.uint8:
rlt = rlt.round()
else:
rlt /= 255.
return rlt.astype(in_img_type)
def draw_nf_rf_vs_psnr_ssim(NF_X, RF_X, NF_Y_PSNR, NF_Y_SSIM, RF_Y_PSNR, RF_Y_SSIM):
plt.subplot(2, 2, 1)
plt.title('PSNR VS channel', fontsize=15) # 标题,并设定字号大小
plt.xlabel("channel", fontsize=14) # 设置x轴,并设定字号大小
plt.ylabel("PSNR", fontsize=14) # 设置y轴,并设定字号大小
plt.grid()
plt.plot(NF_X[:-4], NF_Y_PSNR[:-4], color="deeppink", linewidth=2, marker='^')
plt.plot(NF_X[-4:], NF_Y_PSNR[-4:], color="g", linewidth=2, marker='^',linestyle=':')
plt.subplot(2, 2, 2)
plt.title('SSIM VS channel', fontsize=15) # 标题,并设定字号大小
plt.xlabel("channel", fontsize=14) # 设置x轴,并设定字号大小
plt.ylabel("SSIM", fontsize=14) # 设置y轴,并设定字号大小
plt.grid()
plt.plot(NF_X[:-4], NF_Y_SSIM[:-4], color="deeppink", linewidth=2, marker='^')
plt.plot(NF_X[-4:], NF_Y_SSIM[-4:], color="g", linewidth=2, marker='^',linestyle=':')
plt.subplot(2, 2, 3)
plt.title('PSNR VS RF', fontsize=15) # 标题,并设定字号大小
plt.xlabel("channel", fontsize=14) # 设置x轴,并设定字号大小
plt.ylabel("PSNR", fontsize=14) # 设置y轴,并设定字号大小
plt.grid()
plt.plot(RF_X, RF_Y_PSNR, color="deeppink", linewidth=2, marker='^')
plt.subplot(2, 2, 4)
plt.title('SSIM VS RF', fontsize=15) # 标题,并设定字号大小
plt.xlabel("channel", fontsize=14) # 设置x轴,并设定字号大小
plt.ylabel("SSIM", fontsize=14) # 设置y轴,并设定字号大小
plt.grid()
plt.plot(RF_X, RF_Y_SSIM, color="deeppink", linewidth=2, marker='^')
plt.show()
if __name__ == '__main__':
folder_GT='./results/Derain/test_220/images'
folder_Gen='./results/Derain/test_220/real'
# folder_GT='../../dataset/RAIN800_DID_MDN_dataset/Rain12'
# folder_Gen='../rgb_in_RAIN1400_FSPANet_detal_15/Rain12'
# folder_GT='../../dataset/RAIN800_DID_MDN_dataset/SPANet_test'
# folder_Gen='../rgb_in_RAIN1400_FSPANet_detal_15/SPANet_test'
# folder_GT='../../dataset/RAIN800_DID_MDN_dataset/rain100L_test'
# folder_Gen='../rgb_in_RAIN1400_FSPANet_detal_15/rain100L_test'
# folder_GT='../../dataset/RAIN800_DID_MDN_dataset/rain100H_test'
# folder_Gen='../rgb_in_RAIN1400_FSPANet_detal_15/rain100H_test'
# folder_GT='../../dataset/RescanDataset/test'
# folder_Gen='../rgb_in_RAIN1400_FSPANet_detal_15/test'
# DDN RescanNet PReNet SPANet FSPANet_detail FSPANet_X DSC
main(folder_GT,folder_Gen)