Skip to content

Commit b190817

Browse files
authored
Add files via upload
1 parent 2275cbb commit b190817

File tree

2 files changed

+251
-0
lines changed

2 files changed

+251
-0
lines changed

Pretreatment/PSNR_SSIM_Evaluate.ipynb

+172
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,172 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "code",
5+
"execution_count": 1,
6+
"metadata": {
7+
"collapsed": false
8+
},
9+
"outputs": [],
10+
"source": [
11+
"import math\n",
12+
"import numpy as np\n",
13+
"import cv2\n",
14+
"from PIL import Image"
15+
]
16+
},
17+
{
18+
"cell_type": "code",
19+
"execution_count": 2,
20+
"metadata": {
21+
"collapsed": true
22+
},
23+
"outputs": [],
24+
"source": [
25+
"def calculate_psnr(img1, img2):\n",
26+
" # img1 and img2 have range [0, 255]\n",
27+
" img1 = img1.astype(np.float64)\n",
28+
" img2 = img2.astype(np.float64)\n",
29+
" mse = np.mean((img1 - img2)**2)\n",
30+
" if mse == 0:\n",
31+
" return float('inf')\n",
32+
" return 20 * math.log10(255.0 / math.sqrt(mse))"
33+
]
34+
},
35+
{
36+
"cell_type": "code",
37+
"execution_count": 3,
38+
"metadata": {
39+
"collapsed": true
40+
},
41+
"outputs": [],
42+
"source": [
43+
"def ssim(img1, img2):\n",
44+
" C1 = (0.01 * 255)**2\n",
45+
" C2 = (0.03 * 255)**2\n",
46+
"\n",
47+
" img1 = img1.astype(np.float64)\n",
48+
" img2 = img2.astype(np.float64)\n",
49+
" kernel = cv2.getGaussianKernel(11, 1.5)\n",
50+
" window = np.outer(kernel, kernel.transpose())\n",
51+
"\n",
52+
" mu1 = cv2.filter2D(img1, -1, window)[5:-5, 5:-5] # valid\n",
53+
" mu2 = cv2.filter2D(img2, -1, window)[5:-5, 5:-5]\n",
54+
" mu1_sq = mu1**2\n",
55+
" mu2_sq = mu2**2\n",
56+
" mu1_mu2 = mu1 * mu2\n",
57+
" sigma1_sq = cv2.filter2D(img1**2, -1, window)[5:-5, 5:-5] - mu1_sq\n",
58+
" sigma2_sq = cv2.filter2D(img2**2, -1, window)[5:-5, 5:-5] - mu2_sq\n",
59+
" sigma12 = cv2.filter2D(img1 * img2, -1, window)[5:-5, 5:-5] - mu1_mu2\n",
60+
"\n",
61+
" ssim_map = ((2 * mu1_mu2 + C1) * (2 * sigma12 + C2)) / ((mu1_sq + mu2_sq + C1) *\n",
62+
" (sigma1_sq + sigma2_sq + C2))\n",
63+
" return ssim_map.mean()\n",
64+
"\n",
65+
"\n",
66+
"def calculate_ssim(img1, img2):\n",
67+
" '''calculate SSIM\n",
68+
" the same outputs as MATLAB's\n",
69+
" img1, img2: [0, 255]\n",
70+
" '''\n",
71+
" if not img1.shape == img2.shape:\n",
72+
" raise ValueError('Input images must have the same dimensions.')\n",
73+
" if img1.ndim == 2:\n",
74+
" return ssim(img1, img2)\n",
75+
" elif img1.ndim == 3:\n",
76+
" if img1.shape[2] == 3:\n",
77+
" ssims = []\n",
78+
" for i in range(3):\n",
79+
" ssims.append(ssim(img1, img2))\n",
80+
" return np.array(ssims).mean()\n",
81+
" elif img1.shape[2] == 1:\n",
82+
" return ssim(np.squeeze(img1), np.squeeze(img2))\n",
83+
" else:\n",
84+
" raise ValueError('Wrong input image dimensions.')"
85+
]
86+
},
87+
{
88+
"cell_type": "code",
89+
"execution_count": 4,
90+
"metadata": {
91+
"collapsed": false
92+
},
93+
"outputs": [],
94+
"source": [
95+
"def calculate(path1, path2):\n",
96+
" img1=Image.open(path1)\n",
97+
" img2=Image.open(path2)\n",
98+
"\n",
99+
" img1_array = np.array(img1)\n",
100+
" img2_array = np.array(img2)\n",
101+
"\n",
102+
" result_psnr = calculate_psnr(img1_array,img2_array)\n",
103+
" result_ssim = calculate_ssim(img1_array, img2_array)\n",
104+
"\n",
105+
" print('PSNR: {}'.format(result_psnr))\n",
106+
" print('SSIM: {}'.format(result_ssim))"
107+
]
108+
},
109+
{
110+
"cell_type": "markdown",
111+
"metadata": {},
112+
"source": [
113+
"**🅰path1 为超分结果图** \n",
114+
"**🅱path2 为GT图**"
115+
]
116+
},
117+
{
118+
"cell_type": "code",
119+
"execution_count": 5,
120+
"metadata": {
121+
"collapsed": false
122+
},
123+
"outputs": [
124+
{
125+
"name": "stdout",
126+
"output_type": "stream",
127+
"text": [
128+
"PSNR: 28.941417753051944\n",
129+
"SSIM: 0.8072565770834261\n"
130+
]
131+
}
132+
],
133+
"source": [
134+
"path1 = r\"D:\\A_result_compare\\HuangRW.tif\"\n",
135+
"path2 = r\"D:\\GraduationProjectBackUp\\Result_compare\\95\\95-2048pix-speed3-ave1.tif\" \n",
136+
"\n",
137+
"calculate(path1, path2)"
138+
]
139+
},
140+
{
141+
"cell_type": "code",
142+
"execution_count": null,
143+
"metadata": {
144+
"collapsed": true
145+
},
146+
"outputs": [],
147+
"source": []
148+
}
149+
],
150+
"metadata": {
151+
"anaconda-cloud": {},
152+
"kernelspec": {
153+
"display_name": "Python [default]",
154+
"language": "python",
155+
"name": "python3"
156+
},
157+
"language_info": {
158+
"codemirror_mode": {
159+
"name": "ipython",
160+
"version": 3
161+
},
162+
"file_extension": ".py",
163+
"mimetype": "text/x-python",
164+
"name": "python",
165+
"nbconvert_exporter": "python",
166+
"pygments_lexer": "ipython3",
167+
"version": "3.5.6"
168+
}
169+
},
170+
"nbformat": 4,
171+
"nbformat_minor": 1
172+
}

