將兩張圖都轉成float64進行計算,且歸一化到0~1,防止精度丟失
import numpy as np
def getpsnr(img1,bitdepth1,img2,bitdepth2):
img1 = np.float64(img1) / (2**bitdepth1-1)
img2 = np.float64(img2) / (2**bitdepth2-1)
mse = np.mean(np.square(img1-img2))
psnr = - 10 * np.log10(mse)
return psnr