[深度学习] [Tensorflow] Tensorflow 中计算 PSNR 峰值信噪比

版权声明:博客地址:https://blog.csdn.net/weixin_41028208,未经博主允许不得转载。QQ:335848046。微博:小黑_songrn。 https://blog.csdn.net/weixin_41028208/article/details/83999476

峰值信噪比,Peak signal-to-noise ratio(PSNR)是测量有损压缩编/解码器的重建质量的重要指标,在图像处理领域很常见,因为在图像压缩处理过程中,常常会引入噪声,这些噪声就会影响图像重建质量,对于图像重建,较高的PSNR指标通常表明重建质量较高,图像失真越小。

定义

原图像与被处理图像之间的均方误差相对于(2n-1)2的对数值(信号最大值的平方,n是每个采样值的比特数,例如灰度图像就是8比特,所以MAX值是255),它的单位是dB。

在这里插入图片描述

MSE是均方误差

在这里插入图片描述

在Tensorflow 1.8版本,Tensorflow加入了计算psnr的API

tf.image.psnr

tf.image.psnr(
    a,
    b,
    max_val,
    name=None
)
返回A与B的PSNR值,一般情况下max_val=255(图像数值的动态范围)

官方示例

    # 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.

代码实现,地址 https://github.com/tensorflow/tensorflow/blob/r1.12/tensorflow/python/ops/image_ops_impl.py

@tf_export('image.psnr')
def psnr(a, b, max_val, name=None):
    with ops.name_scope(name, 'PSNR', [a, b]):
    # Need to convert the images to float32.  Scale max_val accordingly so that
    # PSNR is computed correctly.
        max_val = math_ops.cast(max_val, a.dtype)
        max_val = convert_image_dtype(max_val, dtypes.float32)
        a = convert_image_dtype(a, dtypes.float32)
        b = convert_image_dtype(b, dtypes.float32)
        mse = math_ops.reduce_mean(math_ops.squared_difference(a, b), [-3, -2, -1])
        psnr_val = math_ops.subtract(20 * math_ops.log(max_val) / math_ops.log(10.0), np.float32(10 / np.log(10)) * math_ops.log(mse), name='psnr')
        _, _, checks = _verify_compatible_image_shapes(a, b)
    with ops.control_dependencies(checks):
        return array_ops.identity(psnr_val)

猜你喜欢

转载自blog.csdn.net/weixin_41028208/article/details/83999476
今日推荐