图像计算常⽤的指标PSNR,MAE,MSE,SSIM(python代码)图像计算常⽤的指标 PSNR, MAE, MSE, SSIM(python 代码)
import numpy  as np
import math
def psnr(img1, img2):
mse = np.mean((img1 - img2)**2)
if mse ==0:
return100
PIXEL_MAX =255.0
return20* math.log10(PIXEL_MAX / math.sqrt(mse))
def mse(img1, img2):
mse = np.mean((img1 - img2)**2)
return mse
def mae(img1, img2):
mae = np.mean(abs(img1 - img2))
return mae
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
## use the scikit package
asure import compare_ssim as ssim
ssim(img1,img2)# for gray image
ssim(img1,img1,multichannel=True)## for rgb
batch train in tensorflow
mse = np.square(x - gx).sum()
def batch_mae_frame_float(gen_frames, gt_frames):
# [batch, width, height] or [batch, width, height, channel]
if gen_frames.ndim ==3:
axis =(1,2)
elif gen_frames.ndim ==4:
axis =(1,2,3)
x = np.float32(gen_frames)
y = np.float32(gt_frames)
mae = np.sum(np.absolute(x - y), axis=axis, dtype=np.float32)
an(mae)
def batch_psnr(gen_frames, gt_frames):
# [batch, width, height] or [batch, width, height, channel]
if gen_frames.ndim ==3:
axis =(1,2)
elif gen_frames.ndim ==4:
axis =(1,2,3)
x = np.int32(gen_frames)
y = np.int32(gt_frames)
num_pixels =float(np.size(gen_frames[0]))
mse = np.sum((x - y)**2, axis=axis, dtype=np.float32)/ num_pixels
psnr =20* np.log10(255)-10* np.log10(mse)
an(psnr)
python代码转换for b in range(configs.batch_size):
score, _ = compare_ssim(gx[b], x[b], full=True, multichannel=True)
sim[i]+= score
# batch_id means the number of all the batch used in the test process
frame_mse = img_mse[i]/(batch_id * configs.batch_size * configs.n_gpu))# i means the frame index ssim = np.asarray(ssim, dtype=np.float32)/(configs.batch_size * batch_id)
psnr = np.asarray(psnr, dtype=np.float32)/ batch_id
fmae = np.asarray(fmae, dtype=np.float32)/ batch_id

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。