Python 不同分辨率图像峰值信噪比[PSNR]

PNNR:全称为“Peak Signal-to-Noise Ratio”,中文直译为峰值信噪比

前言

峰值信噪比是一种衡量图像质量的指标,描述的是最大值信号与背景噪音之间的关系。

一般来说,PSNR高于40dB说明图像质量极好(即非常接近原始图像);在30—40dB通常表示图像质量是好的(即失真可以察觉但可以接受);在20—30dB说明图像质量差;低于20dB图像不可接受。

一、定义

对于两个m*n的单色图像XY,其均方误差(MSE)定义为

                                Python 不同分辨率图像峰值信噪比[PSNR]

峰值信噪比(PSNR)定义为

                                Python 不同分辨率图像峰值信噪比[PSNR]

其中MAX_{I}表示图像像素点的最大值,如果每个采样点用8位表示,那么最大值就是255。根据定义可知MSE越小,则PSNR越大,所以PSNR越大,代表图像质量越好。

针对彩色图像,通常有三种方法计算

1、分别计算RGB三个通道的PSNR,然后取平均值

2、计算RGB三通道的MSE,然后除以3

3、将图片转化为YCbCr格式,然后只计算Y分量(亮度分量)的PSNR

二、Python代码

1.自定义

import cv2 as cv 
import math
import numpy as np
 
def psnr1(img1,img2):
    #compute mse
    # mse = np.mean((img1-img2)**2)
    mse = np.mean((img1/1.0-img2/1.0)**2)
    #compute psnr
    if mse < 1e-10:
        return 100
    psnr1 = 20*math.log10(255/math.sqrt(mse))
    return psnr1

#像素归一化
def psnr2(img1,img2):
    mse = np.mean((img1/255.0-img2/255.0)**2)
    if mse < 1e-10:
        return 100
    psnr2 = 20*math.log10(1/math.sqrt(mse))
    return psnr2
 
imag1 = cv.imread("C:/Users/Server/Desktop/1.jpg")
imag2 = cv.imread("C:/Users/Server/Desktop/2.jpg")
print (imag1.shape)
#print(imag2.shape)
# imag2 = imag2.reshape(352,352,3)
#print(imag2.shape)
res1 = psnr1(imag1,imag2)
print("res1:",res1)
res2 = psnr2(imag1,imag2)
print("res2:",res2)

2.TensorFlow

'''
compute PSNR with tensorflow
'''
import tensorflow as tf

def read_img(path):
	return tf.image.decode_image(tf.read_file(path))

def psnr(tf_img1, tf_img2):
	return tf.image.psnr(tf_img1, tf_img2, max_val=255)

def _main():
	t1 = read_img('1.jpg')
	t2 = read_img('2.jpg')
	with tf.Session() as sess:
		sess.run(tf.global_variables_initializer())
		y = sess.run(psnr(t1, t2))
		print(y)
 
if __name__ == '__main__':
    _main()

总结

为了更好地展示结果,以下为两张图片的PSNR。

Python 不同分辨率图像峰值信噪比[PSNR]Python 不同分辨率图像峰值信噪比[PSNR]

1(女)与1(女)的PSNR: 100
1(女)与2(男)的PSNR: 6.534605344887611

共计人评分,平均

到目前为止还没有投票!成为第一位评论此文章。

(0)
青葱年少的头像青葱年少普通用户
上一篇 2023年3月11日
下一篇 2023年3月11日

相关推荐