SSIM (Structure Similarity Index Measure) 结构衡量指标+代码

介绍

结构相似指标可以衡量图片的失真程度,也可以衡量两张图片的相似程度。与MSE和PSNR衡量绝对误差不同,SSIM是感知模型,即更符合人眼的直观感受。

同样MSE下,不同SSIM展现的图片结果:

SSIM的取值范围[-1, 1], 具有对成性,边界性,唯一最大性(当且仅当x=y时SSIM=1),是一种距离公式。

SSIM理论

SSIM 主要考量图片的三个关键特征:亮度(Luminance), 对比度(Contrast), 结构 (Structure)

Luminance

亮度以平均灰度衡量,通过平均所有像素的值得到。

对比函数

 

Contrast

对比度通过灰度标准差来衡量。标准差无偏估计:

相应对比函数

 

Structure

结构对比比较的是经过归一化后的​​​​比较, 即可以用相关性系数衡量

 

C1 常量避免l(x,y) 的值接近0时不稳定。 C2, C3 同。

经验常取 K1=0.01, K2=0.03

L像素动态取值范围 

SSIM

 

 分别代表了不同特征在SSIM衡量中的占比,当都为1时,有:

 

SSIM的实践常用方法MSSIM (Mean SSIM)

实际上,当需要衡量一整张图片的质量,经常使用的是以一个一个窗口计算SSIM然后求平均。

当我们用一个一个block去计算平均值,标准差,协方差时,这种方法容易造成 blocking artifacts, 所以在计算MSSIM时,会使用到 circular-symmetric Gaussian weighting function圆对称的高斯加权公式 ​​, 标准差为1.5,和为1,来估计局部平均值,标准差,协方差。 

代码:

def gaussian(window_size, sigma):
    gauss = torch.Tensor([exp(-(x - window_size//2)**2/float(2*sigma**2)) for x in range(window_size)])
    return gauss/gauss.sum()

def create_window(window_size, channel):
    _1D_window = gaussian(window_size, 1.5).unsqueeze(1)
    _2D_window = _1D_window.mm(_1D_window.t()).float().unsqueeze(0).unsqueeze(0)
    window = Variable(_2D_window.expand(channel, 1, window_size, window_size).contiguous())
    return window

SSIM:

 

需要计算:

 

def _ssim(img1, img2, window, window_size, channel, size_average = True):
    mu1 = F.conv2d(img1, window, padding = window_size//2, groups = channel)
    mu2 = F.conv2d(img2, window, padding = window_size//2, groups = channel)

    mu1_sq = mu1.pow(2)
    mu2_sq = mu2.pow(2)
    mu1_mu2 = mu1*mu2

    sigma1_sq = F.conv2d(img1*img1, window, padding = window_size//2, groups = channel) - mu1_sq
    sigma2_sq = F.conv2d(img2*img2, window, padding = window_size//2, groups = channel) - mu2_sq
    sigma12 = F.conv2d(img1*img2, window, padding = window_size//2, groups = channel) - mu1_mu2

    C1 = 0.01**2
    C2 = 0.03**2

    ssim_map = ((2*mu1_mu2 + C1)*(2*sigma12 + C2))/((mu1_sq + mu2_sq + C1)*(sigma1_sq + sigma2_sq + C2))

    if size_average:
        return ssim_map.mean()
    else:
        return ssim_map.mean(1).mean(1).mean(1)

经验常取窗口大小为11x11.

 

公式转换:

同理

 

个人理解:

不同于MSE, SSIM以窗口的局部特征,在加上高斯核,相当于引入了平滑先验,符合人对视觉的感知。在用MSE作为loss的时候,可以额外加入SSIM,效果会更好。

SSIM Dissimilarity, SSIMD = (1 - SSIM)/2, 范围[0, 1], 越大越不相似,一般用SSIMD来作为网络的损失

参考

https://www.cns.nyu.edu/pub/eero/wang03-reprint.pdf​www.cns.nyu.edu/pub/eero/wang03-reprint.pdf

https://medium.com/srm-mic/all-about-structural-similarity-index-ssim-theory-code-in-pytorch-6551b455541e​medium.com/srm-mic/all-about-structural-similarity-index-ssim-theory-code-in-pytorch-6551b455541e

文武:图片结构相似性算法:SSIM53 赞同 · 4 评论文章​编辑

https://ece.uwaterloo.ca/~z70wang/research/ssim/​ece.uwaterloo.ca/~z70wang/research/ssim/

Po-Hsun-Su/pytorch-ssim: pytorch structural similarity (SSIM) loss (github.com)​github.com/pranjaldatta/SSIM-PyTorch/blob/master/SSI
 

 文章来源:SSIM (Structure Similarity Index Measure) 结构衡量指标+代码 - 知乎 (zhihu.com)

猜你喜欢

转载自blog.csdn.net/dou3516/article/details/132028533