PSNR(Peak Signal to Noise Ratio)峰值信噪比
通常用来评价一幅图像压缩后和原图像相比质量的好坏,PSNR越高,压缩后失真越小。
M
S
E
=
1
H
×
W
∑
i
=
1
H
∑
j
=
1
W
(
X
(
i
,
j
)
−
Y
(
i
,
j
)
)
2
MSE=\frac{1}{H\times W}\sum_{i=1}^{H}\sum_{j=1}^{W}\left ( X\left (i,j \right )-Y\left ( i,j \right )\right )^{2}
MSE=H×W1i=1∑Hj=1∑W(X(i,j)−Y(i,j))2
MSE表示当前图像X和参考图像Y的均方误差(Mean Square Error),H,W为图像的高和宽。
P
S
N
R
=
10
log
10
(
(
2
n
−
1
)
2
M
S
E
)
PSNR=10\log_{10}\left ( \frac{\left (2^{n} -1 \right )^{2}}{MSE} \right )
PSNR=10log10(MSE(2n−1)2)
n为像素比特数,一般取8,即像素灰阶数为256,单位是dB,数值越大表示失真越小。
![]() | ![]() | ![]() |
---|---|---|
原始图像 | 图像宽高分别缩小1/2再放大到原图 PSNR=30.2dB | 图像宽高分别缩小1/5再放大到原图 PSNR=24.5dB |
tf.image.psnr
tf.image.psnr(a, b, max_val, name=None)
#返回a和b之间的峰值信噪比.
#这旨在用于信号(或图像).在批处理中为每个图像生成一个信噪比(PSNR)值.
#输入的最后三个维度预计为[height, width, depth].
a:第一组图像; b:第二组图像.
max_val:图像的动态范围(即最大允许值和最小允许值之间的差值).
name:用于嵌入计算的命名空间.
示例
# 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.
返回:
a和b之间的标量PSNR,返回的张量具有tf.float32类型和形状[batch_size,1].
SSIM(Structual Similarity)结构相似性
分别从亮度、对比度、结构三方面度量图像相似性。
SSIM取值范围[0,1],值越大,表示图像失真越小。
tf.image.ssim
tf.image.ssim(img1, img2, max_val)
#img1:第一批图像. img2:第二批图像.
#max_val:图像的动态范围(即最大允许值与最小允许值之间的差值).
#由于过滤器尺寸的原因,图像尺寸必须至少为11x11.
示例
# Read images from file.
im1 = tf.decode_png('path/to/im1.png')
im2 = tf.decode_png('path/to/im2.png')
# Compute SSIM over tf.uint8 Tensors.
ssim1 = tf.image.ssim(im1, im2, max_val=255)
# Compute SSIM over tf.float32 Tensors.
im1 = tf.image.convert_image_dtype(im1, tf.float32)
im2 = tf.image.convert_image_dtype(im2, tf.float32)
ssim2 = tf.image.ssim(im1, im2, max_val=1.0)
# ssim1 and ssim2 both have type tf.float32 and are almost equal.
返回:
一个包含批处理中每个图像的SSIM值的张量.返回的SSIM值在范围(-1,1)中,
当像素值非负时返回形状为:broadcast(img1.shape [: - 3],img2.shape [: - 3])的张量