# python实现指定路径下的图像psnr和ssim平均值计算，并且计算程序运行时间

import os
import numpy as np
import math
import cv2

import time

start = time.clock()

#当中是你的程序

def psnr(img1, img2):
mse = np.mean((img1/1. - img2/1.) ** 2 )
if mse < 1.0e-10:
return 100*1.0
return 10 * math.log10(255.0*255.0/mse)

def mse(img1,img2):
mse = np.mean((img1/1. - img2/1.) ** 2 )
return mse

def ssim(y_true , y_pred):
u_true = np.mean(y_true)
u_pred = np.mean(y_pred)
var_true = np.var(y_true)
var_pred = np.var(y_pred)
std_true = np.sqrt(var_true)
std_pred = np.sqrt(var_pred)
c1 = np.square(0.01*7)
c2 = np.square(0.03*7)
ssim = (2 * u_true * u_pred + c1) * (2 * std_pred * std_true + c2)
denom = (u_true ** 2 + u_pred ** 2 + c1) * (var_pred + var_true + c2)
return ssim / denom

path1 = 'D:/sy_imags1000_80lun/outputs/'  #指定输出结果文件夹
path2 = 'D:/sy_imags1000_80lun/targets/'#指定原图文件夹
f_nums = len(os.listdir(path1))
list_psnr = []
list_ssim = []
list_mse = []
for i in range(1,f_nums+1):
img_a = cv2.imread(path1+str(i)+'-outputs.png')
img_b = cv2.imread(path2+str(i)+'-targets.png')
psnr_num = psnr(img_a, img_b)
ssim_num = ssim(img_a, img_b)
mse_num = mse(img_a,img_b)
list_ssim.append(ssim_num)
list_psnr.append(psnr_num)
list_mse.append(mse_num)
print("平均PSNR:",np.mean(list_psnr))#,list_psnr)
print("平均SSIM:",np.mean(list_ssim))#,list_ssim)
print("平均MSE:",np.mean(list_mse))#,list_mse)

elapsed = (time.clock() - start)
print("Time used:",elapsed)

• 5
点赞
• 24
收藏
觉得还不错? 一键收藏
• 打赏
• 10
评论
10-28
12-07
03-02 2499
10-10 6534
02-24 475
09-02
10-30
10-26
06-13 3013

### “相关推荐”对你有帮助么？

• 非常没帮助
• 没帮助
• 一般
• 有帮助
• 非常有帮助

¥1 ¥2 ¥4 ¥6 ¥10 ¥20

1.余额是钱包充值的虚拟货币，按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载，可以购买VIP、付费专栏及课程。