图像计算常用的指标 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:
        return 100
    PIXEL_MAX = 255.0
    return 20 * 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
from skimage.measure 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)
    return np.mean(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)
    return np.mean(psnr)
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
  • 8
    点赞
  • 65
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
下面是MATLAB图像复原中常用的一些评价指标的简单代码实现: 1. PSNR(峰值信噪比) ```matlab function psnr_value = psnr(original_image, restored_image) mse_value = mean(mean((double(original_image) - double(restored_image)).^2)); if(mse_value == 0) psnr_value = 100; else psnr_value = 10*log10(255^2/mse_value); end end ``` 2. SSIM(结构相似性) ```matlab function ssim_value = ssim(original_image, restored_image) k1 = 0.01; k2 = 0.03; L = 255; C1 = (k1*L)^2; C2 = (k2*L)^2; mu1 = mean2(original_image); mu2 = mean2(restored_image); sigma1 = std2(original_image); sigma2 = std2(restored_image); sigma12 = cov2(original_image,restored_image); numerator = (2*mu1*mu2 + C1)*(2*sigma12 + C2); denominator = (mu1^2 + mu2^2 + C1)*(sigma1^2 + sigma2^2 + C2); ssim_value = numerator/denominator; end ``` 3. MSE(均方误差) ```matlab function mse_value = mse(original_image, restored_image) mse_value = mean(mean((double(original_image) - double(restored_image)).^2)); end ``` 4. MAE(平均绝对误差) ```matlab function mae_value = mae(original_image, restored_image) mae_value = mean(mean(abs(double(original_image) - double(restored_image)))); end ``` 5. SNR(信噪比) ```matlab function snr_value = snr(original_image, restored_image) mse_value = mean(mean((double(original_image) - double(restored_image)).^2)); snr_value = 10*log10(mean(mean(double(original_image).^2))/mse_value); end ``` 6. ISNR(改进信噪比) ```matlab function isnr_value = isnr(original_image, restored_image, noise_image) mse_restored = mean(mean((double(original_image) - double(restored_image)).^2)); mse_noise = mean(mean((double(original_image) - double(noise_image)).^2)); isnr_value = 10*log10(mse_noise/mse_restored); end ```

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

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值