图像质量评价指标之 PSNR(峰值信噪比) python实现

PSNR(峰值信噪比)是单图像超分辨率重建中评价图像质量的一项重要指标,下面给出几种基于python语言的实现方法!

公式推导:

给定一个大小为 m×n 的干净图像 I 和噪声图像 K ,均方误差 (MSE) 定义为:

                                             

PSNR(dB) 就定义为:             

方法一:纯公式计算

import cv2
import numpy as np
import math
import os


def psnr1(img1, img2):
   mse = np.mean((img1/1.0 - img2/1.0) ** 2 )
   if mse < 1.0e-10:
      return 100
   return 10 * math.log10(255.0**2/mse)
 
def psnr2(img1, img2):
   mse = np.mean( (img1/255. - img2/255.) ** 2 )
   if mse < 1.0e-10:
      return 100
   PIXEL_MAX = 1
   return 20 * math.log10(PIXEL_MAX / math.sqrt(mse))

方法二:基于TensorFlow方法

    # Read images from file.
    im1 = tf.decode_png('path/to/im1.png')
    im2 = tf.decode_png('path/to/im2.png')
    # Compute PSNR over tf.uint8 Tensors.
    psnr1 = tf.image.psnr(im1, im2, max_val=255)

    # Compute PSNR over tf.float32 Tensors.
    im1 = tf.image.convert_image_dtype(im1, tf.float32)
    im2 = tf.image.convert_image_dtype(im2, tf.float32)
    psnr2 = tf.image.psnr(im1, im2, max_val=1.0)
    # psnr1 and psnr2 both have type tf.float32 and are almost equal.

方法三:基于skimage方法

from skimage.measure import compare_psnr
psnr = compare_psnr(im1, im2, 255)

 

 

 

 

 

 

 

 

 

你可能感兴趣的:(知识点)