Pretreatment/calculate_PSNR_SSIM.py

+79
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
import math
2+
import numpy as np
3+
import cv2
4+
from PIL import Image
5+
import argparse
6+
# Training settings
7+
parserI = argparse.ArgumentParser(description='Calculate PSNR and SSIM')
8+
parserI.add_argument('--SR', type=str, default=' ', help='your SR img')
9+
parserI.add_argument('--GT', type=str, default=' ', help='the GT img')
10+
optI = parserI.parse_args()
11+
print(optI)
12+
13+
def calculate_psnr(img1, img2):
14+
# img1 and img2 have range [0, 255]
15+
img1 = img1.astype(np.float64)
16+
img2 = img2.astype(np.float64)
17+
mse = np.mean((img1 - img2)**2)
18+
if mse == 0:
19+
return float('inf')
20+
return 20 * math.log10(255.0 / math.sqrt(mse))
21+
22+
def ssim(img1, img2):
23+
C1 = (0.01 * 255)**2
24+
C2 = (0.03 * 255)**2
25+
26+
img1 = img1.astype(np.float64)
27+
img2 = img2.astype(np.float64)
28+
kernel = cv2.getGaussianKernel(11, 1.5)
29+
window = np.outer(kernel, kernel.transpose())
30+
31+
mu1 = cv2.filter2D(img1, -1, window)[5:-5, 5:-5] # valid
32+
mu2 = cv2.filter2D(img2, -1, window)[5:-5, 5:-5]
33+
mu1_sq = mu1**2
34+
mu2_sq = mu2**2
35+
mu1_mu2 = mu1 * mu2
36+
sigma1_sq = cv2.filter2D(img1**2, -1, window)[5:-5, 5:-5] - mu1_sq
37+
sigma2_sq = cv2.filter2D(img2**2, -1, window)[5:-5, 5:-5] - mu2_sq
38+
sigma12 = cv2.filter2D(img1 * img2, -1, window)[5:-5, 5:-5] - mu1_mu2
39+
40+
ssim_map = ((2 * mu1_mu2 + C1) * (2 * sigma12 + C2)) / ((mu1_sq + mu2_sq + C1) *
41+
(sigma1_sq + sigma2_sq + C2))
42+
return ssim_map.mean()
43+
44+
45+
def calculate_ssim(img1, img2):
46+
'''calculate SSIM
47+
the same outputs as MATLAB's
48+
img1, img2: [0, 255]
49+
'''
50+
if not img1.shape == img2.shape:
51+
raise ValueError('Input images must have the same dimensions.')
52+
if img1.ndim == 2:
53+
return ssim(img1, img2)
54+
elif img1.ndim == 3:
55+
if img1.shape[2] == 3:
56+
ssims = []
57+
for i in range(3):
58+
ssims.append(ssim(img1, img2))
59+
return np.array(ssims).mean()
60+
elif img1.shape[2] == 1:
61+
return ssim(np.squeeze(img1), np.squeeze(img2))
62+
else:
63+
raise ValueError('Wrong input image dimensions.')
64+
65+
def calculate(path1, path2):
66+
img1=Image.open(path1)
67+
img2=Image.open(path2)
68+
69+
img1_array = np.array(img1)
70+
img2_array = np.array(img2)
71+
72+
result_psnr = calculate_psnr(img1_array,img2_array)
73+
result_ssim = calculate_ssim(img1_array, img2_array)
74+
75+
print('PSNR: {}'.format(result_psnr))
76+
print('SSIM: {}'.format(result_ssim))
77+
78+
if __name__ == '__main__':
79+
calculate(optI.SR, optI.GT)

0 commit comments

Comments
 (0)