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

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

# 前言

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

# 一、定义

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

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 psnr(tf_img1, tf_img2):
return tf.image.psnr(tf_img1, tf_img2, max_val=255)

def _main():
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
y = sess.run(psnr(t1, t2))
print(y)

if __name__ == '__main__':
_main()``````

# 总结

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

THE END