-
Notifications
You must be signed in to change notification settings - Fork 31
/
calculate_psnr_ssim.py
26 lines (22 loc) · 1.1 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
import os
import cv2
from utils.metrics import calculate_psnr, calculate_ssim
# Sample script to calculate PSNR and SSIM metrics from saved images in two directories
# using calculate_psnr and calculate_ssim functions from: https://github.com/JingyunLiang/SwinIR
gt_path = '/PATH/TO/GROUND_TRUTH/'
results_path = '/PATH/TO/MODEL_OUTPUTS/'
imgsName = sorted(os.listdir(results_path))
gtsName = sorted(os.listdir(gt_path))
assert len(imgsName) == len(gtsName)
cumulative_psnr, cumulative_ssim = 0, 0
for i in range(len(imgsName)):
print('Processing image: %s' % (imgsName[i]))
res = cv2.imread(os.path.join(results_path, imgsName[i]), cv2.IMREAD_COLOR)
gt = cv2.imread(os.path.join(gt_path, gtsName[i]), cv2.IMREAD_COLOR)
cur_psnr = calculate_psnr(res, gt, test_y_channel=True)
cur_ssim = calculate_ssim(res, gt, test_y_channel=True)
print('PSNR is %.4f and SSIM is %.4f' % (cur_psnr, cur_ssim))
cumulative_psnr += cur_psnr
cumulative_ssim += cur_ssim
print('Testing set, PSNR is %.4f and SSIM is %.4f' % (cumulative_psnr / len(imgsName), cumulative_ssim / len(imgsName)))
print(results_path)