写在前面

ssim作为图像质量评价指标,在很多图像领域如图像超分辨率、图像压缩、图像去噪等都有广泛的应用。

SSIM(结构相似性)

简介

结构相似性指标(英文:structural similarity index measure,简称SSIM)是一种用以衡量两张数位影像相似程度的指标。当两张影像其中一张为无失真影像,另一张为失真后的影像,二者的结构相似性可以看成是失真影像的影像品质衡量指标。相较于传统所使用的影像品质衡量指标,像是峰值信噪比(英语:PSNR),结构相似性在影像品质的衡量上更能符合人眼对影像品质的判断。(原始论文:下载

定义

给定两个信号 x {\displaystyle \mathbf {x} } x y {\displaystyle \mathbf {y} } y,两者的结构相似性定义为:
SSIM ( x , y ) = [ l ( x , y ) ] α [ c ( x , y ) ] β [ s ( x , y ) ] γ , {\displaystyle {\text{SSIM}}(\mathbf {x} ,\mathbf {y} )=[l(\mathbf {x} ,\mathbf {y} )]^{\alpha }[c(\mathbf {x} ,\mathbf {y} )]^{\beta }[s(\mathbf {x} ,\mathbf {y} )]^{\gamma }}, SSIM(x,y)=[l(x,y)]α[c(x,y)]β[s(x,y)]γ
l ( x , y ) = 2 μ x μ y + C 1 μ x 2 + μ y 2 + C 1 , c ( x , y ) = 2 σ x σ y + C 2 σ x 2 + σ y 2 + C 2 , s ( x , y ) = σ x y + C 3 σ x σ y + C 3 。 {\displaystyle l(\mathbf {x} ,\mathbf {y} )={\frac {2\mu _{x}\mu _{y}+C_{1}}{\mu _{x}^{2}+\mu _{y}^{2}+C_{1}}}},{\displaystyle c(\mathbf {x} ,\mathbf {y} )={\frac {2\sigma _{x}\sigma _{y}+C_{2}}{\sigma _{x}^{2}+\sigma _{y}^{2}+C_{2}}}},{\displaystyle s(\mathbf {x} ,\mathbf {y} )={\frac {\sigma _{xy}+C_{3}}{\sigma _{x}\sigma _{y}+C_{3}}}}。 l(x,y)=μx2+μy2+C12μxμy+C1c(x,y)=σx2+σy2+C22σxσy+C2s(x,y)=σxσy+C3σxy+C3
其中, l ( x , y ) {\displaystyle l(\mathbf {x} ,\mathbf {y})} l(x,y)比较 x {\displaystyle \mathbf {x} } x y {\displaystyle \mathbf {y} } y 的亮度, c ( x , y ) {\displaystyle c(\mathbf {x} ,\mathbf {y} )} c(x,y)比较 x {\displaystyle \mathbf {x} } x y {\displaystyle \mathbf {y} } y 的对比度, s ( x , y ) {\displaystyle s(\mathbf {x} ,\mathbf {y} )} s(x,y)比较 x {\displaystyle \mathbf {x} } x y {\displaystyle \mathbf {y} } y 的结构(structure), α > 0 {\displaystyle \alpha >0} α>0 β > 0 {\displaystyle \beta >0} β>0 γ > 0 {\displaystyle \gamma >0} γ>0,为调整 l ( x , y ) {\displaystyle l(\mathbf {x} ,\mathbf {y} )} l(x,y) c ( x , y ) {\displaystyle c(\mathbf {x} ,\mathbf {y} )} c(x,y) s ( x , y ) {\displaystyle s(\mathbf {x} ,\mathbf {y} )} s(x,y)相对重要性的参数, μ x {\displaystyle \mu _{x}} μx μ y {\displaystyle \mu _{y}} μy σ x {\displaystyle \sigma _{x}} σx σ y {\displaystyle \sigma _{y}} σy分别为 x {\displaystyle \mathbf {x} } x y {\displaystyle \mathbf {y} } y 的平均值和标准差, σ x y {\displaystyle \sigma _{xy}} σxy x {\displaystyle \mathbf {x} } x y {\displaystyle \mathbf {y} } y 的协方差, C 1 {\displaystyle C_{1}} C1 C 2 {\displaystyle C_{2}} C2 C 3 {\displaystyle C_{3}} C3皆为常数,用以维持 l ( x , y ) 、 c ( x , y ) 、 s ( x , y ) {\displaystyle l(\mathbf {x} ,\mathbf {y} )}、{\displaystyle c(\mathbf {x} ,\mathbf {y} )}、{\displaystyle s(\mathbf {x} ,\mathbf {y} )} l(x,y)c(x,y)s(x,y)的稳定。

结构相似性指标的值越大,代表两个信号的相似性越高。

试想,若使用全等的两张图片去做SSIM运算,也就是说 μ x = μ y {\displaystyle \mu _{x}=\mu _{y}} μx=μy σ x = σ y {\displaystyle \sigma _{x}=\sigma _{y}} σx=σy,如此一来
SSIM ( x , x ) = 2 μ x 2 + C 1 μ x 2 + μ x 2 + C 1 × 2 σ x 2 + C 2 σ x 2 + σ x 2 + C 2 × σ x x + C 3 σ x σ x + C 3 = 1 {\displaystyle {\text{SSIM}}(\mathbf {x} ,\mathbf {x} )={\frac {2\mu _{x}^{2}+C_{1}}{\mu _{x}^{2}+\mu _{x}^{2}+C_{1}}}\times {\frac {2\sigma _{x}^{2}+C_{2}}{\sigma _{x}^{2}+\sigma _{x}^{2}+C_{2}}}\times {\frac {\sigma _{xx}+C_{3}}{\sigma _{x}\sigma _{x}+C_{3}}}=1} SSIM(x,x)=μx2+μx2+C12μx2+C1×σx2+σx2+C22σx2+C2×σxσx+C3σxx+C3=1

性质

结构相似性指标具有下列性质:

  • 结构相似性指标是对称的,亦即: SSIM ( x , y ) = SSIM ( y , x ) {\displaystyle {\text{SSIM}}(\mathbf {x} ,\mathbf {y} )={\text{SSIM}}(\mathbf {y} ,\mathbf {x} )} SSIM(x,y)=SSIM(y,x)
  • 上下界:结构相似性指标的范围为−1到1。当衡量的两个信号完全相同时,结构相似性指标的值为1。

使用

实际使用时,简化起见,一般会将参数设为 α = β = γ = 1 {\displaystyle \alpha =\beta =\gamma =1} α=β=γ=1 C 3 = C 2 / 2 {\displaystyle C_{3}=C_{2}/2} C3=C2/2,得到:
SSIM ( x , y ) = ( 2 μ x μ y + C 1 ) ( 2 σ x y + C 2 ) ( μ x 2 + μ y 2 + C 1 ) ( σ x 2 + σ y 2 + C 2 ) 。 {\displaystyle {\text{SSIM}}(\mathbf {x} ,\mathbf {y} )={\frac {(2\mu _{x}\mu _{y}+C_{1})(2\sigma _{xy}+C_{2})}{(\mu _{x}^{2}+\mu _{y}^{2}+C_{1})(\sigma _{x}^{2}+\sigma _{y}^{2}+C_{2})}}}。 SSIM(x,y)=(μx2+μy2+C1)(σx2+σy2+C2)(2μxμy+C1)(2σxy+C2)
在计算两张影像的结构相似性指标时,会开一个局部性的视窗,一般为 N × N N×N N×N的小区块,计算出视窗内信号的结构相似性指标,每次以像素为单位移动视窗,直到整张影像每个位置的局部结构相似性指标都计算完毕。将全部的局部结构相似性指标平均起来即为两张影像的结构相似性指标。

实现

太复杂了,不想写,😔。我们可以调用第三方库skimage实现,里面有封装好的计算ssim的代码,具体调用方式如下:

from skimage.metrics import structural_similarity as ssim
from PIL import Image
import numpy as np


img1 = np.array(Image.open('original.jpg'))
img2 = np.array(Image.open('compress.jpg'))


if __name__ == "__main__":
	# If the input is a multichannel (color) image, set multichannel=True.
    print(ssim(img1, img2, multichannel=True))

备注:skimage的大名叫scikit-image,安装请用pip install scikit-image

对比

在这里插入图片描述

import numpy as np
import matplotlib.pyplot as plt

from skimage import data, img_as_float
from skimage.metrics import structural_similarity as ssim
from skimage.metrics import mean_squared_error


img = img_as_float(data.camera())
rows, cols = img.shape

noise = np.ones_like(img) * 0.2 * (img.max() - img.min())
noise[np.random.random(size=noise.shape) > 0.5] *= -1

img_noise = img + noise
img_const = img + abs(noise)

fig, axes = plt.subplots(nrows=1, ncols=3, figsize=(10, 4),
                         sharex=True, sharey=True)
ax = axes.ravel()

mse_none = mean_squared_error(img, img)
ssim_none = ssim(img, img, data_range=img.max() - img.min())

mse_noise = mean_squared_error(img, img_noise)
ssim_noise = ssim(img, img_noise,
                  data_range=img_noise.max() - img_noise.min())

mse_const = mean_squared_error(img, img_const)
ssim_const = ssim(img, img_const,
                  data_range=img_const.max() - img_const.min())

label = 'MSE: {:.2f}, SSIM: {:.2f}'

ax[0].imshow(img, cmap=plt.cm.gray, vmin=0, vmax=1)
ax[0].set_xlabel(label.format(mse_none, ssim_none))
ax[0].set_title('Original image')

ax[1].imshow(img_noise, cmap=plt.cm.gray, vmin=0, vmax=1)
ax[1].set_xlabel(label.format(mse_noise, ssim_noise))
ax[1].set_title('Image with noise')

ax[2].imshow(img_const, cmap=plt.cm.gray, vmin=0, vmax=1)
ax[2].set_xlabel(label.format(mse_const, ssim_const))
ax[2].set_title('Image plus constant')

plt.tight_layout()
plt.show()

拓展阅读

ssim的matlab实现:ssim_index.m

相关推荐

【python】psnr原理简介及代码实现

引用参考

https://zh.wikipedia.org/wiki/結構相似性
https://ece.uwaterloo.ca/~z70wang/research/ssim/
https://scikit-image.org/docs/dev/auto_examples/transform/plot_ssim.html

Logo

旨在为数千万中国开发者提供一个无缝且高效的云端环境,以支持学习、使用和贡献开源项目。

更多推荐